2017 © Pedro Peláez
 

library bowl

Yet another dependency injection container for PHP5.4

image

kzykhys/bowl

Yet another dependency injection container for PHP5.4

  • Saturday, December 7, 2013
  • by kzykhys
  • Repository
  • 3 Watchers
  • 8 Stars
  • 74 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

Bowl, Yet Another Dependency Injection Container (PHP5.4+)

Latest Stable Version Build Status Coverage Status SensioLabsInsight, (*1)

  • Manage multiple environment (production/development/test ...)
  • Manage dependencies between objects
  • Perform a Lazy instantiation
  • Perform a Factory pattern
  • No external files to configure dependencies
  • You can avoid Singleton/Factory pattern from your classes

Requirements

  • PHP5.4+

Installation

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;

Define a shared service

``` 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)

Define a service depending on other services

``` 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; });

Working with environment flag

``` 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*');

API

Manage environment

configure(string $environment, \Closure $closure)

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

Service container

share(string $name, \Closure $closure, [_array_ $tags])

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(string $name, \Closure $closure)

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

getTaggedServices(string $name)

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

Contributing

Feel free to fork and send a pull request., (*35)

License

The MIT License, (*36)

Author

Kazuyuki Hayashi (@kzykhys), (*37)

The Versions

07/12 2013

dev-master

9999999-dev

Yet another dependency injection container for PHP5.4

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Kazuyuki Hayashi

container dic dependency di injection

19/11 2013

v1.0.0

1.0.0.0

Yet another dependency injection container for PHP5.4

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Kazuyuki Hayashi

container dic dependency di injection