ikechukwukalu/makeservice

一个用于构建服务类脚手的Laravel包。

v2.0.2 2024-08-26 23:44 UTC

README

Latest Version on Packagist Quality Score Code Quality Known Vulnerabilities Github Workflow Status Total Downloads Licence

一个用于构建Service、特质、枚举、外观、动作、仓库和接口类的Laravel包。

需求

  • PHP 7.3+
  • Laravel 8+

安装步骤

composer require ikechukwukalu/makeservice

服务类

生成一个新的服务类。

php artisan make:service SampleService

php artisan make:service SampleService  -f //This will overwrite an existing service class.

为特定的请求类生成一个新的服务类。

php artisan make:request SampleRequest

php artisan make:service SampleService --request=SampleRequest

一起生成服务类和表单请求类

php artisan make:service SampleService --request=SampleRequest -e

特质类

生成一个新的特质类。

php artisan make:traitclass SampleTrait

php artisan make:traitclass SampleTrait  -f //This will overwrite an existing trait class.

枚举类

生成一个新的枚举类。

php artisan make:enumclass Sample

php artisan make:enumclass Sample  -f //This will overwrite an existing enum class.

动作类

生成一个新的动作类。

php artisan make:action Sample

php artisan make:action Sample  -f //This will overwrite an existing action class.

契约类

生成一个新的契约/接口类。

php artisan make:interface SampleInterface

php artisan make:interface SampleInterface  -f //This will overwrite an existing interface  class.

仓库类

为特定的契约/接口类生成一个新的仓库类。

php artisan make:interface UserRepositoryInterface --model=User

php artisan make:repository UserRepository --model=User --interface=UserRepositoryInterface

php artisan make:repository UserRepository --model=User --interface=UserRepositoryInterface  -f //This will overwrite an existing repository class.

一起生成仓库类和契约/接口类

php artisan make:repository UserRepository --model=User -c

外观类

生成一个新的外观类。

php artisan make:facade Sample

php artisan make:facade Sample  -f //This will overwrite an existing facade class.

为仓库类生成一个外观类

php artisan make:facade User --contract=UserRepositoryInterface

绑定契约和仓库类

最后,我们需要在Laravel的服务容器中将UserRepository绑定到UserRepositoryInterface。我们将通过服务提供者来完成这项工作。使用以下命令创建一个

php artisan make:provider RepositoryServiceProvider

打开app/Providers/RepositoryServiceProvider.php并更新register函数以匹配以下内容

public function register()
{
    $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}

最后,将新的服务提供者添加到config/app.php中的providers数组中。

'providers' => [
    // ...other declared providers
    App\Providers\RepositoryServiceProvider::class,
];

绑定外观和动作类

如果我们有一个User外观类和一个UserService动作类,我们需要将其添加到Laravel的服务容器中。仓库类的外观将无需额外的绑定即可工作,因为它们已经在RepositoryServiceProvider类中添加到了容器中,但我们总是需要注册我们创建的每个外观类。

创建提供者

php artisan make:provider FacadeServiceProvider

绑定动作类

public function register()
{
    $this->app->bind('user', UserService::class);
}

注册提供者

'providers' => [
    // ...other declared providers
    App\Providers\FacadeServiceProvider::class,
    App\Providers\RepositoryServiceProvider::class,
];

最后,将外观添加到config/app.php中的aliases数组中。

    'aliases' => Facade::defaultAliases()->merge([
    // ...other declared facades
        'User' => App\Facades\User::class,
    ])->toArray(),

许可证

MS包是一个开源软件,根据MIT许可证许可。