Recommend this page to a friend! |
Download |
Info | Documentation | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not enough user ratings | Total: 56 | All time: 10,575 This week: 206 |
Version | License | PHP version | Categories | |||
php-di-container 1.0 | MIT/X Consortium ... | 5.5 | PHP 5, Language, Design Patterns |
Description | Author | ||||||||
This package can resolve dependencies from configuration files. |
|
PHP DI Container resolves dependencies for php application using Dependency Injection. And it provides a simple container for all depnedencies as well. It can take dependencies as argument to other classes and resolve them efficiently. Good thing about container is you can specify dependencies in many formats:
PHP DI Container is capable of reading configuration from above four formats and load them as services to the container using lazy loading using PHP Simple Config While using PHP Simple Config can use cache for our configuration as well to boost the performance. Please go through details about PHP Simple Config.
Future plan is to allow simple php container without any config file for small applications.
Note: Currently its only support PHP version 5.5 and above
Package Details:
Add following dependency to your composer.json
{
require: {
// Add this line to your `composer require` as shown below
"gr8abbasi/php-di-container":"dev-master"
}
}
Create a configuration file in the desired formate and use desired reader by default service loader will use php config file reader
use Mcustiel\Config\Drivers\Reader\php\Reader as PhpReader;
use Mcustiel\Config\Drivers\Reader\ini\Reader as IniReader;
use Mcustiel\Config\Drivers\Reader\json\Reader as JsonReader;
use Mcustiel\Config\Drivers\Reader\yaml\Reader as YamlReader;
PHP configuration file looks like something below and you can see it contains others classes as dependencies as well.
<?php
return [
'class-a' => [
'class' => 'Tests\\DummyServices\\ClassA',
],
'class-b' => [
'class' => 'Tests\\DummyServices\\ClassB',
'arguments' => [
'class-a'
]
],
'class-c' => [
'class' => 'Tests\\DummyServices\\ClassC',
'arguments' => [
'class-b'
]
],
];
$loader = new ServiceLoader();
$services = $loader->loadServices(
__DIR__ . "/Fixtures/phpServicesConfig.php",
new PhpReader()
);
$container = new Container($services);
// To get services from container
$service = $container->get('foo');
{
"services": [
{
"id": "class-a",
"class": "Tests\\DummyServices\\ClassA"
},
{
"id": "class-b",
"class": "Tests\\DummyServices\\ClassB",
"arguments": [
{
"id": "class-a"
}
]
},
{
"id": "class-c",
"class": "Tests\\DummyServices\\ClassC",
"arguments": [
{
"id": "class-a",
"id": "class-b"
}
]
}
]
}
$services = $loader->loadServices(
__DIR__ . "/Fixtures/jsonServicesConfig.json",
new JsonReader()
);
SERVICES:
class-a:
class: Tests\\DummyServices\\ClassA
class-b:
class: Tests\\DummyServices\\ClassB
arguments:
class: class-a
class-c:
class: Tests\\DummyServices\\ClassC
arguments:
class: class-a
class: class-b
$services = $loader->loadServices(
__DIR__ . "/Fixtures/yamlServicesConfig.yml",
new YamlReader()
);
[SERVICES]
class-a.class = Tests\\DummyServices\\ClassA
class-b.class = Tests\\DummyServices\\ClassB
class-b.arguments = class-a
class-c.class = Tests\\DummyServices\\ClassC
class-c.arguments = class-a
class-c.arguments = class-b
$services = $loader->loadServices(
__DIR__ . "/Fixtures/iniServicesConfig.ini",
new IniReader()
);
Files (29) |
File | Role | Description | ||
---|---|---|---|---|
src (2 files, 3 directories) | ||||
tests (3 files, 4 directories) | ||||
.scrutinizer.yml | Data | Auxiliary data | ||
.travis.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files (29) | / | src |
File | Role | Description | ||
---|---|---|---|---|
Exception (3 files) | ||||
Factory (3 files) | ||||
Repository (2 files) | ||||
Container.php | Class | Class source | ||
ServiceLoader.php | Class | Class source |
Files (29) | / | src | / | Exception |
File | Role | Description |
---|---|---|
CircularDependencyException.php | Class | Class source |
ContainerException.php | Class | Class source |
NotFoundException.php | Class | Class source |
Files (29) | / | src | / | Factory |
File | Role | Description |
---|---|---|
ConfigFileServiceFactory.php | Class | Class source |
JsonServiceFactory.php | Class | Class source |
ServiceFactoryInterface.php | Class | Class source |
Files (29) | / | src | / | Repository |
File | Role | Description |
---|---|---|
InMemoryServiceRepository.php | Class | Class source |
ServiceRepositoryInterface.php | Class | Class source |
Files (29) | / | tests |
File | Role | Description | ||
---|---|---|---|---|
DummyServices (3 files) | ||||
Factory (2 files) | ||||
Fixtures (4 files) | ||||
Repository (1 file) | ||||
autoload.php | Aux. | Auxiliary script | ||
ContainerTest.php | Class | Class source | ||
ServiceLoaderTest.php | Class | Class source |
Files (29) | / | tests | / | DummyServices |
File | Role | Description |
---|---|---|
ClassA.php | Class | Class source |
ClassB.php | Class | Class source |
ClassC.php | Class | Class source |
Files (29) | / | tests | / | Factory |
File | Role | Description |
---|---|---|
ConfigFileServiceFactoryTest.php | Class | Class source |
services.json | Data | Auxiliary data |
Files (29) | / | tests | / | Fixtures |
File | Role | Description |
---|---|---|
iniServicesConfig.ini | Data | Auxiliary data |
jsonServicesConfig.json | Data | Auxiliary data |
phpServicesConfig.php | Aux. | Auxiliary script |
yamlServicesConfig.yml | Data | Auxiliary data |
Files (29) | / | tests | / | Repository |
File | Role | Description |
---|---|---|
InMemoryServiceRepositoryTest.php | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.