Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Module which adds a Dependency Injection Container using Pimple to Drupal 7.x.
branch: tests

This branch is 9 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
module
tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
composer.lock
phpunit.xml.dist

README.md

Drimple for Drupal 7.x

Module which adds a Dependency Injection Container using Pimple (https://github.com/fabpot/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:

   {
       "require": {
           ...
           "korstiaan/drimple": "*"
       }
   }

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

$ php composer.phar update korstiaan/drimple

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 enabling its autoloader. This can be done in 2 days:

  1. Require autoload.php in settings.php (recommended)

Add the following to your Drupals settings.php:

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

require '/path/to/vendor/autoload.php';
  1. Use composer_loader

Just follow its readme.

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']);
        });

    }
}

License

Drimple is licensed under the MIT license.

Something went wrong with that request. Please try again.