alpenist/artisan-teleport

提供 Laravel Artisan 支持更改默认生成文件夹

1.0.5 2020-09-14 15:14 UTC

This package is auto-updated.

Last update: 2024-09-15 00:19:07 UTC


README

Packagist Downloads

为 Laravel Artisan 提供更灵活地更改默认生成目标文件夹的支持

适用对象

默认的 Laravel 文件夹结构在大多数情况下都运行得很好,但如果您决定采用不同的文件夹结构组织方式,特别是对于比通常更大的项目(例如(六边形领域驱动架构等)),那么如果您仍然想使用 artisan 来生成存根而不需要手动更改新创建的命名空间,这个包对您很有用。它将以与默认的 make 命令完全相同的方式工作,并提供每个命令提供的所有选项。

您可以在这种情况下与默认的 make 命令交替,并轻松生成存根

 |-- laravel
    |-- bootstrap
    ...   
    |-- src
       |-- App
          |-- Console
          |-- Exceptions
          |-- Providers
          ...
       |-- Acme
          |-- Clients
             |-- Models
             ...
          |-- Invoices
             |-- Models
             |-- Events
             |-- Observers
             ...
         ...   
       ...   
    ...   
    |-- vendor
    |-- .env    
    |-- server.php  

或者如果需要,从完全不同的项目(外部于主 Laravel 应用程序)中挂钩到 Laravel 文件夹中的 composer

 |-- laravel              |-- Project
    |-- bootstrap            |-- config
    ...                      |-- database
    |-- vendor               |-- resources
    |-- .env                 |-- src
    |-- server.php              |-- App
                                   |-- Console
                                   |-- Exceptions
                                   |-- Providers
                                   ...
                                |-- Acme
                                   |-- Clients
                                      |-- Models
                                      ...
                                   |-- Invoices
                                      |-- Models
                                      |-- Events
                                      |-- Observers
                                      ...
                                   ...
                                ...
                             |-- tests

安装

使用 composer 安装此包

composer require alpenist/artisan-teleport

使用以下命令发布配置文件

php artisan vendor:publish --provider="Ait\ArtisanTeleport\ArtisanTeleportServiceProvider" --tag="config"

在发布的 artisan-teleport.php 配置文件中更改包根的命名空间,如果需要,也可以更改相对于 base_path() 的根路径

return [

    /*
    |--------------------------------------------------------------------------
    | Artisan Teleport Signature
    |--------------------------------------------------------------------------
    |
    | This option controls the default artisan teleport "command" signature,
    | name. If no value is provided artisan teleport will simply not load.
    |
    */

    'signature_prefix' => 'create',

    /*
    |--------------------------------------------------------------------------
    | Artisan Teleport Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default base path and namespace that will
    | be used to generate stubs in the location specified. If left empty
    | it will default to the "base_path()" and laravel Application
    | default namespace.
    |
    | Default namespace will be overridden if a key with the same name is found
    | in the namespaces array below
    |
    */

    'defaults' => [
        'assets' => '../../../folder/sub-folder',
        'base' => '../../../folder/sub-folder/src/Acme',
        'namespace' => 'Acme',
    ],

    /*
    |--------------------------------------------------------------------------
    | Artisan Teleport Namespaces
    |--------------------------------------------------------------------------
    |
    | You may specify multiple namespaces which allows you to generate
    | any stubs within the specified namespace and will also set the root
    | location of the generator at run time.
    |
    | Lets say you set the default namespace to "Acme" and the base folder
    | to "Company" now when you generate a model it will have
    | namespace Acme\Models; and the file location will be in Company/Models
    |
    | Now suppose you have a Domain folder where you want to keep school course related
    | logic, you may generate a model in the "School" namespace and place it under
    | School/Models folder on the fly by passing a second argument to the artisan command
    | like this: php artisan create:model Course School
    | (assuming your signature prefix is "create")
    |
    | Each key has a required 'namespace' key if not found the key will be discarded and will
    | revert to the default namespace settings
    |
    */

    'namespaces' => [
        // The first key will override the defaults setting above
        // Now the defaults will be set to: 
        // namespace: AcmeCorp
        // base_path: ../../../folder/sub-folder/src/AcmeCorp/Domain
        'acme' => [
            'base_path' => '../../../folder/sub-folder/src/AcmeCorp',
            'path' => 'Domain',
            'namespace' => 'AcmeCorp',
        ],
        'app' => [
            'base_path' => '../../../folder/sub-folder/src/App',
            'path' => '',
            'namespace' => 'App',
        ],
    ],
];

用法

假设 signature_prefix 设置为 create,默认命名空间设置为 App,基础路径设置为 project/src/App,以下行将在 project/src/App/Clients/Models 文件夹中创建具有命名空间 App\Clients\Models 的模型

php artisan create:model Clients/Client

如果您在配置中定义了一个额外的命名空间 Acme,其基础设置为 project/src/Acme,您可以将命名空间作为第二个参数提供,它将在 project/src/Acme/Employees/Models 文件夹中创建具有命名空间 Acme\Employees\Models 的模型

php artisan create:model Employees/Employee Acme

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

安全

如果您发现任何与安全相关的问题,请使用问题跟踪器。

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件