alxdorosenco/porto-for-laravel

此包是构建和操作Porto模式结构(软件架构模式)的解决方案


README

对于 Laravel [9.x, 8.x, 7.x, 6.x, 5.8, 5.7, 5.6, 5.5],请使用最新的 [9.0, 8.0, 7.0, 6.0, 5.8, 5.7, 5.6, 5.5] 版本。

这是一个包,它以灵活的方式在您的 Laravel 项目中构建 Porto(软件架构模式)的结构。您不再需要迁移大量文件和文件夹。

此包只需几步即可为您完成工作。

简介

Laravel 是一个受欢迎且美观的 PHP 框架,它极大地帮助您制作网络应用程序。但是,网络应用程序往往容易增长且难以维护和优化。不幸的是,Laravel,与其他框架一样,没有标准工具允许您编写灵活、可读和易于维护的代码。

Porto(软件架构模式)是构建大型应用的绝佳解决方案。此模式帮助您和您的团队组织并维护代码。

您可以通过此链接了解更多关于 Porto 的信息: https://github.com/Mahmoudz/Porto

如何安装?

  1. 首先,您需要安装此包

    composer require alxdorosenco/porto-for-laravel
    
  2. 接下来,您需要在 .env 文件中启用已安装的包

    PORTO_ENABLED=true
    
  3. 接下来,您可以使用以下命令安装 Porto 结构

    php artisan porto:install --container=<Container Name> --container-<Container Type>
    

    您需要指定安装 Porto 结构的目录路径,或者您可以在默认的 app/ 目录中确认安装。

    当然,如果您指定了自定义目录路径,您需要在 composer.json 文件中的 autoload -> psr-4 中设置它。

    您还可以添加第一个容器的目录名称。例如

    --container=<Container Name>
    

    此容器将安装在 Containers 目录中,具有标准结构。您也可以强制使用其他容器结构,例如

    --container-default
    --container-api
    --container-cli
    --container-web
    --container-full
    
  4. 接下来,您需要在 bootstrap/app.php 文件中做一些更改。

     $app->singleton(
         Illuminate\Contracts\Http\Kernel::class,
         App\Http\Kernel::class
     );
     
     $app->singleton(
         Illuminate\Contracts\Console\Kernel::class,
         App\Console\Kernel::class
     );
     
     $app->singleton(
         Illuminate\Contracts\Debug\ExceptionHandler::class,
         App\Exceptions\Handler::class
     );

     $app->singleton(
         Illuminate\Contracts\Http\Kernel::class,
         <Porto path name>\Ship\Kernels\HttpKernel::class
     );
     
     $app->singleton(
         Illuminate\Contracts\Console\Kernel::class,
         <Porto path name>\Ship\Kernels\ConsoleKernel::class
     );
     
     $app->singleton(
         Illuminate\Contracts\Debug\ExceptionHandler::class,
         <Porto path name>\Ship\Exceptions\Handler::class
     );
  5. 最后,您需要在文件 config/app.php 中注释或清除应用程序服务提供者。因为您不需要它们。该包自动加载 Ship 和 Containers 中的所有提供者。

    /*
    * Application Service Providers...
    */
    //App\Providers\AppServiceProvider::class,
    //App\Providers\AuthServiceProvider::class,
    // App\Providers\BroadcastServiceProvider::class,
    //App\Providers\EventServiceProvider::class,
    //App\Providers\RouteServiceProvider::class
  6. 这就是全部。下面您可以找到船的骨架结构、每种类型的容器以及关于适配 Laravel 控制台命令的信息。

船结构

这是已安装的 Ship 结构的骨架。

