Skip to content

Commit 0a19c4c

Browse files
author
Dogan Ucar
committed
Container
1 parent 0cfe8c7 commit 0a19c4c

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

src/DI/Container.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
namespace doganoo\PHPUtil\DI;
2727

28+
use doganoo\PHPAlgorithms\Datastructure\Lists\Node;
29+
use doganoo\PHPAlgorithms\Datastructure\Maps\HashMap;
2830
use doganoo\PHPUtil\Exception\ClassNotFoundException;
29-
use doganoo\PHPUtil\Util\AppContainer;
31+
use Exception;
3032
use ReflectionClass;
3133
use ReflectionParameter;
3234

@@ -47,6 +49,18 @@
4749
*/
4850
class Container {
4951

52+
private $instances = null;
53+
54+
private $created = null;
55+
56+
public function __construct() {
57+
$this->instances = new HashMap();
58+
$this->created = new HashMap();
59+
}
60+
61+
public function add(string $name, callable $callable):void {
62+
$this->instances->add($name, $callable);
63+
}
5064
/**
5165
* retrieving the container instance.
5266
*
@@ -56,25 +70,47 @@ class Container {
5670
* @return mixed|null
5771
*/
5872
public function get(string $name, ...$params) {
59-
try{
73+
if ($this->created->containsKey($name)) return $this->created->getNodeByKey($name)->getValue();
74+
if ($this->instances->containsKey($name)) return $this->fromInstances($name, $params);
75+
76+
try {
6077
$reflectionClass = new ReflectionClass($name);
6178
$constructor = $reflectionClass->getConstructor();
6279
$parameters = [];
6380

64-
if (null !== $constructor){
81+
if (null !== $constructor) {
6582
/** @var ReflectionParameter $parameter */
66-
foreach ($constructor->getParameters() as $parameter){
83+
foreach ($constructor->getParameters() as $parameter) {
6784
$className = $parameter->getClass()->getName();
68-
$class = AppContainer::get($className);
85+
$class = $this->get($className);
6986
if (null === $class) throw new ClassNotFoundException();
7087
$parameters[] = $class;
7188
}
7289
}
7390
$clazz = $reflectionClass->newInstanceArgs($parameters + $params);
91+
$this->created->add($name, $clazz);
7492
return $clazz;
75-
} catch (ClassNotFoundException $exception){
93+
} catch (Exception $exception) {
94+
return null;
95+
}
96+
}
97+
98+
/**
99+
* @param string $name
100+
* @param mixed ...$params
101+
* @return mixed|null
102+
*/
103+
private function fromInstances(string $name, ...$params){
104+
if (false === $this->instances->containsKey($name)) return null;
105+
106+
/** @var Node $node */
107+
$node = $this->instances->getNodeByKey($name);
108+
if (null === $node) {
76109
return null;
77110
}
111+
$clazz = $node->getValue()($params);
112+
$this->created->add($name, $clazz);
113+
return $clazz;
78114
}
79115

80116
}
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
<?php
2-
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
325

426
namespace doganoo\PHPUtil\Exception;
527

6-
7-
class ClassNotFoundException extends PHPUtilException
8-
{
28+
/**
29+
* Class ClassNotFoundException
30+
* @package doganoo\PHPUtil\Exception
31+
*/
32+
class ClassNotFoundException extends PHPUtilException {
933

1034
}

src/Exception/PHPUtilException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525

2626
namespace doganoo\PHPUtil\Exception;
2727

28+
use Exception;
29+
2830
/**
2931
* Class PHPUtilException
3032
*
3133
* @package doganoo\PHPUtil\Exception
3234
*/
33-
class PHPUtilException extends \Exception {
35+
class PHPUtilException extends Exception {
3436
protected $message = 'no message specified ¯\_(ツ)_/¯';
3537
}

0 commit comments

Comments
 (0)