File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88
99class Container implements ContainerInterface
1010{
11+ /**
12+ * Contains all entries
13+ *
14+ * @var $container array
15+ */
1116 private $ container = [];
1217
18+ /**
19+ * Contains all instantiated entries
20+ *
21+ * @var $instances array
22+ */
23+ private $ instances = [];
24+
1325 /**
1426 * Container constructor accept an array.
1527 * It must be an associative array with a 'alias' key and a 'entry' value.
@@ -36,14 +48,19 @@ public function __construct($entries = [])
3648 */
3749 public function get ($ alias )
3850 {
39- if (isset ($ this ->container [$ alias ])) {
40- if (is_callable ($ this ->container [$ alias ])) {
41- return $ this ->container [$ alias ]($ this );
42- }
51+ if (!isset ($ this ->container [$ alias ])) {
52+ throw new NotFoundException ("$ alias doesn't exists in the container " );
53+ }
4354
55+ if (!is_callable ($ this ->container [$ alias ])) {
4456 return $ this ->container [$ alias ];
4557 }
46- throw new NotFoundException ("$ alias doesn't exists in the container " );
58+
59+ if (!isset ($ this ->instances [$ alias ])) {
60+ $ this ->instances [$ alias ] = $ this ->container [$ alias ]($ this );
61+ }
62+
63+ return $ this ->instances [$ alias ];
4764 }
4865
4966 /**
Original file line number Diff line number Diff line change @@ -94,6 +94,20 @@ public function testHasEntry()
9494 $ this ->assertTrue ($ c ->has ('entry ' ));
9595 }
9696
97+ /**
98+ * Test that is always returned the same object
99+ */
100+ public function testReturnAlwaysSameInstances ()
101+ {
102+ $ entries = [
103+ 'instance ' => function () {
104+ return new \SplObjectStorage ();
105+ }
106+ ];
107+ $ c = new Container ($ entries );
108+ $ this ->assertTrue ($ c ->get ('instance ' ) === $ c ->get ('instance ' ));
109+ }
110+
97111 /**
98112 * Test that has not an entry
99113 */
You can’t perform that action at this time.
0 commit comments