Blog about making a service provider for lumen

As part of my project for Outline I had to make it talk to laravel/lumen in a simple way. I want to write down what I did.

New Project

To make it available via composer, you have to start 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",
"require": {
"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 simple namespace and a require on top of the library I want to integrate into Lumen.

Then I created a folder structure like this:

The composer.lock file and vendor folder will get added as soon as you run composer install

What I needed

I needed some form of command to run when I needed updated tests from my api blueprint changes.

outline:regenerate

That seems nice. Let me show you how to make the command that services that call.

The Command

<?php
namespace
OutlineLaravel\Commands;

use DrafterPhp\Drafter;
use Illuminate\Console\Command;
use Outline\ApiBlueprint\ApiBlueprint;
use Outline\Test\Generator\Generator;
use Outline\Transformer\Outline\Transformer;

class Regenerate extends Command
{
protected $name = 'outline:regenerate';
protected $description = 'Regenerate feature tests from the api blueprint specification';
private $apiBlueprintFile;

public function __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');
}
}
See how you set the name of the command, also see that the command runs via the handle() method. This leverages from the epic lumen command 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 function boot()
{
$this->app['outline:regenerate'] =
$this->app->share(function () {
return new Commands\Regenerate();
});

$this->commands(
'outline:regenerate'
);
}

/**
* Register the service provider.
*
*
@return void
*/
public function register()
{
// Nothing to register
}
}
See how the command gets bound to a name
$this->app['outline:regenerate']

On boot, the framework will know to bind your custom command to that name and make it available for the cli list of executable tasks.

Now, if you include this library as a composer dependency in your actual lumen app, the command will be available to run.

php artisan

Thanks for reading :)

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.