Drimple for Drupal 7.x
Module which adds a Dependency Injection Container using Pimple to Drupal., (*1)
, (*2)
Requirements
Installation
The recommended way to install Drimple
is with Composer.
Just add the following to your composer.json
:, (*3)
{
"minimum-stability": "dev",
"require": {
...
"korstiaan/drimple": "dev-master"
}
}
Now update composer and install the newly added requirement and its dependencies (including Pimple
):, (*4)
``` bash
$ php composer.phar update korstiaan/drimple, (*5)
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](http://voiture.hoppinger.com) just add `drimple` to `cnf/shared/modules.php`)
### Using Composer
Using `Composer` means including its autoloader. Add the following to your Drupals settings.php:
```php
// /path/to/sites/default/settings.php
require '/path/to/vendor/autoload.php';
Usage
Drimple
s container and its services can then be retrieved as singleton via drimple()
or \Drimple\Drimple::getInstance()
., (*6)
Adding services
Recommended way of adding services is by implementing hook_drimple_provide(\Drimple\Drimple $drimple)
:, (*7)
<?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
., (*8)
Example:, (*9)
<?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, (*10)
License
Drimple is licensed under the MIT license., (*11)