This package provides the ability to use artisan commands in different domain contexts. It allows to work interactively in the migration commands and seeders, choosing which class should be executed.
See the article for a better understanding!
See the article for better understanding!
No, because the files handled by artisan
are already in their default folders.
artisan
commands in different contexts/folders of the application.The package has been developed and tested to work with the following minimum requirements:
Laravel | PHP | Package | Maintained |
---|---|---|---|
9.x | 8.0 | ^2.0 | โ |
8.70 | 8.0 | ^1.0 | โ |
You can install the package via Composer:
composer require allysonsilva/laravel-artisan-domain-contexts
You can then publish the package's config file by using the following command:
php artisan vendor:publish --tag="context-config"
Create a folder, inside the app
folder with the same name as the config of config('context.folders.domain')
Add trait to app/Console/Kernel.php
file with usage options in all Laravel commands:
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
+use Allyson\ArtisanDomainContext\Concerns\ArtisanConsoleTrait;
class Kernel extends ConsoleKernel
{
+ use ArtisanConsoleTrait;
Inside the domain folder (config('context.folders.domain')
), is where you can find Laravel components (such as migrations, seeders, models, jobs, etc). The names of the folders, where the classes are, are in the config of config('context.folders.components')
.
To use commands by context, the following options have been added to Laravel commands:
--context
--context-namespace
--all-contexts
--only-default
--multi-databases
Some options are only available in a certain command, but the --context
option is present in all commands in the list below!
The list of standard laravel commands that were handled by adding these options can be seen table below.
--context
optionThis option is present in all commands listed below!
When this option is passed in the command, then the component/class/resource is manipulated or created, according to the resource type setting in config('context.folders.components')
.
To change the path/name of the folder where the class should be manipulated or created, see the config in config('context.folders.components')
.
So, for example, to create a middleware in a given context, in this case, in the user context, the command can be: php artisan make:middleware --context=User YourMiddleware
.
A middleware class was created in app/Domain/User/Http/Middlewares/YourMiddleware.php
.
If the config of config('context.folders.components.middlewares')
has the value of Http/AnotherFolder
instead of Http/Middlewares
(default), and the previous command is executed, then the class would be created in app/Domain/User/Http/AnotherFolder/YourMiddleware.php
.
--context-namespace
optionThis option will only be used in the make
commands, with that, see and explanation about it.
--all-contexts
optionWhen this option is passed in the command, then it will be executed non-interactively, that is, it will execute the context-specific filtered classes.
This option is not present in the make
commands only in the migration and db:seed commands. See the table below.
It has the same behavior as the --force
option.
--only-default
optionBy default, migrations commands are executed in all contexts when no options are passed. To run migrations commands in Laravel's default folder (database/migrations
) use this option.
This option is only in the migration and db:seed
commands. It is not present in the make
commands.
--multi-databases
optionThis option is only present in migration commands!
By default the migration commands are run on the database configured in the DB_DATABASE
env, so you can only use one database in the command. With this option, you can use multiple databases for the same command via the config config('context.migrations.databases')
When this option is passed, then the command will be executed on different databases according to the config of config('context.migrations.databases')
.
The config of config('context.migrations.databases')
refers to the name of the database that the operation will be performed on.
Commands | Additional Command Options | ||||
---|---|---|---|---|---|
--context |
--context-namespace |
--all-contexts |
--only-default |
--multi-databases |
|
make:cast |
โ๏ธ | โ๏ธ | |||
make:channel |
โ๏ธ | โ๏ธ | |||
make:command |
โ๏ธ | โ๏ธ | |||
make:event |
โ๏ธ | โ๏ธ | |||
make:exception |
โ๏ธ | โ๏ธ | |||
make:factory |
โ๏ธ | โ๏ธ | |||
make:factory |
โ๏ธ | โ๏ธ | |||
make:job |
โ๏ธ | โ๏ธ | |||
make:listener |
โ๏ธ | โ๏ธ | |||
make:mail |
โ๏ธ | โ๏ธ | |||
make:middleware |
โ๏ธ | โ๏ธ | |||
make:migration |
โ๏ธ | โ๏ธ | |||
make:model |
โ๏ธ | โ๏ธ | |||
make:notification |
โ๏ธ | โ๏ธ | |||
make:observer |
โ๏ธ | โ๏ธ | |||
make:policy |
โ๏ธ | โ๏ธ | |||
make:provider |
โ๏ธ | โ๏ธ | |||
make:request |
โ๏ธ | โ๏ธ | |||
make:resource |
โ๏ธ | โ๏ธ | |||
make:rule |
โ๏ธ | โ๏ธ | |||
make:seeder |
โ๏ธ | โ๏ธ | |||
migrate:fresh |
โ๏ธ | โ๏ธ | |||
migrate:refresh |
โ๏ธ | โ๏ธ | โ๏ธ | ||
migrate:reset |
โ๏ธ | โ๏ธ | โ๏ธ | โ๏ธ | |
migrate:rollback |
โ๏ธ | โ๏ธ | โ๏ธ | โ๏ธ | |
migrate:status |
โ๏ธ | โ๏ธ | โ๏ธ | โ๏ธ | |
migrate |
โ๏ธ | โ๏ธ | โ๏ธ | โ๏ธ | |
db:seed |
โ๏ธ | โ๏ธ | โ๏ธ |
make
commandsMake commands, create files in a given context or Laravel's default folder when no context is specified.
All the make
commands that are listed in the table above, have 2 options in their command, which are:
--context
: Name of the folder where the class will be created, if not passed in the command, then the class will be created in the Laravel's default folder.
--context-namespace
: Custom namespace that will be used in place of the class's normal namespace.
To change the name of the component folder in which the class is created, see the following config config('context.folders.components')
.
The example commands below will use the following folder organization:
app/
โโโ Domain
โย ย โโโ Foo
โย ย โโโ Post
โโโย โโโ User
How to create a [MIGRATION] in a specific context?
Use the following command below as an example, passing the --context
option with the name of the context in which the migration will be created.
The migration files will be saved in the config folder config('context.folders.components.migrations')
in the context folder, according to the --context
option of the command.
php artisan make:migration --context=Post create_posts_table
A new migration has been created at: app/Domain/Post/Database/Migrations/2022_xx_xx_xxxxxx_create_posts_table.php
The file path parts are:
Domain
: Value configured according to config('context.folders.domain')
.Post
: Value of the --context
option.Database/Migrations
: Value configured according to config('context.folders.components.migrations')
.How to create a [JOB] in a specific context?
In the same way as explained earlier in the case of migration, the --context
option is also used to create a new job in a specific context.
php artisan make:job --context=Foo MyJob
A new job has been created at: app/Domain/Foo/Jobs/MyJob.php
The file path parts are:
Domain
: Value configured according to config('context.folders.domain')
.Foo
: Value of the --context
option.Jobs
: Value configured according to config('context.folders.components.jobs')
.How to create a class/component with a custom namespace?
Use the --context-namespace
option to customize the class namespace prefix.
If you want to create a model with a specific namespace, Use the following command below as an example.
php artisan make:model --context=Post --context-namespace=PostDomain Post
The previous command will create a model in the path: app/Domain/Post/Models/Post.php
With the following content:
<?php
+namespace PostDomain\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
}
The namespace for the above class is: namespace PostDomain\Models
.
How to create class in Laravel's default folder?
To create the class in the default Laravel folder, don't use/pass the --context
option!
The following command will create the class in Laravel's default folder at app/Events/MyEvent.php
.
php artisan make:event YourEvent
migrate
commandsThe migration commands are executed interactively, and you can even select each individual migration, or all migrations in a given context to perform the operation.
The following are the options that may be on commands:
--context
: Context where migrations should be performed/handled.
--all-contexts
: The command must be run on migrations from all contexts.
--only-default
: If this option is passed, then only migrations from the default Laravel folder (database/migrations
) will be used for the command.
--multi-databases
: Will run the command on all config databases config('context.migrations.databases')
.
To see the migration commands in action, let's use the folder organization below as an example and see the expected results:
app
โโโ Domain
โโโ Foo
โย ย โโโ Database
โย ย โโโ Migrations
โย ย โย ย โโโ 2022_02_30_000000_create_baz_table.php
โย ย โย ย โโโ 2022_02_30_000000_create_foo_table.php
โย ย โโโ Seeders
โย ย โโโ BazTableSeeder.php
โย ย โโโ FooTableSeeder.php
โโโ Post
โย ย โโโ Database
โย ย โโโ Migrations
โย ย โย ย โโโ 2022_02_30_000000_create_posts_1_table.php
โย ย โย ย โโโ 2022_02_30_000000_create_posts_2_table.php
โย ย โย ย โโโ 2022_02_30_000000_create_posts_3_table.php
โย ย โโโ Seeders
โย ย โโโ PostsTableSeeder1.php
โย ย โโโ PostsTableSeeder2.php
โย ย โโโ PostsTableSeeder3.php
โโโ User
โโโ Database
โโโ Migrations
โย ย โโโ 2022_02_30_000000_create_users_1_table.php
โย ย โโโ 2022_02_30_000000_create_users_2_table.php
โย ย โโโ 2022_02_30_000000_create_users_3_table.php
โโโ Seeders
โโโ UsersTableSeeder1.php
โโโ UsersTableSeeder2.php
โโโ UsersTableSeeder3.php
migrate:fresh
and migrate:refresh
--context
and --only-default
migrate:refresh
has one more option which is --multi-databases
Both work in the same way / Summary:
--all-contexts
option. Well, it's the same behavior as if you had the option.See the questions and answers below for better understanding:
How to run the command in a specific context?
php artisan migrate:<fresh or refresh> --context=YOUR_CONTEXT
How to run command in all contexts?
php artisan migrate:<fresh or refresh>
How to run command only in default Laravel migration folder?
php artisan migrate:<fresh or refresh> --only-default
How can I run the command on multiple databases? (only refresh)
# In all config databases `config('context.migrations.databases')`
php artisan migrate:refresh --multi-databases
# Or on multiple databases of a specific context:
php artisan migrate:refresh --context=User --multi-databases
migrate:fresh
See the demo below for better understanding:
migrate:refresh
See the demo below for better understanding:
migrate:reset
, migrate:rollback
, migrate:status
and migrate
--context
--all-contexts
--only-default
--multi-databases
Summary of all commands:
--context
option, there will always be a list of migrations to be chosen and used in the commands. To run non-interactive, use the --force
option.choice
method.--all-contexts
option.See the questions and answers below for better understanding:
The $command
variable, can be one of the items ['migrate:reset', 'migrate:rollback', 'migrate:status', 'migrate']
.
How to run the command in a specific context?
# A list of migrations present in the context will appear to be chosen for execution!
php artisan $command --context=YOUR_CONTEXT
# To execute in a "forced" way, that is, all migrations from a given context, use the `--force` option!
php artisan $command --context=YOUR_CONTEXT --force
How to run command in all contexts?
# List of migrations to choose which will be performed
php artisan $command
# Run migrations from all contexts "forced"
php artisan $command --all-contexts
How to run command only in default Laravel migration folder?
php artisan $command --only-default
How can I run the command on multiple databases?
# In all config databases `config('context.migrations.databases')`
php artisan $command --multi-databases
# Or on multiple databases of a specific context:
php artisan $command --context=User --multi-databases
# Or force execution of the command
php artisan $command --context=User --multi-databases --force
migrate:reset
See the demo below for better understanding:
migrate:rollback
See the demo below for better understanding:
db:seed
commandThree options are found in the command:
--context
--all-contexts
--only-default
See the questions and answers below for better understanding:
How to run the command in a specific context?
# A list of seeders present in the context will appear to be chosen for execution!
php artisan db:seed --context=YOUR_CONTEXT
# To execute in a "forced" way, that is, all seeders from a given context, use the `--force` option!
php artisan db:seed --context=YOUR_CONTEXT --force
How to run command in all contexts?
# List of seeders to choose which will be performed
php artisan db:seed
# Run seeders from all contexts "forced"
php artisan db:seed --all-contexts
How to run command only in default Laravel migration folder?
php artisan db:seed --only-default
db:seed
See the demo below for better understanding:
composer test:unit
Please see CHANGELOG for more information about the changes on this package.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email github@allyson.dev instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.