Posted in Code on August 3rd, 2014

BinderClip: Simple Laravel Bindings Registration

Looking for a simple and handy (yet useful) code snippet to test out Jekyll's code blocks and syntax highlighting, I decided I'd package up this simple Laravel Service Provider which I use (typically in smaller projects) to handle registering all of my project's application bindings.

BinderClip This simple Service Provider (which I've decided to call BinderClip) loads the bindings.php configuration file (which must be added to your project's app/config directory), parses the configured interface/implementation bindings and registers them with Laravel's IoC Container.

BinderClip's Service Provider looks like this:


# /src/Coderabbi/BinderClip/BindingConfigurationServiceProvider.php

/**
 * @author Yitzchok Willroth (@coderabbi) 
 * @copyright Yitzchok Willroth (@coderabbi) 
 * @license MIT 
 *
 * @package  Coderabbi\BinderClip
 */

namespace Coderabbi\BinderClip;


use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Config;


/**
 * Class BindingConfigurationServiceProvider
 *
 * @package Coderabbi\BinderClip
 *
 * @final
 */
final class BindingConfigurationServiceProvider
    extends ServiceProvider
{

    /**
     * Configuration String
     */
    const CONFIG_STRING = 'bindings';


    /**
     * Service Provider Registration
     *
     * @access public
     */
    public function register()
    {
        if ( ! empty(Config::get(self::CONFIG_STRING)))
        {
            $this->registerBindingGroups(Config::get(self::CONFIG_STRING));
        }
    }


    /**
     * Register Application Binding Groups
     *
     * @access private
     * @param array $groups
     */
    private function registerBindingGroups(array $groups)
    {
        foreach ($groups as $bindings)
        {
            $this->registerBindings($bindings);
        }
    }


   /**
    * Register Application Bindings
    *
    * @access private
    * @param array $bindings
    */
    private function registerBindings(array $bindings)
    {
        foreach ($bindings as $interface => $implementation)
        {
            $this->app->bind($interface, $implementation);
        }
    }

}

To add application bindings to your project, simply add an array of interface/implementation pairs to the bindings.php configuration array, like so:


# /app/config/bindings.php

return [

    'bindings' => [
        'Acme/Blog/User/UserRepository'
            => 'Acme/Blog/User/EloquentUserRepository',
        'Acme/Blog/Post/PostRepository'
            => 'Acme/Blog/Post/EloquentPostRepository',
        'Acme/Blog/Comment/CommentRepository'
            => 'Acme/Blog/Comment/EloquentCommentRepository',
        'Acme/Blog/User/UserService'
            => 'Acme/Blog/User/IlluminateUserService',
        'Acme/Blog/Post/PostService'
            => 'Acme/Blog/Post/IlluminatePostService',
        'Acme/Blog/Comment/CommentService'
            => 'Acme/Blog/Comment/IlluminateCommentService',
    ]

];

For larger projects, you might instead organize your application bindings into binding groups, like so:


# /app/config/bindings.php

return [

    'repositories' => [
        'Acme/Blog/User/UserRepository'
            => 'Acme/Blog/User/EloquentUserRepository',
        'Acme/Blog/Post/PostRepository'
            => 'Acme/Blog/Post/EloquentPostRepository',
        'Acme/Blog/Comment/CommentRepository'
            => 'Acme/Blog/Comment/EloquentCommentRepository',
    ],

    'services' => [
        'Acme/Blog/User/UserService'
            => 'Acme/Blog/User/IlluminateUserService',
        'Acme/Blog/Post/PostService'
            => 'Acme/Blog/Post/IlluminatePostService',
        'Acme/Blog/Comment/CommentService'
            => 'Acme/Blog/Comment/IlluminateCommentService',
    ]

];

Finally, for more complex projects you might prefer to organize your application bindings by domain, like so:


# /app/config/bindings.php

return [

    'users' => [
        'Acme/Blog/User/UserRepository'
            => 'Acme/Blog/User/EloquentUserRepository',
        'Acme/Blog/User/UserService'
            => 'Acme/Blog/User/IlluminateUserService',
    ],

    'posts' => [
        'Acme/Blog/Post/PostRepository'
            => 'Acme/Blog/Post/EloquentPostRepository',
        'Acme/Blog/Post/PostService'
            => 'Acme/Blog/Post/IlluminatePostService',
    ],

    'comments' => [
        'Acme/Blog/Comment/CommentRepository'
            => 'Acme/Blog/Comment/EloquentCommentRepository',
        'Acme/Blog/Comment/CommentService'
            => 'Acme/Blog/Comment/IlluminateCommentService',
    ]

];

No matter which way you choose to organize your project's application bindings within the configuration file, there's no need to compose additional Service Providers as BinderClip's BindingConfigurationServiceProvider will register all of your application bindings for you from the configuration file automatically!

While this approach may not be appropriate for all applications (I limit its use to prototypes & simple apps myself), it certainly speeds up development when it's use is justified!

You may find BinderClip on Github.



Applies to:
  • Laravel 4.2.*
  • PHP 5.5.*