PHPackages                             matecat/simple-dic - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. matecat/simple-dic

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

matecat/simple-dic
==================

Simple DIC Client

v1.1.10(6y ago)15MITPHPPHP &gt;=5.5

Since Aug 1Pushed 2y ago1 watchersCompare

[ Source](https://github.com/matecat/simple-dic)[ Packagist](https://packagist.org/packages/matecat/simple-dic)[ Docs](https://github.com/matecat/simple-dic)[ RSS](/packages/matecat-simple-dic/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (5)Versions (25)Used By (0)

Simple DIC
==========

[](#simple-dic)

[![Codacy Badge](https://camo.githubusercontent.com/efb9360be24a6a7bbdd42598b948c69cba906316f75cf4fdf72bcbf56cd0db59/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3562656533633561356537373465356162613166636639663632326630386432)](https://www.codacy.com/app/mauretto78_2/simple-dic?utm_source=github.com&utm_medium=referral&utm_content=mauretto78/simple-dic&utm_campaign=Badge_Grade)![license](https://camo.githubusercontent.com/d54f5c226f68c60b4ddac0e9391c2e958b0524fc0eda7b75a1fc64c9b775d496/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6174656361742f73696d706c652d6469632e737667)![Packagist](https://camo.githubusercontent.com/47d2f2e0b5c2d7033b470cc17797f52102180c4c268dbf693584dbbf1eb0958c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6174656361742f73696d706c652d6469632e737667)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/1002bbc7cf652cc8827b189914bb5d7a7c67809f20a46dfda9774ca3f4132273/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6174656361742f73696d706c652d6469632f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mauretto78/simple-dic/?branch=master)

**Simple DIC** is a simple Dependency Injection Container(DIC).

Basic Usage
-----------

[](#basic-usage)

To init DIC you must provide a configuration file. Use `initFromFile` method:

Here is a sample (in YAML format):

```
dummy-key: 'dummy-value'
dummy-array: [43243,2432,4324,445667]
three: 3
two: 2
acme:
  class: Matecat\SimpleDIC\Dummy\Acme
acme-calculator:
  class: Matecat\SimpleDIC\Dummy\AcmeCalculator
  method: init
  method_arguments: ['@three', '@two']
acme-parser:
  class: Matecat\SimpleDIC\Dummy\AcmeParser
  arguments: ['string']
acme-repo:
  class: Matecat\SimpleDIC\Dummy\AcmeRepo
  arguments: ['@acme']
```

After pass the name of dependency, you can specify:

- `class` : the full qualified class name
- `arguments`: an array of arguments to pass to instantiate the class from the constructor
- `method`: if you want to run a specific method of the class (method could be static or not not)
- `method_arguments`: an array of arguments to pass to instantiate the runned class method

If you want to pass an entry already present to other one, simply use the '@' symbol.

Change Caching Directory
------------------------

[](#change-caching-directory)

You cah use `setCacheDir` to setup yor cache directory. Do this **before** invoke `initFromFile` method:

```
DIC::setCacheDir(__DIR__.'/../_cache_custom');
DIC::initFromFile(__DIR__ . '/../config/ini/redis.ini');

// ...
```

Parameters
----------

[](#parameters)

If you want to use a separate parameters file, you can use `DICParams` class. Take a look at the following example of params configuration file (YAML format):

```
your_secret_token: 'YOUR_SECRET_TOKEN'
your_secret_password: 'YOUR_SECRET_PASS'
```

You can setup `DICParams` class now:

```
use Matecat\SimpleDIC\DICParams;

DICParams::initFromFile('your_params_file.yaml');
```

And then, you can use '%' synthax in your DIC configuration file. Please bear in mind that you MUST set parameters **before** instantiate DIC.

```
client:
    class: 'SimpleDIC\Dummy\Client'
    arguments: ['%your_secret_token%', '%your_secret_password%']
```

Environment Variable Support
----------------------------

[](#environment-variable-support)

To use your environment variable, simply follow the `%env(xxxx)%` synthax, consider this example:

```
logger:
    class: 'Matecat\SimpleDIC\Dummy\Logger'
    arguments: ['%env(FOO)%']
```

Retrieve an entry
-----------------

[](#retrieve-an-entry)

In order to retrieve an entry use `get` method:

```
use Matecat\SimpleDIC\DIC;

$dependency = DIC::get('key');
```

Please note that the method returns:

- `false` if the entry has a wrong configuration;
- `NULL` if the entry does not exists.

Lazy loading and automatic caching
----------------------------------

[](#lazy-loading-and-automatic-caching)

The entries are **lazy-loaded** when you invoke `get` or `has` method for the first time.

If you have apcu enabled on your system, DIC will automatically cache the entry in APCU store. Please note that the **id in cache always refers to the sha1() of the init file**.

Commands
--------

[](#commands)

If you have an application which uses [Symfony Console](https://github.com/symfony/console), you have some commands available:

- `dic:debug` Dumps the entry list in the DIC.

You can register the commands in your app, consider this example:

```
#!/usr/bin/env php
