Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
137 lines (100 sloc) 3.39 KB

Drimple for Drupal 7.x

Module which adds a Dependency Injection Container using Pimple to Drupal.

Build Status

Requirements

  • Drupal 7.x
  • PHP 5.3.3+
  • Pimple

Installation

The recommended way to install Drimple is with Composer. Just add the following to your composer.json:

   {
       "minimum-stability": "dev",
       "require": {
           ...
           "korstiaan/drimple": "dev-master"
       }
   }

Now update composer and install the newly added requirement and its dependencies (including Pimple):

$ php composer.phar update korstiaan/drimple

If all went well and composer/installers did its job, Drimple was installed to modules/drimple. If you don't want it there, or it's not part of your Drupal rootdir, symlink it to your folder of choice.

Next go to site/all/modules and enable it on http://yourdomain.com/admin/modules/list.

(If you're using voiture just add drimple to cnf/shared/modules.php)

Using Composer

Using Composer means including its autoloader. Add the following to your Drupals settings.php:

// /path/to/sites/default/settings.php

require '/path/to/vendor/autoload.php';

Usage

Drimples container and its services can then be retrieved as singleton via drimple() or \Drimple\Drimple::getInstance().

Adding services

Recommended way of adding services is by implementing hook_drimple_provide(\Drimple\Drimple $drimple):

<?php
// sites/all/modules/foo/foo.module

function foo_drimple_provide(\Drimple\Drimple $drimple) 
{
    $drimple['database']            = $drimple->share(function($c) {
        $options = $c['database.options'] + array(
            'user'      => null,
            'password'  => null,
        );
        if (!isset($options['dsn'])) {
            throw new \Exception('Please provide dsn');
        }

        return new \PDO($options['dsn'],$options['user'],$options['password']);
    });
    $drimple['database.options']    = array(
        'dsn'       => 'mysql:dbname=drupal;host=localhost',
        'user'      => 'root',
        'password'  => 'root',
    ); 
}

Service providers

Just like Silex you can also add services to Drimple by registering Service Providers.

Example:

<?php
// sites/all/modules/foo/foo.module

function foo_drimple_provide(\Drimple\Drimple $drimple) 
{
    $drimple->register(new \Foo\Provider\DBProvider(), array(
        'database.options'  => array(
            'dsn'       => 'mysql:dbname=drupal;host=localhost',
            'user'      => 'root',
            'password'  => 'root',
        ),
    ));
}

// sites/all/modules/foo/Foo/Provider/DBProvider.php
namespace Foo\Provider;

use Drimple\Drimple,
    Drimple\Provider\ServiceProviderInterface; 

class DBProvider implements ServiceProviderInterface
{
    public function register(Drimple $drimple)
    {
        $drimple['database'] = $drimple->share(function($c) {
            $options = $c['database.options'] + array(
                'user'      => null,
                'password'  => null,
            );
            if (!isset($options['dsn'])) {
                throw new \Exception('Please provide dsn');
            }
            return new \PDO($options['dsn'],$options['user'],$options['password']);
        });

    }
}

Providers

See the wiki

License

Drimple is licensed under the MIT license.