Ship
    ├── Abstracts
    │   ├── Broadcasting
    │   │   ├── Channel.php
    │   │   ├── PresenceChannel.php
    │   │   └── PrivateChannel.php
    │   │── Commands
    │	│   └── ConsoleCommand.php
    │   ├── Components
    │	│   └── Component.php
    │   ├── Controllers
    │	│   └── Controller.php
    │   ├── Events
    │	│   └── Event.php
    │   ├── Exceptions
    │	│   └── Handler.php
    │   ├── Factories
    │	│   └── Factory.php
    │   ├── Jobs
    │	│   └── Job.php
    │   ├── Mails
    │	│   ├── Mailables
    │	│   │   ├── Content.php
    │	│   │   └── Envelope.php
    │	│   └── Mailable.php
    │   ├── Middleware
    │   │   ├── Authenticate.php
    │   │   ├── EncryptCookies.php
    │   │   ├── PreventRequestsDuringMaintenance.php
    │   │   ├── TrimStrings.php
    │   │   ├── TrustHosts.php
    │   │   ├── TrustProxies.php
    │   │   ├── ValidateSignature.php
    │   │   └── VerifyCsrfToken.php
    │   ├── Models
    │   │   ├── BaseModel.php
    │   │   ├── UserModel.php
    │   │   ├── Pivot.php
    │   │   ├── MorphPivot.php
    │   │   └── Builder.php
    │   ├── Notifications
    │	│   ├── Messages
    │	│   │   └── MailMessage.php
    │   │   └── Notification.php
    │   ├── Policies
    │	│   └── Policy.php
    │   ├── Providers
    │   │   ├── AuthServiceProvider.php
    │   │   ├── ServiceProvider.php
    │   │   ├── EventServiceProvider.php
    │   │   └── RouteServiceProvider.php
    │   ├── Requests
    │   │   ├── Request.php
    │   │   └── FormRequest.php
    │   ├── Responses
    │   │   ├── Response.php
    │   │   ├── RedirectResponse.php
    │   │   └── JsonResource.php
    │   ├── Resources
    │	│   └── ResourceCollection.php
    │   ├── Seeders
    │	│   └── Seeder.php
    │   ├── Tests
    │	│   └── PhpUnit
    │	│       └── TestCase.php
    │   ├── Translations
    │	│   └── PotentiallyTranslatedString.php
    │   └── Views
    │	    └── Component.php
    ├── Broadcasting
    │   ├── Channel.php
    │   ├── PresenceChannel.php
    │   └── PrivateChannel.php
    ├── Commands
    ├── Configs
    ├── Controllers
    │   └── Controller.php
    ├── Events
    │   └── Event.php 
    ├── Exceptions
    │   └── Handler.php
    ├── Helpers
    │   └── helper.php
    ├── Kernels
    │   ├── ConsoleKernel.php
    │   └── HttpKernel.php 
    ├── Mails
    │   └── Mailables
    │       ├── Envelope.php
    │       └── Content.php
    ├── Middleware
    │   ├── Authenticate.php
    │   ├── EncryptCookies.php
    │   ├── PreventRequestsDuringMaintenance.php
    │   ├── RedirectIfAuthenticated.php
    │   ├── TrimStrings.php
    │   ├── TrustHosts.php
    │   ├── TrustProxies.php
    │   ├── ValidateSignature.php
    │   └── VerifyCsrfToken.php
    ├── Migrations
    ├── Models
    │   ├── Model.php
    │   ├── UserModel.php
    │   ├── Pivot.php
    │   ├── MorphPivot.php
    │   ├── Builder.php
    │   └── Scope.php
    ├── Notifications
    │   ├── Messages
    │   │   └── MailMessage.php
    │   └── Notification.php
    ├── Policies
    │   └── Policy.php
    ├── Providers
    │   ├── AuthServiceProvider.php
    │   ├── BroadcastServiceProvider.php
    │   ├── EventServiceProvider.php
    │   └── RouteServiceProvider.php
    ├── Requests
    │   ├── Request.php
    │   └── FormRequest.php
    ├── Responses
    │   ├── Response.php
    │   └── RedirectResponse.php
    ├── Resources
    │   ├── JsonResource.php
    │   └── ResourceCollection.php
    ├── Seeders
    ├── Tests
    │   └── TestCase.php
    ├── Traits
    │   └── CreatesApplication.php
    └── Translations

