PHPackages                             catacgc/juice-di-container - 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. [Framework](/categories/framework)
4. /
5. catacgc/juice-di-container

ActiveLibrary[Framework](/categories/framework)

catacgc/juice-di-container
==========================

Small, fast, feature rich Dependency Injection for PHP 5.2

14591PHP

Since Mar 10Pushed 13y ago3 watchersCompare

[ Source](https://github.com/catacgc/juice-di-container)[ Packagist](https://packagist.org/packages/catacgc/juice-di-container)[ RSS](/packages/catacgc-juice-di-container/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Juice - Fast DI Container for PHP &gt;= 5.2
===========================================

[](#juice---fast-di-container-for-php--52)

Features
--------

[](#features)

- small and fast
- compatible with PHP 5.2
- lighter than most other PHP DI container implementations (Symfony2, ZF2)
- fluent service definition interface
- tag service definitions
- circular dependency resolver
- ArrayAccess implementation so it can be easily be mocked in tests with an array

Installing
----------

[](#installing)

The container is comprised of a single file, **src/Container.php**

For PHP 5.2 you can use curl / wget / git to download that file and simple require it

For PHP 5.3 on, you can use composer:

```
{
    "catacgc/juice-di-container": "dev-master"
}

```

After cloning the repo, you can run the tests with phpunit:

```
phpunit -c phpunit.xml.dist

```

Creating the container
----------------------

[](#creating-the-container)

```
require 'src/Container.php';
$container = new JuiceContainer();

```

Container values
----------------

[](#container-values)

The container stores parameters as key =&gt; value, but it's main benefit is the runtime wiring of services

The services are created using two special container values:

- **JuiceDefinition** instances
- valid php **callbacks** (including closures, from PHP 5.3 on) that receive the container instance as their only parameter to allow referencing of other parameters or services

Parameters
----------

[](#parameters)

```
$container['mysql_host'] = 'localhost';
$container['mysql_user'] = 'username';
$container['mysql_pass'] = 'password';
$container['mysql_port'] = 3306;

echo $container['mysql_port'];

```

Service definitions
-------------------

[](#service-definitions)

Because creating factory methods for every service is tedious, the container specially handles a **JuiceDefinition**type that is handy when creating complex services

You can reference other services or parameters in a definition using **@service\_id** notation in all of JuiceDefinition method arguments

### API

[](#api)

```
// simple class
$container['conn'] = new JuiceDefinition('Connection');   // => new Connection()

// or using the fluent interface to do the same
$container['conn'] = JuiceDefinition::create('Connection')

// replacing the class name
$container['conn'] = JuiceDefinition::create('Connection')->className('ChangedMyMindConnection')

// providing arguments
JuiceDefinition::create('Connection', array('username', 'password'))

// replacing arguments
JuiceDefinition::create('Connection')->arguments(array('username1', 'password1'))

// replacing specific argument
JuiceDefinition::create('Connection')->argument(0, 'new_username')

// calling methods
JuiceDefinition::create('Connection', array('user', 'pass'))
    ->call('setDb', array('db_name'))

```

### Constructor injection:

[](#constructor-injection)

```
$container['conn'] = new JuiceDefinition('MysqlConnection', array('@mysql_user', '@mysql_password'));
$container['dbal'] = new JuiceDefinition('Dbal', array('@conn'));

```

Now calling `$dbal = $container['dbal']` is equivalent with calling

```
$connection = new MysqlConnection('username', 'password');
$dbal = new Dbal($connection);

```

### Setter injection

[](#setter-injection)

```
$container['conn'] = new JuiceDefinition('Connection');
$container['dbal'] = JuiceDefinition::create('Dbal')->call('setConnection', array('@conn'))

```

### Extending definitions

[](#extending-definitions)

```
$container['conn'] = JuiceDefinition::create('MysqlConnection');

// now in another module, in its configuration

$connDef = $container->raw('conn');
$connDef->className('MysqlWrapperConnection');

```

Adding services from callbacks
------------------------------

[](#adding-services-from-callbacks)

Every callable that is passed to the container will be called, when retrieved by the client, with the container instance as it's only parameter and the return value will represent the actual service / parameter for the associated id.

```
$container['db'] = array('Factory', 'createDbConnection');
$pdoObject = $container['db']; //actually calls the factory method

```

where the factory will look like:

```
class Factory
{
    public static function createDbConnection($container) // unlock();`

Creating new service instances
------------------------------

[](#creating-new-service-instances)

The default behaviour is to retrive the same service instance on each call.

If you want to create new service instances on the fly use the build method:

```
$conn1 = $container->build($container->raw('conn'));
$conn2 = $container->build($container->raw('conn'));

```

Full example usage
------------------

[](#full-example-usage)

```
/**
* Configuration
*/

$container = new JuiceContainer();

$container['cache_dir'] = '/tmp/cache';
$container['memcache_host'] = 'localhost';
$container['memcache_port'] = 11211;

$container['main_cache'] = JuiceDefinition::create('Memcache')
    ->call('connect', array('@memcache_host', '@memcache_port'));

$container['slow_cache'] = JuiceDefinition::create('FileCache')
    ->arguments(array('@cache_dir'));

$container['two_level_cache'] = JuiceDefinition::create('TwoLevelCache')
    ->arguments(array('@main_cache', '@slow_cache'));

$container['cache'] = '@two_level_cache';

/**
* Usage
*/

$cache = $container['cache'];

if ($data = $cache->load('expensive_operation_id')) {
    //cache hit
    return;
}

$cache->save('expensive_operation_id', do_expensive_operation(), 60);

```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/860d0a8a9c3447f54f66fb539254b0158d19425fec025f59ee8b773c901b7f06?d=identicon)[catacgc](/maintainers/catacgc)

---

Top Contributors

[![catacgc](https://avatars.githubusercontent.com/u/490592?v=4)](https://github.com/catacgc "catacgc (10 commits)")

### Embed Badge

![Health badge](/badges/catacgc-juice-di-container/health.svg)

```
[![Health](https://phpackages.com/badges/catacgc-juice-di-container/health.svg)](https://phpackages.com/packages/catacgc-juice-di-container)
```

###  Alternatives

[laravel/passport

Laravel Passport provides OAuth2 server support to Laravel.

3.4k85.0M532](/packages/laravel-passport)[nolimits4web/swiper

Most modern mobile touch slider and framework with hardware accelerated transitions

41.8k177.2k1](/packages/nolimits4web-swiper)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k36.7M259](/packages/laravel-dusk)[laravel/prompts

Add beautiful and user-friendly forms to your command-line applications.

712181.8M596](/packages/laravel-prompts)[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M121](/packages/cakephp-chronos)[laravel/pail

Easily delve into your Laravel application's log files directly from the command line.

91545.3M590](/packages/laravel-pail)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
