Wallogit.com
2017 © Pedro Peláez
Yet another dependency injection container for PHP5.4
Create or update your composer.json and run composer update, (*2)
``` json { "require": { "kzykhys/bowl": "1.0" } }, (*3)
Usage ----- ### Define parameters ``` php $bowl = new \Bowl\Bowl(); $bowl['lang'] = 'en'; $bowl['debug'] = true;
``` php $bowl = new \Bowl\Bowl();, (*4)
$bowl->share('service_name', function () { return new stdClass(); });, (*5)
var_dump($bowl->get('service_name') === $bowl->get('service_name')); // bool(true), (*6)
### Define a factory service ``` php $bowl = new \Bowl\Bowl(); $bowl->factory('service_name', function () { return new stdClass(); }); var_dump($bowl->get('service_name') === $bowl->get('service_name')); // bool(false)
``` php $bowl = new \Bowl\Bowl();, (*7)
$bowl->share('driver.mysql', function () { return new MysqlDriver(); });, (*8)
$bowl->share('connection', function () { $c = new Connection(); $c->setDriver($this->get('driver.mysql'));, (*9)
return $c;
});, (*10)
### Using tags to manage a collection of services ``` php $bowl = new \Bowl\Bowl(); $bowl->share('form.type.text', function () { return new TextType(); }, ['form.type']); $bowl->share('form.type.email', function () { return new EmailType(); }, ['form.type']); $bowl->share('form', function () { $form = new Form(); foreach ($this->getTaggedServices('form.type') as $service) { $form->addType($service); } return $form; });
``` php use Bowl\Bowl;, (*11)
$bowl = new Bowl();, (*12)
// Common parameters $bowl['lang'] = 'en';, (*13)
// Production configuration $bowl->configure('production', function (Bowl $bowl) { $bowl['debug'] = false;, (*14)
$bowl->share('orm.repository', function () {
return new EntityRepository();
});
});, (*15)
// Development configuration $bowl->configure('development', function (Bowl $bowl) { $bowl['debug'] = true;, (*16)
$bowl->share('orm.repository', function () {
return new MockRepository();
});
});, (*17)
// Common services $bowl->share('orm.manager', function () { return new OrmManager($this->get('orm.repository')); }); $bowl->share('fixture.loader', function () { return new Loader($this->get('orm.manager'), $this['debug']); });, (*18)
// Set enviroment manually $bowl->env('production');, (*19)
// Or using system's environment variable $bowl->env(getenv('APP_ENV') ? getenv('APP_ENV') : 'production');, (*20)
### Real life example ``` php <?php require __DIR__ . '/../vendor/autoload.php'; $bowl = new \Bowl\Bowl(); // Set a parameter $bowl['debug'] = false; // Shared service $bowl->share('ciconia.renderer', function () { return new \Ciconia\Renderer\HtmlRenderer(); }); // Tagged service $bowl->share('ciconia.extension.table', function () { return new \Ciconia\Extension\Gfm\TableExtension(); }, ['ciconia.extension']); // This example shows how to manage services using tags $bowl->share('ciconia', function () { $ciconia = new \Ciconia\Ciconia(); // $bowl is bind to this closure, so you can access $this as Bowl. if ($this['debug']) { $ciconia = new \Ciconia\Diagnose\Ciconia(); } // Resolve dependencies $ciconia->setRenderer($this->get('ciconia.renderer')); // All services tagged as "ciconia.extension" foreach ($this->getTaggedServices('ciconia.extension') as $extension) { $ciconia->addExtension($extension); } return $ciconia; }); // Get the object $ciconia = $bowl->get('ciconia'); echo $ciconia->render('Markdown is *awesome*'); // Create a new instance even if this is a shared object $ciconia = $bowl->reset('ciconia')->get('ciconia'); echo $ciconia->render('Markdown is *awesome*');
You can configure Bowl based on environment flags such as production and development., (*21)
``` php $bowl = new \Bowl\Bowl(); $bowl->configure('prod', function (\Bowl\Bowl $bowl) { $bowl['debug'] = false; });, (*22)
#### env(_string_ **$environment**) You have to call `env()` to apply one of environments. ``` php $bowl = new \Bowl\Bowl(); $bowl->configure('prod', function (\Bowl\Bowl $bowl) { $bowl['debug'] = false; }); $bowl->env('prod');
Register a shared service, (*23)
``` php $bowl = new \Bowl\Bowl(); $bowl->share('logger', function () { return new Logger(); });, (*24)
$bowl->get('logger')->log($message);, (*25)
#### factory(_string_ **$name**, _\Closure_ **$closure**, \[_array_ **$tags**\]) Register a factory service ``` php $bowl = new \Bowl\Bowl(); $bowl->share('date.now', function () { return new \DateTime('now'); }); $bowl->get('date.now')->format('r');
Extend a service definition, (*26)
``` php $bowl = new \Bowl\Bowl(); $bowl->share('logger', function () { return new Logger(); });, (*27)
$bowl->extend('logger', function (LoggerInterface $logger) { $logger->setPath(DIR.'/../app/logs');, (*28)
return $logger;
});, (*29)
$bowl->get('logger')->log($message);, (*30)
#### get(_string_ **$name**) Get an object ``` php $bowl = new \Bowl\Bowl(); $bowl['debug'] = true; $bowl->factory('filesystem', function () { return new Filesystem(); }); $bowl->share('logger', function () { if ($this['debug']) { return new ConsoleLogger(); } else { return new FilesystemLogger($this->get('filesystem')); } }); $logger = $bowl->get('logger');
Get services having a tag, (*31)
``` php $bowl = new \Bowl\Bowl(); $bowl->share('transport.smtp', function () { return new SmtpTransport(); }, ['email.transport']); $bowl->share('transport.sendmail', function () { return new SendmailTransport(); }, ['email.transport']); $bowl->share('mailer', function () { $mailer = new Mailer(); foreach ($this->getTaggedServices('email.transport') as $service) { $mailer->addTransport($service); }, (*32)
return $mailer;
});, (*33)
$bowl->get('mailer')->send($mimeMessage);, (*34)
#### reset(_string_ **$name**) Re-instantiate the object, even if the service is shared object. **This is unsafe operation** ``` php $bowl = new \Bowl\Bowl(); $bowl->share('registry', function () { return new Registry(); }); try { $bowl->get('registry')->getManager()->flush(); } catch (\Exception $e) { $bowl->reset('registry'); }
Feel free to fork and send a pull request., (*35)
The MIT License, (*36)
Kazuyuki Hayashi (@kzykhys), (*37)