sunaoka / laravel-facade-generator
提供命令行生成门面层文件。
v1.5.0
2024-03-13 07:03 UTC
Requires
- php: ^7.1.3 || ^8.0
- illuminate/support: ^5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- orchestra/testbench: ^3.8 || ^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0
README
这是一个 artisan 控制台命令,用于生成服务、服务提供者和门面。
安装
composer require --dev sunaoka/laravel-facade-generator
用法
php artisan make:facade [Facade Name]
配置
php artisan vendor:publish --tag=facade-generator-config
<?php return [ /* |-------------------------------------------------------------------------- | Class names suffix | | Sets the string to be suffixed to the class name. |-------------------------------------------------------------------------- */ 'suffix' => [ 'facade' => '', 'service' => 'Service', 'provider' => 'ServiceProvider', ], /* |-------------------------------------------------------------------------- | Generate test | | If `false`, no test will be generated. |-------------------------------------------------------------------------- */ 'test' => true, ];
示例
php artisan make:facade Foo
生成:app/Facades/Foo.php
<?php namespace App\Facades; use Illuminate\Support\Facades\Facade; /** * Class Foo * * @method static \Mockery\MockInterface spy() Convert the facade into a Mockery spy. * @method static \Mockery\MockInterface partialMock() Initiate a partial mock on the facade. * @method static \Mockery\Expectation shouldReceive(string|array ...$methodNames) Initiate a mock expectation on the facade. * @method static void swap($instance) Hotswap the underlying instance behind the facade. * @method static void clearResolvedInstance(string $name) Clear a resolved facade instance. * @method static void clearResolvedInstances() Clear all of the resolved instances. * * @see \App\Services\FooService */ class Foo extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'Foo'; } }
生成:app/Providers/FooServiceProvider.php
<?php namespace App\Providers; use App\Services\FooService; use Illuminate\Support\ServiceProvider; class FooServiceProvider extends ServiceProvider { /** * Register services. * * @return void */ public function register() { $this->app->bind('Foo', FooService::class); } }
生成:app/Services/FooService.php
<?php namespace App\Services; class FooService { }
并且调用 artisan make:test
创建 tests/Feature/FooServiceTest.php
。
Laravel 5.8 到 10.x
您必须在 config/app.php
中添加提供者和别名。
'providers' => [ App\Providers\FooServiceProvider::class, ], 'aliases' => [ 'Foo' => App\Facades\Foo::class, ],
Laravel 11.x
您必须在 bootstrap/providers.php
中添加提供者。
return [ App\Providers\FooServiceProvider::class, ];
并且,您必须在 config/app.php
中添加别名。
'aliases' => [ 'Foo' => App\Facades\Foo::class, ],