Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
Allow loading of an external container (#19)
Browse files Browse the repository at this point in the history
* Allow loading of an external container

* Update README.md
  • Loading branch information
qurben authored Dec 1, 2019
1 parent 8a28b99 commit 31e3cf5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 52 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,19 @@ Database::transaction(function () use ($car) {

## Dependency Injection

The orm does a very simple way of dependency injection. When a instance of a model is created is created
You can provide your own `ContainerInterface` to `DependencyManager`, this container will be used for everything. You still need to provide the container with instances of `Database`, `DatabaseAdmin` and `OrmMemcache`.

```php
$container = $kernel->getContainer();

DependencyManager::setContainer($container);

$container->set(OrmMemcache::class, new OrmMemcache($cachePath));
$container->set(Database::class, new Database($pdo));
$container->set(DatabaseAdmin::class, new DatabaseAdmin($pdo));
```

It is also possible to leverage the dependency injection provided by the orm. The orm does a very simple way of dependency injection. When a instance of a model is created is created
it tries to lookup any dependencies which extend `DependencyManager` if they are found they are wired into the
model and available for use. There can only be one version of a model and this is kept track of in `DependencyManager`.

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"license": "MIT",
"require": {
"php": ">=7.0.27",
"zumba/json-serializer": "^2.2",
"psr/container": "^1.0",
"zumba/json-serializer": "^2.2",
"ext-pdo": "*",
"ext-json": "*"
},
Expand Down
104 changes: 54 additions & 50 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/DependencyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace CsrDelft\Orm;

use CsrDelft\Orm\Exception\CsrOrmException;
use Psr\Container\ContainerInterface;

/**
* Any class extending this class is able to be autoloaded.
Expand Down Expand Up @@ -31,13 +32,22 @@ abstract class DependencyManager {
*/
private static $loading = [];

/**
* @var ContainerInterface|null
*/
protected static $container = null;

/**
* Static constructor. Can be implemented in base classes.
*/
public static function __static() {
// Empty.
}

public static function setContainer(ContainerInterface $container) {
static::$container = $container;
}

/**
* Used to initialize a Singleton if needed.
*
Expand Down Expand Up @@ -71,6 +81,10 @@ public static function addDependency($dependency) {
* @return static
*/
final public static function instance() {
if (static::$container && static::$container->has(static::class)) {
return static::$container->get(static::class);
}

if (!isset(self::$instance[static::class])) {
self::$instance[static::class] = static::init();
}
Expand Down

0 comments on commit 31e3cf5

Please sign in to comment.