jake142 / laravel-service
A package to split a laravel project into smaller services
Requires
None
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A package to divide Laravel into smaller services. Then main goal of this package is:
- Readable, the code is split into services
- Scalable, run services as needed
- Faster development, split work between developers
Installation
Simply run:
composer require jake142/laravel-service
Usage
Start by running
php artisan laravel-service:make
This will start a setup wizard where you define:
- The name of the service
- The version of the service (eg. V1)
- If you want a sample controller (recommended)
- If you want a sample job (recommended)
- If you want a sample test (recommended)
The service files are created under Services/{version}/{name}/ and a path repository is added to your root composer.json. The service is not enabled yet. php artisan laravel-service:list shows it as DISABLED until you enable it.
Enable a service
To enable a service run:
php artisan laravel-service:enable laravel-service/$version-$service (eg. laravel-service/v1-sampleservice)
Use the lowercase package name (laravel-service/v1-sampleservice), not the folder path.
This will enable the service which means:
- Registers the path repository in your root
composer.jsonif it is missing (for example after merging a branch) - Composer runs
requirewith@devon your service - The tests will be added to the phpunit.xml and therefore able to run with the phpunit command
Working across git branches
make still writes a path repo into root composer.json so list can show DISABLED. That file is shared across checkouts. Services/ is not. After a branch switch Composer can point at a folder that does not exist.
Run this after every git checkout / git switch:
php artisan laravel-service:sync
That command:
- Adds path repos for services that exist under
Services/on this branch (listshows them as DISABLED until you enable) - Removes path repos (and
require/ phpunit entries) for services whose folders are missing on this branch - Rewrites remaining
laravel-service/*constraints to@dev(path packages resolve asdev-main,dev-sandbox, …) - Updates
composer.lockfor those packages
Typical flow:
- On a feature branch:
php artisan laravel-service:make→listshows DISABLED →php artisan laravel-service:enable laravel-service/v1-myservicewhen you need it. - Commit
Services/...together with thecomposer.jsonpath-repo line when the service belongs on that branch. - Merge into the target branch. If the repo line never landed there,
enableorsyncregisters it from disk. - After switching branches:
php artisan laravel-service:sync.
Disable a service
To disable a service run:
php artisan laravel-service:disable laravel-service/$version-$service (eg. laravel-service/v1-sampleservice)
This will disable the service which means:
- Composer runs remove on your service
- The tests will be removed from the phpunit.xml
List services and their status
php artisan laravel-service:list
Generic Queues and Jobs
To call the generic queue:
(new GenericQueue('Services\\V1\\Test\\Jobs\\ExampleJob', ['param'=>'test'], $queue = null))->dispatch();
To run the generic job:
<?php namespace Services\V1\Test\Jobs; use Jake142\Service\Queue\Jobs\Generic as GenericJob; /** * An example job */ class ExampleJob extends GenericJob { /** * Execute the job. * * @return void */ public function handle($payload) { try { //Your code goes here... Log::info($payload['param']); } catch(\Exception $e) { throw $e; } } }
Config
You can either use vendor:publish to publish the config files for each service or you can publish the laravel-service config file which will get updated when you add new configs to a service. If you use the laravel-service config file you access the config values as following:
config('laravel-service.<version>.<service>.<config_file>.<value>');
Release note
Version 3.1.2
laravel-service:make registers the path repo again. After make, laravel-service:list shows the service as DISABLED. enable still registers from disk if the repo line is missing after a merge.
Branch-safe laravel-service:sync: After git checkout, sync aligns path repos with Services/ on the current branch (add present, remove missing, drop require/phpunit for services not on this branch) and re-resolves remaining packages with @dev.
Removed OpenAPI/Swagger. laravel-service:generate-docs, --workaround-readme and the zircote/swagger-php dependency are gone.
Version 3.1.1
Branch-safe enable/disable: enable and disable now use @dev constraints and --ignore-platform-reqs, so path packages work regardless of git branch name and dev containers missing optional PHP extensions.
laravel-service:sync: New command to normalize all laravel-service/* constraints to @dev and re-resolve path packages after switching git branches.
Version 3.1.0
Workflow change: laravel-service:make no longer adds a path repository to your root composer.json. Registration happens when you run laravel-service:enable. This makes it safer to create services on one git branch and enable them on another after merge.
laravel-service:enable: If the service folder exists under Services/ but is missing from composer.json, it is registered automatically (matched by package name, e.g. laravel-service/v1-myservice).
Bug fixes:
- Skipping the sample controller now creates
routes/api.phpandroutes/web.php(previously wrote uselessapi_routes.stub/web_routes.stubfiles that were never loaded). - Generated tests use the
Testsnamespace segment to match thetests/directory. - Aggregated config no longer errors when the
Services/directory does not exist yet.
Unchanged for existing projects: Services already listed in composer.json repositories behave as before. You do not need to re-run make.
Version 3.0.6 -> Version 3.0.9 Updated swagger dependency.
Version 3.0.5 Removed dev-master from composer install.
Version 3.0.4 Made changes to the generic queues.
Version 3.0.3 Special support for readme.com to support swagger features.
Version 3.0.2 Enabled all services for open api doc generation.
Version 3.0.1 Added constants to open api doc generation.
Version 3.0.0 Now with open api 3.0! Read more below!
Version 0.2.9 Fixed bug in GenericQueue where job was not deleted from queue when processed.
Version 0.2.8 Fixed the bug in 0.2.7 whereas $this->findComposer() is an array in laravel 5.8 and above and a string in laravel 5.7 and below.
Version 0.2.7 Override the deprecated function getProcess of the Composer class to support laravel 5.8 and above.
Version 0.2.6 the dev-master param was added to the composer require command run when you enable a service.
Version 0.2.5 Fixed broken update package command. Now you should be able to run
php artisan laravel-service:update-package
If you have been using version 0.2.2 -> 0.2.4 you will have to fix your composer files and phpunit file manually. Sorry!
Note: If you have dependencies between your services (eg. common, core services etc) you should manually disable all service before running the laravel-service:update-package command.
Version 0.2.3 service:make command renamed to laravel-service:make.
Version 0.2.2 Due to Composer 2.x more strict naming rules this package has new naming of the services. I have created an artisan upgrade command to make the changes for you. The command will disable all your services and you have to manually enable them when you have run the command.
php artisan laravel-service:update-package
I have also updated all commands with the prefix laravel-service (instead of service as before)
Version 0.2.1 enabled automatic update of service configs. More info below.
Version 0.2.0 includes generic jobs and queues. This is needed so one service can push data on to a queue without knowing about the code in another service job.
Version 0.1.X is a complete rewrite of the package. Version 0.1.X now creates each service as a composer package and uses composer to add it to your project. It is tested in laravel version 5.6. Previous versions are not supported anymore.
PLEASE NOTE
When you enable a service, laravel-service will add (if not already present):
"minimum-stability": "dev", "prefer-stable": true
to your composer.json file, along with the path repository for that service.