diego-rlima / artisan-make-file-location
修改由 "artisan make" 命令生成的文件命名空间/位置的权限。
Requires
- php: >=7.1.0
- laravel/framework: >=5.4
This package is auto-updated.
Last update: 2024-07-21 00:23:55 UTC
README
修改由 "artisan make" 命令生成的文件命名空间/位置的权限。
要求
此包需要 Laravel 5.4 或更高版本 和 PHP 7.1.0 或更高版本。
安装
$ composer require diego-rlima/artisan-make-file-location
对于 Laravel 5.4,您必须注册此包的服务提供者。将以下代码添加到您的 config/app.php
文件的 providers 部分。
DRL\AMFL\ArtisanServiceProvider::class,
使用
您可以使用所有 "artisan make" 命令,就像平常一样。但现在,您可以添加 --prefix 和 --suffix 选项来更改文件命名空间。
注意:对于没有命名空间(如迁移)的文件,只能使用前缀。
前缀
$ php artisan make:controller ProductController --prefix=Units\\Products\\Controllers
这将输出具有命名空间 App\Units\Products\Controllers 的文件。
后缀
$ php artisan make:controller ProductController --suffix=Products
这将输出具有命名空间 App\Http\Controllers\Products 的文件。
前缀和后缀同时使用
$ php artisan make:controller ProductController --prefix=Units --suffix=Products
这将输出具有命名空间 App\Units\Controllers\Products 的文件。
自定义
该包配置为与 Laravel 标准兼容,但允许您设置前缀和后缀。然而,您可以替换 Laravel 模式为您自己的。
使用以下命令发布配置
$ php artisan vendor:publish --provider="DRL\AMFL\ArtisanServiceProvider"
现在您有一个 config/amfl.php 文件。设置被分配给具有命名空间的文件和没有命名空间的文件。
return [ /* |-------------------------------------------------------------------------- | Files namespaces |-------------------------------------------------------------------------- */ // List of all files with namespace. Eg.: 'controller' => '{root}\{prefix|default:Http}\Controllers\{suffix}', 'test' => '{root}\{prefix}\{type}\{suffix}', /* |-------------------------------------------------------------------------- | Files locations |-------------------------------------------------------------------------- */ // List of all files without namespace. Eg.: 'seeder' => '{root}/{prefix}/seeds/{name}.php', ];
对于具有命名空间的文件,{root} 通常会被 "App" 命名空间替换。当然,{prefix} 和 {suffix} 将会被您选择的前缀和后缀替换。
在测试文件中,{root} 将被替换为 "Tests" 命名空间。这些文件也有命名空间变体。 {type} 将被替换为 "Unit" 或 "Feature"。
对于没有命名空间的文件,{root} 将被替换为应用程序的根目录。 {prefix} 和 {name} 将分别被前缀和文件名替换。
重要:如果您打算在其他文件夹中创建迁移,请确保文件夹已经创建,否则迁移将不会被创建。这是由于原始命令的编写方式。
使前缀/后缀成为必需
您只需将 |required
添加到文件配置中即可。例如。
return [ // Now you will always have to enter a prefix when creating a Model. 'model' => '{root}\{prefix|required}', // The same for the notification suffix. 'notification' => '{root}\{prefix}\Notifications\{suffix|required}', ];
为前缀/后缀定义默认值
只需将 |default:YouDefaultValue
添加到文件配置中即可。例如。
return [ // Now, if you do not set a prefix, the default value will be used. 'model' => '{root}\{prefix|default:Models}', // The same for the suffix. 'rule' => '{root}\{prefix}\Rules\{suffix|default:Admin}', ];
注意:如果前缀/后缀是必需的,则不会应用默认值。
扩展其他命令
您想在您的命令或第三方命令中使用前缀/后缀吗?那就这么做吧!
假设您的命令类如下所示
namespace App\Console\Commands; use Illuminate\Console\GeneratorCommand; class FooBarMakeCommand extends GeneratorCommand { protected $name = 'make:foobar'; protected $description = 'Create a new bar'; protected $type = 'FooBar'; protected function getStub() { return __DIR__ . '/stubs/bar.stub'; } protected function getDefaultNamespace($rootNamespace) { return $rootNamespace.'\Foo\Bar'; } }
您需要更改类,使其能够检索我们设置的模式。它将类似于
use DRL\AMFL\TraitCommand; class FooBarMakeCommand extends GeneratorCommand { // You will use trait. use TraitCommand; // You will use the amflCustomNamespace() method to retrieve the correct namespace and return it in the getDefaultNamespace() method. protected function getDefaultNamespace($rootNamespace) { return $this->amflCustomNamespace($rootNamespace); } // You will create the amflInit() method and load the correct settings. protected function amflInit() { $this->amflCommandSetup('foobar'); // "foobar" must be the configuration key within your "config/amfl.php" file; } // Code omitted }
如果您的命令将生成没有命名空间的文件,代码需要稍作修改。它应该看起来像
use DRL\AMFL\TraitCommand; class FooBarMakeCommand extends GeneratorCommand { use TraitCommand; // You will use the amflCustomPath() method to retrieve the correct path and return it in the getPath() method. protected function getPath($name) { $rootPath = $this->laravel->basePath(); return return $this->amflCustomPath($rootPath, $name); } protected function amflInit() { $this->amflCommandSetup('foobar'); } }
在您的 AppServiceProvider 中扩展命令列表。
use DRL\AMFL\CommandsList; use App\Console\Commands\FooBarMakeCommand; class AppServiceProvider extends ServiceProvider { public function register() { // The first argument must be the name of the command. The second is a function that receives the $app variable and returns an instance of the command class. CommandsList::extend('command.foobar.make', function ($app) { return new FooBarMakeCommand($app['files']); }); } }
将命令的模式配置放在您的 "config/amfl.php" 文件中。
return [ // For files with namespace. 'foobar' => '{root}\{prefix}\Foo\{suffix}', // For files without namespace. 'foobar' => '{root}/{prefix}/Foo/{name}.php', ];