PHPackages                             allysonsilva/laravel-artisan-domain-contexts - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. allysonsilva/laravel-artisan-domain-contexts

ActiveLibrary[CLI &amp; Console](/categories/cli)

allysonsilva/laravel-artisan-domain-contexts
============================================

A laravel package for using artisan commands in domain contexts

v2.2.0(3y ago)301373MITPHPPHP &gt;=8.0

Since Mar 6Pushed 3y ago3 watchersCompare

[ Source](https://github.com/allysonsilva/laravel-artisan-domain-contexts)[ Packagist](https://packagist.org/packages/allysonsilva/laravel-artisan-domain-contexts)[ Docs](https://github.com/allysonsilva/laravel-artisan-domain-contexts)[ RSS](/packages/allysonsilva-laravel-artisan-domain-contexts/feed)WikiDiscussions 2.x Synced 4d ago

READMEChangelog (7)Dependencies (12)Versions (12)Used By (0)

Laravel Artisan Domain Contexts
===============================

[](#laravel-artisan-domain-contexts)

 [![Social Card of Laravel Artisan Domain Contexts](/images/art/socialcard.png)](/images/art/socialcard.png)

[![PHP Version](https://camo.githubusercontent.com/202a75e9fb46e51fcd0dea107428473bb0258c1945ae8925dbcbb3230039fa11/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f616c6c79736f6e73696c76612f6c61726176656c2d6172746973616e2d646f6d61696e2d636f6e74657874733f636f6c6f723d253233344635423933266c6f676f3d706870)](https://www.php.net)[![Laravel Version](https://camo.githubusercontent.com/4e55c6d6cca644ed9cd390b02fa876085b2eaed01fe8b17ec6865a5ca18ef1d1/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d6c61726176656c266d6573736167653d254532253839254135392e3026636f6c6f723d666632643230266c6f676f3d6c61726176656c)](https://laravel.com)[![CI Status](https://github.com/allysonsilva/laravel-artisan-domain-contexts/actions/workflows/ci.yml/badge.svg)](https://github.com/allysonsilva/laravel-artisan-domain-contexts/actions/workflows/ci.yml)[![PHPCS - GitHub Workflow Status](https://camo.githubusercontent.com/1f81c2239fa5015a99796462ad72f8b20ea3955c9511c21a7539bac8bdf142ba/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f616c6c79736f6e73696c76612f6c61726176656c2d6172746973616e2d646f6d61696e2d636f6e74657874732f504850253230436f6465536e69666665722532302d253230436f64696e672532305374616e64617264733f6c6162656c3d5048504353266c6f676f3d676974687562)](https://github.com/allysonsilva/laravel-artisan-domain-contexts/actions/workflows/phpcs.yml)[![PHPMD - GitHub Workflow Status](https://camo.githubusercontent.com/806ef57ae3543fb8daf95945fe43419472a27cc25fce7a79ce87eb51a00875c3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f616c6c79736f6e73696c76612f6c61726176656c2d6172746973616e2d646f6d61696e2d636f6e74657874732f5048504d442532302d2532305048502532304d6573732532304465746563746f723f6c6162656c3d5048504d44266c6f676f3d676974687562)](https://github.com/allysonsilva/laravel-artisan-domain-contexts/actions/workflows/phpmd.yml)[![PHPStan - GitHub Workflow Status](https://camo.githubusercontent.com/51b9c02f4797bb6d2586f7eac12acd4ceb3f55605f3af46c7f72edb7902403b4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f616c6c79736f6e73696c76612f6c61726176656c2d6172746973616e2d646f6d61696e2d636f6e74657874732f5048505374616e2532302d253230436f6465253230537461746963253230416e616c797369733f6c6162656c3d5048505374616e266c6f676f3d676974687562)](https://github.com/allysonsilva/laravel-artisan-domain-contexts/actions/workflows/phpstan.yml)[![Coverage Status](https://camo.githubusercontent.com/6fff8150aa96596daf9db9ade987fc6cdea6461c8fd2198be68dbdc71a71a449/68747470733a2f2f636f6465636f762e696f2f67682f616c6c79736f6e73696c76612f6c61726176656c2d6172746973616e2d646f6d61696e2d636f6e74657874732f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d4d58514f30484942464d)](https://codecov.io/gh/allysonsilva/laravel-artisan-domain-contexts)[![Code Quality/Consistency](https://camo.githubusercontent.com/562da8ae3e6d38257fb30f800e40c37ec3d0487c35269ff3679d80bd74f4004c/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3566326233613938646265653438616138653134626134613535363831663039)](https://www.codacy.com/gh/allysonsilva/laravel-artisan-domain-contexts/dashboard)[![Latest Version](https://camo.githubusercontent.com/fd563f55b8d6287514904db59d4f714754c7dfe17dd9bbb955bcfcb753f28902/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616c6c79736f6e73696c76612f6c61726176656c2d6172746973616e2d646f6d61696e2d636f6e74657874732e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/allysonsilva/laravel-artisan-domain-contexts)[![Total Downloads](https://camo.githubusercontent.com/898ae5532434128433564a7ab268d17beb9950bbcac0d291dfc7145092baa8af/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616c6c79736f6e73696c76612f6c61726176656c2d6172746973616e2d646f6d61696e2d636f6e74657874732e737667)](https://packagist.org/packages/allysonsilva/laravel-artisan-domain-contexts)[![MIT Licensed](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE.md)

Table of Contents
-----------------

[](#table-of-contents)

- [Overview](#overview)
    - [Concepts](#concepts)
        - [What is "Domain"?](#what-is-domain)
        - [What is "Contexts"?](#what-is-contexts)
    - [Questions](#questions)
        - [Should I use this package in the default Laravel framework?](#should-i-use-this-package-in-the-default-laravel-framework)
        - [When should I use this package?](#when-should-i-use-this-package)
- [🚀 Installation](#--installation)
    - [Requirements](#requirements)
    - [Laravel version Compatibility](#laravel-version-compatibility)
    - [Install the Package](#install-the-package)
    - [Publish the Config](#publish-the-config)
- [🔧 Configuration](#--configuration)
- [📖 Usage](#--usage)
    - [Understanding the `--context` option](#understanding-the---context-option)
        - [Example](#example)
    - [Understanding the `--context-namespace` option](#understanding-the---context-namespace-option)
    - [Understanding the `--all-contexts` option](#understanding-the---all-contexts-option)
    - [Understanding the `--only-default` option](#understanding-the---only-default-option)
    - [Understanding the `--multi-databases` option](#understanding-the---multi-databases-option)
    - [List of commands using contexts](#list-of-commands-using-contexts)
    - [🏗 `make` commands](#-make-commands)
        - [Examples](#examples)
    - [`migrate` commands](#migrate-commands)
        - [Understanding the behavior of `migrate:fresh` and `migrate:refresh`](#understanding-the-behavior-of-migratefresh-and-migraterefresh)
        - [📹 Demo `migrate:fresh`](#--demo-migratefresh)
        - [📹 Demo `migrate:refresh`](#--demo-migraterefresh)
        - [Understanding the behavior of `migrate:reset`, `migrate:rollback`, `migrate:status` and `migrate`](#understanding-the-behavior-of-migratereset-migraterollback-migratestatus-and-migrate)
        - [📹 Demo `migrate:reset`](#--demo-migratereset)
        - [📹 Demo `migrate:rollback`](#--demo-migraterollback)
    - [`db:seed` command](#dbseed-command)
        - [📹 Demo `db:seed`](#--demo-dbseed)
- [🧪 Testing](#--testing)
- [📝 Changelog](#--changelog)
- [🤝 Contributing](#--contributing)
- [🔒 Security](#--security)
- [🏆 Credits](#--credits)
- [License](#license)

Overview
--------

[](#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.

 [![](/images/demos/overview.gif?raw=true)](/images/demos/overview.gif?raw=true)

### Concepts

[](#concepts)

#### What is "Domain"?

[](#what-is-domain)

- The term "**Domain**" has nothing to do with URL or host ([www.example.com](http://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](https://stitcher.io/blog/organise-by-domain) for a better understanding!

#### What is "Contexts"?

[](#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](https://martinfowler.com/bliki/BoundedContext.html) for better understanding!

### Questions

[](#questions)

#### Should I use this package in the default Laravel framework?

[](#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-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
--------------

[](#--installation)

### Requirements

[](#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-version-compatibility)

LaravelPHPPackageMaintained9.x8.0**^2.0**✅8.708.0**^1.0**✅### Install the Package

[](#install-the-package)

You can install the package via Composer:

```
composer require allysonsilva/laravel-artisan-domain-contexts
```

### Publish the Config

[](#publish-the-config)

You can then publish the package's config file by using the following command:

```
php artisan vendor:publish --tag="context-config"
```

🔧 Configuration
---------------

[](#--configuration)

1. Create a folder, inside the `app` folder with the same name as the config of `config('context.folders.domain')`
2. 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;
    ```
3. 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')`.

📖 Usage
-------

[](#--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](#list-of-commands-using-contexts).

### Understanding the `--context` option

[](#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

[](#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

[](#understanding-the---context-namespace-option)

This option will only be used in the `make` commands, with that, see and [explanation about it](#make-commands).

### Understanding the `--all-contexts` option

[](#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](#list-of-commands-using-contexts).

It has the same behavior as the `--force` option.

### Understanding the `--only-default` 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

[](#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

[](#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)

> 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

[](#examples)

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:

```
