|
30 | 30 | - [Xdebug CLI Usage](#xdebug-cli-usage)
|
31 | 31 | - [Xdebug Browser Usage](#xdebug-browser-usage)
|
32 | 32 | - [Customization](#sail-customization)
|
| 33 | +- [Sail Plugins](#sail-plugins) |
33 | 34 |
|
34 | 35 | <a name="introduction"></a>
|
35 | 36 | ## Introduction
|
@@ -568,3 +569,112 @@ After running this command, the Dockerfiles and other configuration files used b
|
568 | 569 | ```shell
|
569 | 570 | sail build --no-cache
|
570 | 571 | ```
|
| 572 | + |
| 573 | +<a name="sail-plugins"></a> |
| 574 | +## Sail Plugins |
| 575 | + |
| 576 | +Laravel Sail supports third-party plugins for customizing its Docker environment. Plugins may define new services or override Sail’s built-in services within the `docker-compose.yml` file. Custom service configurations, environment variable modifications, application container overrides, and additional logic for installation or publishing may be included. |
| 577 | + |
| 578 | +<a name="sail-plugins-overview"></a> |
| 579 | +### Overview |
| 580 | + |
| 581 | +Plugins customize Sail’s default Docker configuration. New services may be added, built-in services like `mysql` or `redis` modified. |
| 582 | + |
| 583 | +<a name="sail-plugins-registering"></a> |
| 584 | +### Registering a Plugin |
| 585 | + |
| 586 | +You may register custom services within the service provider of your plugin package: |
| 587 | + |
| 588 | +```php |
| 589 | +namespace Vendor\CustomPlugin; |
| 590 | +
|
| 591 | +use Laravel\Sail\Sail; |
| 592 | +use Illuminate\Support\ServiceProvider; |
| 593 | +use Illuminate\Console\Command; |
| 594 | +
|
| 595 | +class YourSailPluginProvider extends ServiceProvider |
| 596 | +{ |
| 597 | + public function boot() |
| 598 | + { |
| 599 | + Sail::addService( |
| 600 | + 'custom-service', |
| 601 | + __DIR__ . '/../stubs/custom-service.stub', |
| 602 | + isPersistent: true, |
| 603 | + isDependency: true, |
| 604 | + env: [ |
| 605 | + 'CUSTOM_SERVICE_FOO' => 'bar', |
| 606 | + ], |
| 607 | + preInstallCallback: function (Command $command, array $services, string $appService) { |
| 608 | + $command->info('The PHP app will run in '.$appService.' service.'); |
| 609 | + $command->info('Following services have also been installed: '.implode($services, ', ')); |
| 610 | + }, |
| 611 | + )->addService( |
| 612 | + 'redis', |
| 613 | + env: function (string $environment): string { |
| 614 | + $commentedEnv = '# REDIS_HOST=127.0.0.1'; |
| 615 | + $environment = str_replace($commentedEnv, substr($commentedEnv, 2), $environment); |
| 616 | +
|
| 617 | + return str_replace('REDIS_HOST=127.0.0.1', "REDIS_HOST=redis", $environment); |
| 618 | + }, |
| 619 | + default: true, |
| 620 | + )->setBaseTemplate('path-to-app-service.stub'); |
| 621 | + } |
| 622 | +} |
| 623 | +``` |
| 624 | + |
| 625 | +This example defines a `custom-service` using a service configuration located at `stubs/custom-service.stub`. The service is configured to persist data and depend on the application container. The `env` parameter updates the `.env` file with relevant variables, while the `preInstallCallback` closure executes additional logic before the services are built. |
| 626 | +It also overrides the built-in `redis` service, marking it as a default service. |
| 627 | +Finally, `setBaseTemplate` replaces the built-in docker-compose template. |
| 628 | + |
| 629 | +> [!NOTE] |
| 630 | +> If multiple plugins override the same service, the last registered configuration takes precedence in the `docker-compose.yml` file. Check existing services with the `availableServices` method. |
| 631 | + |
| 632 | +> [!NOTE] |
| 633 | +> Sail will look for the {{APP}} placeholder in the template to identify the app service. |
| 634 | + |
| 635 | +<a name="sail-plugins-stub"></a> |
| 636 | +### Creating a Docker Compose Service Configuration |
| 637 | + |
| 638 | +A plugin requires a Docker Compose service configuration to define new services or override built-in services. An example is shown below: |
| 639 | + |
| 640 | +```yaml |
| 641 | +custom-service: |
| 642 | + image: custom/service:latest |
| 643 | + ports: |
| 644 | + - "8080:8080" |
| 645 | + volumes: |
| 646 | + - custom-service-data:/data |
| 647 | + networks: |
| 648 | + - sail |
| 649 | + depends_on: |
| 650 | + - {{APP}} |
| 651 | +``` |
| 652 | + |
| 653 | +This service configuration should be placed within your plugin’s package, such as in a `stubs` directory. |
| 654 | + |
| 655 | +Sail resolves `{{APP}}` to the active application container name (typically `laravel.test`) during configuration. |
| 656 | + |
| 657 | +<a name="sail-plugins-callbacks"></a> |
| 658 | +### Executing additional logic |
| 659 | + |
| 660 | +Plugins may register additional logic that executes during the `sail:install` or `sail:publish` command: |
| 661 | + |
| 662 | +```php |
| 663 | +namespace Vendor\CustomPlugin; |
| 664 | +
|
| 665 | +use Laravel\Sail\Sail; |
| 666 | +use Illuminate\Support\ServiceProvider; |
| 667 | +use Illuminate\Console\Command; |
| 668 | +
|
| 669 | +class YourSailPluginProvider extends ServiceProvider |
| 670 | +{ |
| 671 | + public function boot() |
| 672 | + { |
| 673 | + Sail::addPreInstallCallback(function (Command $command, array $services, string $appService) { |
| 674 | + $command->info('A docker-compose.yaml file with '.$appService.' and the following services has been created: '.implode($services, ', ')); |
| 675 | + })->addPostPublishCallback(function (Command $command) { |
| 676 | + $command->info('Resources have been published. Update docker-compose.yml paths accordingly.'); |
| 677 | + }); |
| 678 | + } |
| 679 | +} |
| 680 | +``` |
0 commit comments