In almost every Laravel package, there is a ServiceProvider class responsible for registering bindings package resources such as config files, views, migrations, …

While preparing a workshop that I gave on package development, I watched some videos of our own Laravel package training video course again. While watching the videos on using the service provider again, I had the idea to simplify how resources can be registered.

Meanwhile, I’ve fleshed out the idea and release it as a new package called laravel-package-tools. In this blog post, I’d like to introduce the package to you.

Introducing the PackageServiceProvider

Let’s look at a service provider that registers a config file, migrations, views, translations, and a command. It uses several functions that are explained in Laravel’s package development docs.

<span class="hljs-keyword">namespace</span> <span class="hljs-title">YourVendor</span><span class="hljs-title">YourAwesomePackage</span>;
<p><span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span><span class="hljs-title">Support<span class="hljs-title">ServiceProvider;
<span class="hljs-keyword">use</span> <span class="hljs-title">YourVendor</span><span class="hljs-title">YourAwesomePackage<span class="hljs-title">Commands<span class="hljs-title">YourAwesomeCommand;</p>
<p><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">YourAwesomePackageServiceProvider</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ServiceProvider</span>
</span>{
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">boot</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">$this</span>->app->runningInConsole()) {
<span class="hljs-keyword">$this</span>->publishes([
<span class="hljs-keyword"><strong>DIR</strong></span> . <span class="hljs-string">'/../config/laravel-awesome-package.php'</span> => config_path(<span class="hljs-string">'laravel-awesome-package.php'</span>),
], <span class="hljs-string">'config'</span>);</p>
<pre><code>        <span class="hljs-keyword">$this</span>-&gt;publishes([
            <span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../resources/views'</span> =&gt; base_path(<span class="hljs-string">'resources/views/vendor/laravel-awesome-package'</span>),
        ], <span class="hljs-string">'views'</span>);

        $migrationFileName = <span class="hljs-string">'create_package_table.php'</span>;
        <span class="hljs-keyword">if</span> (! <span class="hljs-keyword">$this</span>-&gt;migrationFileExists($migrationFileName)) {
            <span class="hljs-keyword">$this</span>-&gt;publishes([
                <span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">"/../database/migrations/{$migrationFileName}.stub"</span> =&gt; database_path(<span class="hljs-string">'migrations/'</span> . date(<span class="hljs-string">'Y_m_d_His'</span>, time()) . <span class="hljs-string">'_'</span> . $migrationFileName),
            ], <span class="hljs-string">'migrations'</span>);
        }

        <span class="hljs-keyword">$this</span>-&gt;commands([
            YourAwesomeCommand::class,
        ]);
    }

    <span class="hljs-keyword">$this</span>-&gt;loadViewsFrom(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../resources/views'</span>, <span class="hljs-string">'laravel-awesome-package'</span>);
}

<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">register</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">$this</span>-&gt;mergeConfigFrom(<span class="hljs-keyword">__DIR__</span> . <span class="hljs-string">'/../config/laravel-awesome-package.php'</span>, <span class="hljs-string">'laravel-awesome-package'</span>);
}

<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">migrationFileExists</span><span class="hljs-params">(string $migrationFileName)</span>: <span class="hljs-title">bool</span>
</span>{
    $len = strlen($migrationFileName);
    <span class="hljs-keyword">foreach</span> (glob(database_path(<span class="hljs-string">"migrations/*.php"</span>)) <span class="hljs-keyword">as</span> $filename) {
        <span class="hljs-keyword">if</span> ((substr($filename, -$len) === $migrationFileName)) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        }
    }

    <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
}

}

Our laravel-package-tools package contains a PackageServiceProvider class .

Let extend that PackageServiceProvider to vastly simplify the code above. The class below is equivalent to class in the previous code snippet.

<span class="hljs-keyword">namespace</span> <span class="hljs-title">YourVendor</span><span class="hljs-title">YourAwesomePackage;
<p><span class="hljs-keyword">use</span> <span class="hljs-title">OriginalVendor</span><span class="hljs-title">LaravelPackageTools<span class="hljs-title">Package;
<span class="hljs-keyword">use</span> <span class="hljs-title">OriginalVendor</span><span class="hljs-title">LaravelPackageTools<span class="hljs-title">PackageServiceProvider;
<span class="hljs-keyword">use</span> <span class="hljs-title">YourVendor</span><span class="hljs-title">YourAwesomePackage<span class="hljs-title">Commands<span class="hljs-title">YourAwesomeCommand;</p>
<p><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SkeletonServiceProvider</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">PackageServiceProvider</span>
</span>{
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">configurePackage</span><span class="hljs-params">(Package $package)</span>: <span class="hljs-title">void</span>
</span>{
$package
->name(<span class="hljs-string">'your-awesome-package'</span>)
->hasConfigFile()
->hasViews()
->hasMigration(<span class="hljs-string">'create_package_table'</span>)
->hasCommand(YourAwesomeCommand::class);
}
}
</p>

That is way cleaner, right? I’ve already updated some of our packages to make use of our PackageServiceProvider. Here’s the service provider from laravel-backup.

<span class="hljs-keyword">namespace</span> <span class="hljs-title">Spatie</span><span class="hljs-title">Backup;
<p><span class="hljs-comment">// import statements omitted for brevity</span></p>
<p><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BackupServiceProvider</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">PackageServiceProvider</span>
</span>{
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">configurePackage</span><span class="hljs-params">(Package $package)</span>: <span class="hljs-title">void</span>
</span>{
$package
->name(<span class="hljs-string">'laravel-backup'</span>)
->hasConfigFile()
->hasTranslations()
->hasCommands([
BackupCommand::class,
CleanupCommand::class,
ListCommand::class,
MonitorCommand::class,
]);
}</p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">packageRegistered</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">$this</span>-&gt;app[<span class="hljs-string">'events'</span>]-&gt;subscribe(EventHandler::class);

    <span class="hljs-keyword">$this</span>-&gt;app-&gt;singleton(ConsoleOutput::class);

    <span class="hljs-keyword">$this</span>-&gt;app-&gt;bind(CleanupStrategy::class, config(<span class="hljs-string">'backup.cleanup.strategy'</span>));
}

}

In closing

To know more about the PackageServiceProvider header over to the readme of laravel-package-tools on GitHub.

I’ve not PRed these changes to Laravel because the functions provided by PackageServiceProvider assume an opinionated package structure.

To kickstart your next Laravel package, you could consider using our Laravel package skeleton, which already uses the shiny new PackageServiceProvider out of the box. Here’s a video that explains how to use the Laravel package skeleton (spoiler: it’s very easy).

Be sure to take a look at this list of Laravel and PHP packages our team has released previously. I’m pretty sure there’s something there for your next project.

Categories: PHP