容器

1. 标准

要创建具有必要文件和文件夹的容器,您需要放置以下命令

php artisan make:container <Name>

如果没有强制容器类型,将创建标准容器结构。

标准容器的结构

Container
	├── Actions
	├── Tasks
	├── Models
	├── Loaders
	│   ├── AliasesLoader.php
	│   ├── ProvidersLoader.php
	│   └── MiddlewareLoader.php  
	└── UI
	    ├── WEB
	    │   ├── Routes
	    │   ├── Controllers
	    │   └── Views
	    ├── API
	    │   ├── Routes
	    │   ├── Controllers
	    │   └── Transformers
	    └── CLI
	        ├── Routes
	        └── Commands

2. 默认

要创建具有默认路由、控制器、视图和测试文件的容器,您可以执行以下命令

php artisan make:container <Name> --default

默认容器的结构

Container
	├── Actions
	├── Tasks
	├── Models
	├── Loaders
	│   ├── AliasesLoader.php
	│   ├── ProvidersLoader.php
	│   └── MiddlewareLoader.php  
	└── UI
	    ├── WEB
	    │   ├── Routes
	    │   │   ├── home.php
	    │   ├── Controllers
	    │   │   ├── HomeController.php 
	    │   ├── Views
	    │   │   ├── home.blade.php
	    │   ├── Tests
	    │   │   ├── Functional
	    │   └── └── └── ExampleTest.php 
	    ├── API
	    │   ├── Routes
	    │   ├── Controllers
	    │   └── Transformers
	    └── CLI
	        ├── Routes
	        └── Commands

3. API

要仅创建用于API针的容器,可以通过以下命令实现

php artisan make:container <Name> --api

API容器的结构

Container
	├── Actions
	├── Tasks
	├── Models
	├── Loaders
	│   ├── AliasesLoader.php
	│   ├── ProvidersLoader.php
	│   └── MiddlewareLoader.php  
	└── UI
	    └── API
	        ├── Routes
	        ├── Controllers
	        └── Transformers

4. 命令行界面(CLI)

要仅创建用于命令行界面针的容器,可以通过以下命令实现

php artisan make:container <Name> --cli

CLI容器的结构

Container
	├── Actions
	├── Tasks
	├── Models
	├── Loaders
	│   ├── AliasesLoader.php
	│   ├── ProvidersLoader.php
	│   └── MiddlewareLoader.php  
	└── UI
	    └── CLI
	        ├── Routes
	        └── Commands

5. 网络(WEB)

要仅创建用于网络针的容器,可以通过以下命令实现

php artisan make:container <Name> --web

WEB容器的结构

Container
	├── Actions
	├── Tasks
	├── Models
	├── Loaders
	│   ├── AliasesLoader.php
	│   ├── ProvidersLoader.php
	│   └── MiddlewareLoader.php  
	└── UI
	    └── WEB
	        ├── Routes
	        ├── Controllers
	        └── Views

6. 完整

要创建具有完整结构的容器,可以通过以下命令实现

php artisan make:container <Name> --full

完整容器的结构

Container
	├── Actions
	├── Tasks
	├── Models
	├── Values
	├── Events
	├── Listeners
	├── Policies
	├── Exceptions
	├── Contracts
	├── Traits
	├── Enums
	├── Jobs
	├── Notifications
	├── Providers
	├── Configs
	├── Loaders
	│   ├── AliasesLoader.php
	│   ├── ProvidersLoader.php
	│   └── MiddlewareLoader.php
	├── Mails
	│   └── Templates
	├── Data
	│   ├── Migrations
	│   ├── Seeders
	│   ├── Factories
	│   ├── Criteria
	│   ├── Repositories
	│   ├── Validators
	│   ├── Transporters
	│   └── Rules
	├── Tests
	│   ├── Traits
	│   └── Unit
	└── UI
	    ├── WEB
	    │   ├── Routes
	    │   ├── Controllers
	    │   ├── Requests
	    │   ├── Tests
	    │   │   └── Functional
	    │   └── Views
	    ├── API
	    │   ├── Routes
	    │   ├── Controllers
	    │   ├── Requests
	    │   ├── Tests
	    │   │   └── Functional
	    │   └── Transformers
	    └── CLI
	        ├── Routes
	        ├── Commands
	        └── Tests
	            └── Functional

