-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mariano
committed
Sep 17, 2019
1 parent
2ee05d2
commit faaf0af
Showing
39 changed files
with
1,551 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
### JetBrains template | ||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm | ||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
||
# User-specific stuff | ||
.idea/**/workspace.xml | ||
.idea/**/tasks.xml | ||
.idea/**/usage.statistics.xml | ||
.idea/**/dictionaries | ||
.idea/**/shelf | ||
|
||
# Generated files | ||
.idea/**/contentModel.xml | ||
|
||
# Sensitive or high-churn files | ||
.idea/**/dataSources/ | ||
.idea/**/dataSources.ids | ||
.idea/**/dataSources.local.xml | ||
.idea/**/sqlDataSources.xml | ||
.idea/**/dynamic.xml | ||
.idea/**/uiDesigner.xml | ||
.idea/**/dbnavigator.xml | ||
|
||
# Gradle | ||
.idea/**/gradle.xml | ||
.idea/**/libraries | ||
|
||
# Gradle and Maven with auto-import | ||
# When using Gradle or Maven with auto-import, you should exclude module files, | ||
# since they will be recreated, and may cause churn. Uncomment if using | ||
# auto-import. | ||
# .idea/modules.xml | ||
# .idea/*.iml | ||
# .idea/modules | ||
# *.iml | ||
# *.ipr | ||
|
||
# CMake | ||
cmake-build-*/ | ||
|
||
# Mongo Explorer plugin | ||
.idea/**/mongoSettings.xml | ||
|
||
# File-based project format | ||
*.iws | ||
|
||
# IntelliJ | ||
out/ | ||
|
||
# mpeltonen/sbt-idea plugin | ||
.idea_modules/ | ||
|
||
# JIRA plugin | ||
atlassian-ide-plugin.xml | ||
|
||
# Cursive Clojure plugin | ||
.idea/replstate.xml | ||
|
||
# Crashlytics plugin (for Android Studio and IntelliJ) | ||
com_crashlytics_export_strings.xml | ||
crashlytics.properties | ||
crashlytics-build.properties | ||
fabric.properties | ||
|
||
# Editor-based Rest Client | ||
.idea/httpRequests | ||
|
||
# Android studio 3.1+ serialized cache file | ||
.idea/caches/build_file_checksums.ser | ||
|
||
composer.lock | ||
./vendor/ | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# mdarc/DI | ||
A simple yet powerful PSR-11 autowiring dependency injection container. | ||
|
||
**mdarc/DI** was conceived to be simple to configure and use. It was built | ||
for performance. (check [benchmarks](tests/Benchmarks/README.md)) | ||
|
||
mdarc/DI has an very small but robust code base. It is **production ready** and can be used for small micro-services or large monolithic projects. | ||
|
||
##### Features: ##### | ||
- Autowiring: Automatically instantiate and inject dependencies | ||
- Manual configuration: When classes cannot be autowired, you can create them by yourself | ||
- Circular reference detection: It throws a `CircularReferenceException` with enough details to fix the problem | ||
|
||
##### What mdarc/DI is not good for: ##### | ||
- Autowiring via setter methods is not supported (and it will never be) | ||
- Autowiring using phpDoc annotations is not supported (and it will never be) | ||
- Automatically injecting dependencies on constructor parameters without type hints is not supported. You must manually configure those cases | ||
|
||
Installation | ||
------------ | ||
|
||
### Composer ### | ||
|
||
Before anything else, use this to add it to your composer.json | ||
|
||
```shell script | ||
$ composer require mdarc/di "^1.0" | ||
``` | ||
|
||
Usage | ||
----- | ||
Creating a container ready to use with autowiring enabled is a matter of creating a `Container` instance: | ||
|
||
```php | ||
use Mdarc\DI\Container; | ||
|
||
$container = new Container(); | ||
``` | ||
|
||
If your classes contain **constructor parameters** that are other objects, then simply: | ||
```php | ||
$myClass = $container->get(\Path\To\MyClass::class); | ||
``` | ||
As any other DI container, `$myClass` will always get the same instance on the requested class. | ||
|
||
If you want to create a new object every time (instead of getting the same object instance) then use the **factory** helper: | ||
```php | ||
use Mdarc\DI\Container; | ||
use Mdarc\DI\DI; | ||
|
||
$container = new Container([ | ||
\Path\To\MyClass::class => DI::factory(function () { | ||
return new \Path\To\MyClass(); | ||
}), | ||
]); | ||
|
||
$myClass = $container->get(\Path\To\MyClass::class); | ||
``` | ||
|
||
For those classes that cannot be created using autowiring, then you can add their **definitions**: | ||
```php | ||
use Mdarc\DI\Container; | ||
|
||
$container = new Container([ | ||
\Monolog\Logger::class => function (Container $c) { | ||
$config = $c->get(\Path\To\Config::class); | ||
$logger = new \Monolog\Logger($config->get('name')); | ||
$logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG)); | ||
|
||
return $logger; | ||
}), | ||
]); | ||
|
||
$logger = $container->get(\Monolog\Logger::class); | ||
``` | ||
|
||
Defining **aliases** for binding interfaces to implementations is simple: | ||
```php | ||
use Mdarc\DI\Container; | ||
|
||
$container = new Container([ | ||
// Binding Interface to implementation | ||
\Psr\Log\LoggerInterface::class => \Monolog\Logger::class, | ||
// Concrete implementation | ||
\Monolog\Logger::class => function (Container $c) { | ||
// build Monolog here | ||
}), | ||
]); | ||
|
||
$logger = $container->get(\Psr\Log\LoggerInterface::class); | ||
``` | ||
|
||
Specifying **arguments** for classes with constructor parameters that are scalar, array or undefined type: | ||
```php | ||
use Mdarc\DI\Container; | ||
|
||
class MyClass { | ||
public function __construct(array $config, \Psr\LoggerInterface $logger) { /*...*/ } | ||
} | ||
|
||
$definitions = [ | ||
// Binding Interface to implementation | ||
\Psr\Log\LoggerInterface::class => \Monolog\Logger::class, | ||
// Concrete implementation | ||
\Monolog\Logger::class => function (Container $c) { | ||
// build Monolog here | ||
}, | ||
]; | ||
|
||
$constructorParameters = [ | ||
MyClass::class => [ | ||
'config' => ['an array', 'of relevant', 'things'] | ||
], | ||
]; | ||
$container = new Container($definitions, $constructorParameters); | ||
|
||
$myClass = $container->get(MyClass::class); | ||
``` | ||
|
||
### License ### | ||
mdarc/DI is licensed under the MIT License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "mdarc/di", | ||
"description": "A simple yet powerful PSR-11 autowiring dependency injection container", | ||
"type": "library", | ||
"keywords": ["container", "di", "dependency injection", "ioc", "psr-11", "psr11", "autowiring"], | ||
"prefer-stable": true, | ||
"license": "MIT", | ||
"homepage": "https://github.com/mdarc/DI", | ||
"authors": [ | ||
{"name": "Mariano Darc", "email": "[email protected]"} | ||
], | ||
"require": { | ||
"php": ">=7.1.0", | ||
"psr/container": "^1.0", | ||
"ext-reflection": "*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.5.16", | ||
"jbzoo/profiler": "^1.0.5", | ||
"pimple/pimple":"^3.2.0", | ||
"php-di/php-di": "^6.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Mdarc\\DI\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Mdarc\\DI\\Test\\UnitTest\\": "tests/UnitTest/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<phpunit backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
beStrictAboutTestsThatDoNotTestAnything="false"> | ||
<testsuites> | ||
<testsuite name="all"> | ||
<directory>./tests/UnitTest</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
Oops, something went wrong.