How To Register A Service Provider For Lumen ?
Weblog about making a service provider for lumen
Every bit part of my project for Outline I had to arrive talk to laravel/lumen in a simple way. I desire to write downwards what I did.
New Project
To make information technology available via composer, you have to kickoff with a composer file. Much like this one:
{
"name": "etiennemarais/outline-laravel",
"description": "Service provider for outline test generator",
"type": "library",
"license": "GPL-3.0",
"crave": {
"etiennemarais/outline": "~0.2"
},
"authors": [
{
"name": "Etienne Marais",
"email": "hello@etiennemarais.co.za"
}
],
"autoload": {
"psr-4": {
"OutlineLaravel\\": "src/OutlineLaravel/"
}
},
"minimum-stability": "dev"
}
I needed a unproblematic namespace and a require on meridian of the library I want to integrate into Lumen.
Then I created a folder construction like this:
The composer.lock file and vendor binder will get added as soon as you run composer install
What I needed
I needed some course of command to run when I needed updated tests from my api blueprint changes.
outline:regenerate
That seems squeamish. Let me show you how to brand the command that services that telephone call.
The Command
<?php
namespace OutlineLaravel\Commands; utilize DrafterPhp\Drafter;
use Illuminate\Panel\Command;
use Outline\ApiBlueprint\ApiBlueprint;
employ Outline\Test\Generator\Generator;
utilise Outline\Transformer\Outline\Transformer;
class Regenerate extends Command
{
protected $proper noun = 'outline:regenerate';
protected $clarification = 'Regenerate feature tests from the api blueprint specification';
private $apiBlueprintFile;
public role __construct()
{
parent::__construct();
$this->apiBlueprintFile = base_path() . '/apiary.apib';
}
public function handle()
{
$this->info("Regenerating feature tests from your apiary doc: \n{$this->apiBlueprintFile}");
$drafter = new Drafter(base_path() . '/vendor/bin/drafter');
$apiBlueprint = new ApiBlueprint($drafter, $this->apiBlueprintFile);
(new Generator(new Transformer))
->with($apiBlueprint)
->outputTo(base_path() . '/tests/Features')
->generateTestsFor('lumen');
}
}
Encounter how you set the proper noun of the command, also meet that the control runs via the handle() method. This leverages from the epic lumen control framework.
ServiceProvider (The glue)
The glue that sticks all of this together is called the service provider. This is how lumen knows to include your library and knows how to bind references to your commands/classes etc.
<?php
namespace OutlineLaravel; use Illuminate\Support\ServiceProvider;
class OutlineLaravelServiceProvider extends ServiceProvider
{
public office boot()
{
$this->app['outline:regenerate'] =
$this->app->share(function () {
return new Commands\Regenerate();
});
$this->commands(
'outline:regenerate'
);
}
/**
* Annals the service provider.
*
* @return void
*/
public function register()
{
// Goose egg to register
}
}
See how the command gets bound to a proper name
$this->app['outline:regenerate']
On boot, the framework will know to bind your custom command to that name and make information technology available for the cli listing of executable tasks.
Now, if you lot include this library as a composer dependency in your actual lumen app, the command will be available to run.
php artisan
Thanks for reading :)
How To Register A Service Provider For Lumen ?,
Source: https://medium.com/@etbal/blog-about-making-a-service-provider-for-lumen-561715a1c047
Posted by: clementsenty1997.blogspot.com
0 Response to "How To Register A Service Provider For Lumen ?"
Post a Comment