Laravel 控制台命令

1. 构建(Make)

这是一个适用于Porto的适配Laravel控制台命令的列表。

没有容器名称,某些命令将在Ship中创建类。

其他命令需要容器名称

make:cast

php artisan make:cast <Name> --container=<Container Name>

make:channel

php artisan make:channel <Name> --container=<Container Name>

make:command

php artisan make:command <Name>
php artisan make:command <Name> --container=<Container Name>

make:component

php artisan make:component <Name> --container=<Container Name>

make:controller

php artisan make:controller <Name> --container=<Container Name>

make:enum

php artisan make:enum <Name> --container=<Container Name>

make:event

php artisan make:event <Name>
php artisan make:event <Name> --container=<Container Name>

make:exception

php artisan make:exception <Name>
php artisan make:exception <Name> --container=<Container Name>

make:factory

php artisan make:factory <Name> --container=<Container Name>

make:job

php artisan make:job <Name>
php artisan make:job <Name> --container=<Container Name>

make:listener

php artisan make:listener <Name> --container=<Container Name>

make:mail

php artisan make:mail <Name>
php artisan make:mail <Name> --container=<Container Name>

make:middleware

php artisan make:middleware <Name>
php artisan make:middleware <Name> --container=<Container Name>

make:model

php artisan make:model <Name> --container=<Container Name>

make:notification

php artisan make:notification <Name>
php artisan make:notification <Name> --container=<Container Name>

make:observer

php artisan make:observer <Name> --container=<Container Name>

make:policy

php artisan make:policy <Name> --container=<Container Name>

make:provider

php artisan make:provider <Name>
php artisan make:provider <Name> --container=<Container Name>

make:request

php artisan make:request <Name> --container=<Container Name> --uiType=api
php artisan make:request <Name> --container=<Container Name> --uiType=web

make:resource

php artisan make:resource <Name> --container=<Container Name>

make:rule

php artisan make:rule <Name> --container=<Container Name>

make:scope

php artisan make:scope <Name> --container=<Container Name>

make:seeder

php artisan make:seeder <Name>
php artisan make:seeder <Name> --container=<Container Name>

make:test

php artisan make:test <Name> --container=<Container Name> --uiType=api
php artisan make:test <Name> --container=<Container Name> --uiType=cli
php artisan make:test <Name> --container=<Container Name> --uiType=web

2. 模型(Model)

make:show

php artisan make:show --container=<Container Name>

附加控制台命令

1. 构建(Make)

这是一个适用于Porto的控制台命令列表,这些命令在Laravel中不存在。

没有容器名称,某些命令将在Ship中创建类。

其他命令需要容器名称

make:action

php artisan make:action <Name> --container=<Container Name>

make:config

php artisan make:config <Name>
php artisan make:config <Name> --container=<Container Name>

make:contract

php artisan make:contract <Name> --container=<Container Name>

make:helper

php artisan make:helper <Name>

make:repository

php artisan make:repository <Name>

make:task

php artisan make:task <Name> --container=<Container Name>

make:trait

php artisan make:trait <Name> --container=<Container Name>
php artisan make:trait <Name> --container=<Container Name> --test

make:translation

php artisan make:translation <Name> --lang=<lang code>

make:value

php artisan make:value <Name> --container=<Container Name>

许可证(License)

在MIT许可证下发布,请参阅LICENSE