alxdorosenco/porto-for-laravel

This package is a solution for easy building and working with Porto pattern structure


README

For Laravel [9.x, 8.x, 7.x, 6.x, 5.8, 5.7, 5.6, 5.5] please use the latest [9.0, 8.0, 7.0, 6.0, 5.8, 5.7, 5.6, 5.5] version.

This is a package who gives flexible way to build a structure of the Porto (Software Architectural Pattern) in your Laravel project. You should no longer to migrate a lot of files and folders handle.

This package will do the job for you in some clicks.

Introduction

Laravel is a popular and beautiful PHP framework who helps a lot to make your web applications. But web applications tend to grow and become harder to maintain and optimize. Unfortunately, Laravel, like other frameworks, does not have standard tools that allow you to write flexible, readable and easily maintainable code.

Porto (Software Architectural Pattern) is a brilliant solution for building large applications. This pattern helps you and your team to organize and maintain your code.

You can find more information about Porto by this link: https://github.com/Mahmoudz/Porto

How to install?

  1. First of all you need to install the package:

    composer require alxdorosenco/porto-for-laravel
    
  2. Next, you need to enable installed package in your .env file:

    PORTO_ENABLED=true
    
  3. Next, you can install porto structure using this command:

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

    You need to put directory path when the Porto structure will be installed or you can confirm installation in the default app/ directory.

    Of course, if you put the custom directory path, you need to set it in the autoload -> psr-4 in the composer.json file.

    You also can add directory name of your first container. For example:

    --container=<Container Name>
    

    This container will be installed in the Containers directory with standard structure. You also can force another container structures like:

    --container-default
    --container-api
    --container-cli
    --container-web
    --container-full
    
  4. Next, you need to put some changes in the bootstrap/app.php file.

    From:

     $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
     );

    To:

     $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. At last, you need to comment or clear Application Service Providers in the file config/app.php. Because you don't need them. The package loads automatically all providers from Ship and 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. That's all. Below you can find the skeleton structure of ship, each type of containers and information about adapted laravel console commands.

Ship structure

This is a skeleton of the installed Ship structure.

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

Containers

1. Standard

To create container with necessary files and folders you need to put this command:

php artisan make:container <Name>

Without forced container type will be created standard container structure.

Standard Container's Structure

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

2. Default

To create container with default route, controller, view and test file you can via command below:

php artisan make:container <Name> --default

Default Container's Structure

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

To create container only for API needles you can via command below:

php artisan make:container <Name> --api

API Container's Structure

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

4. CLI

To create container only for command line interface needles you can via command below:

php artisan make:container <Name> --cli

CLI Container's Structure

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

5. WEB

To create container only for web needles you can via command below:

php artisan make:container <Name> --web

WEB Container's Structure

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

6. Full

To create container with full structure you can via command below:

php artisan make:container <Name> --full

Full Container's Structure

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 console commands

1. Make

This is a list of adapted laravel console commands for the Porto.

Without container name some commands will be created class in the Ship.

Other commands require the container name

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>

Additional console commands

1. Make

This is a list of console commands for the Porto who does not exists in Laravel.

Without container name some commands will be created class in the Ship.

Other commands require the container name

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

Released under the MIT License, see LICENSE.