korstiaan/drimple

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

Installs: 32

Dependents: 1

Suggesters: 0

Security: 0

Stars: 8

Watchers: 1

Forks: 0

Open Issues: 0

Type:drupal-module

dev-master 2013-10-22 18:46 UTC

This package is not auto-updated.

Last update: 2024-12-21 15:13:46 UTC


README

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.