- Saved searches
- Use saved searches to filter your results more quickly
- Codinards/PHP-DI-Container
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- ReadME.md
- About
- Saved searches
- Use saved searches to filter your results more quickly
- License
- gr8abbasi/php-di-container
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Saved searches
- Use saved searches to filter your results more quickly
- License
- PHP-DI/PHP-DI
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A simple dependency injection container for php projects
Codinards/PHP-DI-Container
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
ReadME.md
PHP Dependency Injection Container
A simple dependency injection container for php projects
composer require njeaner/di-container
Initialize without parameter
$container = new \NJContainer\Container\Container();
Initialize using the parameter
The container can be constructed with:
and NJContainer\Container\InstanceDefinition can be constructed with:
$containerDefinition = new \NJContainer\Container\ContainerDefinition(); $instance = new \NJContainer\Container\InstanceDefinition(); // or $instance = new \NJContainer\Container\InstanceDefinition($containerDefinition); $container = new \NJContainer\Container\Container($instance);
Setting a dependency in the container
/** * @param id string * @param mixed $definition * @param bool $shared, if "true" container will save this definition instance as a factory dependency */ $container->set($id, $definition) $container->set($id, $definition, true)
Setting several definition using an array of definitions
/** * @param $definitions * @example [ * 'name'=> ['John Doe'], * 'stdClass1' => [new stdClass()], * \Namespace\Route::class => [new \Namespace\Route(), true] * ]; * The true value as second item in dependency array means that you are storing an= factory dependency */ $container->add($definitions)
Setting several definition using an file with return an array of definitions
/** * @param string $definitionsPath, path directory of definition file */ $container->addDefinition($definitionsPath)
Setting dependency parameters
Setting a parameter of an alias
/** * @param string $id definition id * @param string $name parameter name * @param mixed $parameter param value to set in definition */ $container->setParameter($id, $name, $parameter)
Setting several parameter of an alias
/** * @param string $id definition id * @param array $parameters */ $container->setParameters($id, $parameters)
Getting a dependency from the container
/** * @param string $id * @param bool $shared, if "true" container with retrieve a factory dependency */ $container->get($id) $container->get($id, true)
class Foo < >class Bar < public function __construct(Foo $foo, $type = 'Bar')< $this->foo = $foo; $this->type = $type > > class FooBar< public function __construct(Foo $foo , string $name, array $params) < >>
# config.php # dependency configuration file return [ 'name'=> 'John Doe', FooBar::class => get()->setParameters( 'name' => get('name'), 'params' => ['type' => FooBar::class] ) ];
$container = new \NJContainer\Container\Container(); $container->addDefinition('config.php'); $arrayDefinition = [ FooBar::class => get()->setParameters( 'params' => add(['bool' => false]) ) ]; $container->add($arrayDefintion); $container->lock(); $fooObject = $container->get(Foo::class); $barObject = $container->get(Foo::class); // Foo::class and Bar::class has been resolved using autowiring $foobar = $container->get(FooBar::class);
About
A simple dependency injection container for php projects
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
PHP DI Container is simple dependency injection container for PHP.
License
gr8abbasi/php-di-container
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
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
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.
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() );
About
PHP DI Container is simple dependency injection container for PHP.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
The dependency injection container for humans
License
PHP-DI/PHP-DI
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
PHP-DI is a dependency injection container meant to be practical, powerful, and framework-agnostic.
Read more on the website: php-di.org
Available as part of the Tidelift Subscription
The maintainers of php-di/php-di and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
About
The dependency injection container for humans