Etienne Marais in Php 8 minutes

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 a package available via composer, you have to start with a composer.json 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": ""
    "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:

Namespace Folder

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 my API blueprint changes to update my generated tests.


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

Creating the command

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()
        $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))
            ->outputTo(base_path() . '/tests/Features')

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/laravel knows to include your library and knows how to bind references to your commands/classes etc.

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();


     * Register the service provider.
     * @return void
    public function register()
        // Nothing to register

See how the command gets bound to a name


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

Namespace Folder