Download the PHP package allysonsilva/laravel-artisan-domain-contexts without Composer
On this page you can find all versions of the php package allysonsilva/laravel-artisan-domain-contexts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download allysonsilva/laravel-artisan-domain-contexts
More information about allysonsilva/laravel-artisan-domain-contexts
Files in allysonsilva/laravel-artisan-domain-contexts
Package laravel-artisan-domain-contexts
Short Description A laravel package for using artisan commands in domain contexts
License MIT
Homepage https://github.com/allysonsilva/laravel-artisan-domain-contexts
Informations about the package laravel-artisan-domain-contexts
Laravel Artisan Domain Contexts
Table of Contents
- Overview
- Concepts
- What is "Domain"?
- What is "Contexts"?
- Questions
- Should I use this package in the default Laravel framework?
- When should I use this package?
- Concepts
- ๐ Installation
- Requirements
- Laravel version Compatibility
- Install the Package
- Publish the Config
- ๐ง Configuration
- ๐ Usage
- Understanding the
--context
option- Example
- Understanding the
--context-namespace
option - Understanding the
--all-contexts
option - Understanding the
--only-default
option - Understanding the
--multi-databases
option - List of commands using contexts
- ๐
make
commands- Examples
migrate
commands- Understanding the behavior of
migrate:fresh
andmigrate:refresh
- ๐น Demo
migrate:fresh
- ๐น Demo
migrate:refresh
- Understanding the behavior of
migrate:reset
,migrate:rollback
,migrate:status
andmigrate
- ๐น Demo
migrate:reset
- ๐น Demo
migrate:rollback
- Understanding the behavior of
db:seed
command- ๐น Demo
db:seed
- ๐น Demo
- Understanding the
- ๐งช Testing
- ๐ Changelog
- ๐ค Contributing
- ๐ Security
- ๐ Credits
- License
Overview
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.
Concepts
What is "Domain"?
- The term "Domain" has nothing to do with URL or host (www.example.com), but with the business, the specific sphere of activity or knowledge. In other words, the "Domain" is the company's business itself.
- "Domain" is the first (main ๐) word of DDD (Domain-Driven Design), which is the company's business.
See the article for a better understanding!
What is "Contexts"?
- If the "Domain" is the business of the company, the "Context" are the parts of that business, that is, the groups of related things, different parts of the business logic. For example, in an online store business, we could have the contexts of: Orders, Customers, Products and more. The "Domain" is the business of the online store, and the contexts are the parts of that same business.
See the article for better understanding!
Questions
Should I use this package in the default Laravel framework?
No, because the files handled by artisan
are already in their default folders.
When should I use this package?
- When the folder structure is not Laravel's default.
- When needed, for example, execute/manipulate
artisan
commands in different contexts/folders of the application.
๐ Installation
Requirements
The package has been developed and tested to work with the following minimum requirements:
- PHP 8.0
- Laravel 8.70
Laravel version Compatibility
Laravel | PHP | Package | Maintained |
---|---|---|---|
9.x | 8.0 | ^2.0 | โ |
8.70 | 8.0 | ^1.0 | โ |
Install the Package
You can install the package via Composer:
Publish the Config
You can then publish the package's config file by using the following command:
๐ง Configuration
-
Create a folder, inside the
app
folder with the same name as the config ofconfig('context.folders.domain')
-
Add trait to
app/Console/Kernel.php
file with usage options in all Laravel commands: - 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 ofconfig('context.folders.components')
.
๐ Usage
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.
Understanding the --context
option
This 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')
.
Example
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
.
Understanding the --context-namespace
option
This option will only be used in the make
commands, with that, see and explanation about it.
Understanding the --all-contexts
option
When 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.
Understanding the --only-default
option
By 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.
Understanding the --multi-databases
option
This 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.
List of commands using contexts
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
commands
Make 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')
.
Examples
The example commands below will use the following folder organization:
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.
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 toconfig('context.folders.domain')
.Post
: Value of the--context
option.Database/Migrations
: Value configured according toconfig('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.
A new job has been created at: app/Domain/Foo/Jobs/MyJob.php
The file path parts are:
Domain
: Value configured according toconfig('context.folders.domain')
.Foo
: Value of the--context
option.Jobs
: Value configured according toconfig('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.
The previous command will create a model in the path: app/Domain/Post/Models/Post.php
With the following content:
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
.
migrate
commands
The 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 databasesconfig('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:
Understanding the behavior of migrate:fresh
and migrate:refresh
- Both have the same set of options:
--context
and--only-default
migrate:refresh
has one more option which is--multi-databases
Both work in the same way / Summary:
- When no options are passed to the command, the default is to perform migrations from all contexts. For this reason it does not have the
--all-contexts
option. Well, it's the same behavior as if you had the option. - Cannot select/choose migration individually.
- Run all migrations from given context, from all contexts or from Laravel's default folder.
See the questions and answers below for better understanding:
-
How to run the command in a specific context?
-
How to run command in all contexts?
-
How to run command only in default Laravel migration folder?
- How can I run the command on multiple databases? (only refresh)
๐น Demo migrate:fresh
See the demo below for better understanding:
๐น Demo migrate:refresh
See the demo below for better understanding:
Understanding the behavior of migrate:reset
, migrate:rollback
, migrate:status
and migrate
- All 4 commands have the following options:
--context
--all-contexts
--only-default
--multi-databases
Summary of all commands:
- By default, a list will always appear with the migrations to choose from. Whether migrations from all contexts, or migrations from a specific context using the
--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. - When no options are passed to the command, the default is to list migrations from all contexts, using artisan's
choice
method. - To run the command on migrations of all contexts, use the
--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?
-
How to run command in all contexts?
-
How to run command only in default Laravel migration folder?
- How can I run the command on multiple databases?
๐น Demo migrate:reset
See the demo below for better understanding:
๐น Demo migrate:rollback
See the demo below for better understanding:
db:seed
command
Three 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?
-
How to run command in all contexts?
- How to run command only in default Laravel migration folder?
๐น Demo db:seed
See the demo below for better understanding:
๐งช Testing
๐ Changelog
Please see CHANGELOG for more information about the changes on this package.
๐ค Contributing
Please see CONTRIBUTING for details.
๐ Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
๐ Credits
- Allyson Silva
- All Contributors
License
The MIT License (MIT). Please see License File for more information.