PHPackages                             kzykhys/bowl - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. kzykhys/bowl

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

kzykhys/bowl
============

Yet another dependency injection container for PHP5.4

v1.0.0(12y ago)810311MITPHPPHP &gt;=5.4

Since Nov 19Pushed 12y ago3 watchersCompare

[ Source](https://github.com/kzykhys/Bowl)[ Packagist](https://packagist.org/packages/kzykhys/bowl)[ RSS](/packages/kzykhys-bowl/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (1)DependenciesVersions (2)Used By (1)

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

[](#bowl-yet-another-dependency-injection-container-php54)

[![Latest Stable Version](https://camo.githubusercontent.com/844a19b2fc64a9fa58235b952b4d0069bd2762fab17dd7b2f22fc7d4731db4cb/68747470733a2f2f706f7365722e707567782e6f72672f6b7a796b6879732f626f776c2f762f737461626c652e706e67)](https://packagist.org/packages/kzykhys/bowl)[![Build Status](https://camo.githubusercontent.com/f963b3013b0f228e7c9059e3976a529fc217de33ed62bacf9c603d6e34bd9178/68747470733a2f2f7472617669732d63692e6f72672f6b7a796b6879732f426f776c2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/kzykhys/Bowl)[![Coverage Status](https://camo.githubusercontent.com/4564817d15b3519ec478ea17e214ef96beea9810d3475200049a3b4b54bb5596/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6b7a796b6879732f426f776c2f62616467652e706e67)](https://coveralls.io/r/kzykhys/Bowl)[![SensioLabsInsight](https://camo.githubusercontent.com/1db7f807c38f535b44fb1d3f3cfc69786d0b69512413378778a59805e5e0434f/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f35316364633839632d663638352d343532642d393936352d3665356538363630306163632f6d696e692e706e67)](https://insight.sensiolabs.com/projects/51cdc89c-f685-452d-9965-6e5e86600acc)

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

[](#requirements)

- PHP5.4+

Installation
------------

[](#installation)

Create or update your composer.json and run `composer update`

```
{
    "require": {
        "kzykhys/bowl": "1.0"
    }
}
```

Usage
-----

[](#usage)

### Define parameters

[](#define-parameters)

```
$bowl = new \Bowl\Bowl();
$bowl['lang'] = 'en';
$bowl['debug'] = true;
```

### Define a shared service

[](#define-a-shared-service)

```
$bowl = new \Bowl\Bowl();

$bowl->share('service_name', function () {
    return new stdClass();
});

var_dump($bowl->get('service_name') === $bowl->get('service_name')); // bool(true)
```

### Define a factory service

[](#define-a-factory-service)

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

[](#define-a-service-depending-on-other-services)

```
$bowl = new \Bowl\Bowl();

$bowl->share('driver.mysql', function () {
    return new MysqlDriver();
});

$bowl->share('connection', function () {
    $c = new Connection();
    $c->setDriver($this->get('driver.mysql'));

    return $c;
});
```

### Using tags to manage a collection of services

[](#using-tags-to-manage-a-collection-of-services)

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

[](#working-with-environment-flag)

```
use Bowl\Bowl;

$bowl = new Bowl();

// Common parameters
$bowl['lang'] = 'en';

// Production configuration
$bowl->configure('production', function (Bowl $bowl) {
    $bowl['debug'] = false;

    $bowl->share('orm.repository', function () {
        return new EntityRepository();
    });
});

// Development configuration
$bowl->configure('development', function (Bowl $bowl) {
    $bowl['debug'] = true;

    $bowl->share('orm.repository', function () {
        return new MockRepository();
    });
});

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

// Set enviroment manually
$bowl->env('production');

// Or using system's environment variable
$bowl->env(getenv('APP_ENV') ? getenv('APP_ENV') : 'production');
```

### Real life example

[](#real-life-example)

```
