@@ -12,49 +12,49 @@ |
||
12 | 12 | |
13 | 13 | class ComponentContainer implements ContainerInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * @var object[] |
|
17 | - */ |
|
18 | - protected $storedComponents = []; |
|
19 | - |
|
20 | - /** |
|
21 | - * @param ComponentInterface $object $object |
|
22 | - * |
|
23 | - * @throws ContainerException |
|
24 | - */ |
|
25 | - public function add(ComponentInterface $object) |
|
26 | - { |
|
27 | - $id = get_class($object); |
|
28 | - |
|
29 | - if ($this->has($id)) |
|
30 | - throw new ContainerException('A class with ID ' . $id . ' already exists in this container.'); |
|
31 | - |
|
32 | - $this->storedComponents[$id] = $object; |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * @inheritdoc |
|
37 | - */ |
|
38 | - public function get($id) |
|
39 | - { |
|
40 | - if (!is_string($id)) |
|
41 | - throw new ContainerException('Given ID must be a string'); |
|
42 | - |
|
43 | - if (!$this->has($id)) |
|
44 | - throw new NotFoundException(); |
|
45 | - |
|
46 | - return $this->storedComponents[$id]; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @inheritdoc |
|
15 | + /** |
|
16 | + * @var object[] |
|
17 | + */ |
|
18 | + protected $storedComponents = []; |
|
19 | + |
|
20 | + /** |
|
21 | + * @param ComponentInterface $object $object |
|
22 | + * |
|
51 | 23 | * @throws ContainerException |
52 | - */ |
|
53 | - public function has($id) |
|
54 | - { |
|
55 | - if (!is_string($id)) |
|
56 | - throw new ContainerException('Given ID must be a string'); |
|
57 | - |
|
58 | - return array_key_exists($id, $this->storedComponents); |
|
59 | - } |
|
24 | + */ |
|
25 | + public function add(ComponentInterface $object) |
|
26 | + { |
|
27 | + $id = get_class($object); |
|
28 | + |
|
29 | + if ($this->has($id)) |
|
30 | + throw new ContainerException('A class with ID ' . $id . ' already exists in this container.'); |
|
31 | + |
|
32 | + $this->storedComponents[$id] = $object; |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * @inheritdoc |
|
37 | + */ |
|
38 | + public function get($id) |
|
39 | + { |
|
40 | + if (!is_string($id)) |
|
41 | + throw new ContainerException('Given ID must be a string'); |
|
42 | + |
|
43 | + if (!$this->has($id)) |
|
44 | + throw new NotFoundException(); |
|
45 | + |
|
46 | + return $this->storedComponents[$id]; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @inheritdoc |
|
51 | + * @throws ContainerException |
|
52 | + */ |
|
53 | + public function has($id) |
|
54 | + { |
|
55 | + if (!is_string($id)) |
|
56 | + throw new ContainerException('Given ID must be a string'); |
|
57 | + |
|
58 | + return array_key_exists($id, $this->storedComponents); |
|
59 | + } |
|
60 | 60 | } |
61 | 61 | \ No newline at end of file |
@@ -26,8 +26,9 @@ discard block |
||
26 | 26 | { |
27 | 27 | $id = get_class($object); |
28 | 28 | |
29 | - if ($this->has($id)) |
|
30 | - throw new ContainerException('A class with ID ' . $id . ' already exists in this container.'); |
|
29 | + if ($this->has($id)) { |
|
30 | + throw new ContainerException('A class with ID ' . $id . ' already exists in this container.'); |
|
31 | + } |
|
31 | 32 | |
32 | 33 | $this->storedComponents[$id] = $object; |
33 | 34 | } |
@@ -37,11 +38,13 @@ discard block |
||
37 | 38 | */ |
38 | 39 | public function get($id) |
39 | 40 | { |
40 | - if (!is_string($id)) |
|
41 | - throw new ContainerException('Given ID must be a string'); |
|
41 | + if (!is_string($id)) { |
|
42 | + throw new ContainerException('Given ID must be a string'); |
|
43 | + } |
|
42 | 44 | |
43 | - if (!$this->has($id)) |
|
44 | - throw new NotFoundException(); |
|
45 | + if (!$this->has($id)) { |
|
46 | + throw new NotFoundException(); |
|
47 | + } |
|
45 | 48 | |
46 | 49 | return $this->storedComponents[$id]; |
47 | 50 | } |
@@ -52,8 +55,9 @@ discard block |
||
52 | 55 | */ |
53 | 56 | public function has($id) |
54 | 57 | { |
55 | - if (!is_string($id)) |
|
56 | - throw new ContainerException('Given ID must be a string'); |
|
58 | + if (!is_string($id)) { |
|
59 | + throw new ContainerException('Given ID must be a string'); |
|
60 | + } |
|
57 | 61 | |
58 | 62 | return array_key_exists($id, $this->storedComponents); |
59 | 63 | } |
@@ -13,10 +13,10 @@ |
||
13 | 13 | |
14 | 14 | interface ComponentInterface |
15 | 15 | { |
16 | - /** |
|
17 | - * @param ContainerInterface $container |
|
18 | - * |
|
19 | - * @return null|object |
|
20 | - */ |
|
21 | - public static function fromContainer(ContainerInterface $container); |
|
16 | + /** |
|
17 | + * @param ContainerInterface $container |
|
18 | + * |
|
19 | + * @return null|object |
|
20 | + */ |
|
21 | + public static function fromContainer(ContainerInterface $container); |
|
22 | 22 | } |
23 | 23 | \ No newline at end of file |
@@ -13,24 +13,24 @@ |
||
13 | 13 | |
14 | 14 | trait ContainerTrait |
15 | 15 | { |
16 | - /** |
|
17 | - * @var ContainerInterface |
|
18 | - */ |
|
19 | - protected $container; |
|
16 | + /** |
|
17 | + * @var ContainerInterface |
|
18 | + */ |
|
19 | + protected $container; |
|
20 | 20 | |
21 | - /** |
|
22 | - * @return ContainerInterface |
|
23 | - */ |
|
24 | - public function getContainer(): ContainerInterface |
|
25 | - { |
|
26 | - return $this->container; |
|
27 | - } |
|
21 | + /** |
|
22 | + * @return ContainerInterface |
|
23 | + */ |
|
24 | + public function getContainer(): ContainerInterface |
|
25 | + { |
|
26 | + return $this->container; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @param ContainerInterface $container |
|
31 | - */ |
|
32 | - public function setContainer(ContainerInterface $container) |
|
33 | - { |
|
34 | - $this->container = $container; |
|
35 | - } |
|
29 | + /** |
|
30 | + * @param ContainerInterface $container |
|
31 | + */ |
|
32 | + public function setContainer(ContainerInterface $container) |
|
33 | + { |
|
34 | + $this->container = $container; |
|
35 | + } |
|
36 | 36 | } |
37 | 37 | \ No newline at end of file |
@@ -12,19 +12,19 @@ |
||
12 | 12 | |
13 | 13 | trait ComponentTrait |
14 | 14 | { |
15 | - /** |
|
16 | - * @param ContainerInterface $container |
|
17 | - * |
|
18 | - * @return static |
|
19 | - * @throws NotFoundException |
|
20 | - */ |
|
21 | - public static function fromContainer(ContainerInterface $container) |
|
22 | - { |
|
23 | - $obj = $container->get(__CLASS__); |
|
15 | + /** |
|
16 | + * @param ContainerInterface $container |
|
17 | + * |
|
18 | + * @return static |
|
19 | + * @throws NotFoundException |
|
20 | + */ |
|
21 | + public static function fromContainer(ContainerInterface $container) |
|
22 | + { |
|
23 | + $obj = $container->get(__CLASS__); |
|
24 | 24 | |
25 | - if ($obj && $obj instanceof static) |
|
26 | - return $obj; |
|
25 | + if ($obj && $obj instanceof static) |
|
26 | + return $obj; |
|
27 | 27 | |
28 | - throw new NotFoundException(); |
|
29 | - } |
|
28 | + throw new NotFoundException(); |
|
29 | + } |
|
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -22,8 +22,9 @@ |
||
22 | 22 | { |
23 | 23 | $obj = $container->get(__CLASS__); |
24 | 24 | |
25 | - if ($obj && $obj instanceof static) |
|
26 | - return $obj; |
|
25 | + if ($obj && $obj instanceof static) { |
|
26 | + return $obj; |
|
27 | + } |
|
27 | 28 | |
28 | 29 | throw new NotFoundException(); |
29 | 30 | } |
@@ -13,13 +13,13 @@ |
||
13 | 13 | |
14 | 14 | class ContainerTraitTest extends TestCase |
15 | 15 | { |
16 | - public function testGetSetContainerWithTrait() |
|
17 | - { |
|
18 | - $container = new ComponentContainer(); |
|
16 | + public function testGetSetContainerWithTrait() |
|
17 | + { |
|
18 | + $container = new ComponentContainer(); |
|
19 | 19 | |
20 | - $class = new SampleContainerTraitClass(); |
|
21 | - $class->setContainer($container); |
|
20 | + $class = new SampleContainerTraitClass(); |
|
21 | + $class->setContainer($container); |
|
22 | 22 | |
23 | - self::assertSame($container, $class->getContainer()); |
|
24 | - } |
|
23 | + self::assertSame($container, $class->getContainer()); |
|
24 | + } |
|
25 | 25 | } |
@@ -13,5 +13,5 @@ |
||
13 | 13 | |
14 | 14 | class SampleContainerTraitClass |
15 | 15 | { |
16 | - use ContainerTrait; |
|
16 | + use ContainerTrait; |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -13,5 +13,5 @@ |
||
13 | 13 | |
14 | 14 | class SampleComponent implements ComponentInterface |
15 | 15 | { |
16 | - use ComponentTrait; |
|
16 | + use ComponentTrait; |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -11,52 +11,52 @@ |
||
11 | 11 | |
12 | 12 | class ComponentContainerTest extends TestCase |
13 | 13 | { |
14 | - public function testContainerGet() |
|
15 | - { |
|
16 | - $container = new ComponentContainer(); |
|
17 | - |
|
18 | - $sampleComponent = new \Yoshi2889\Container\Tests\SampleComponent(); |
|
19 | - $container->add($sampleComponent); |
|
20 | - |
|
21 | - $sampleComponentFromContainer = \Yoshi2889\Container\Tests\SampleComponent::fromContainer($container); |
|
22 | - |
|
23 | - self::assertInstanceOf(\Yoshi2889\Container\Tests\SampleComponent::class, $sampleComponentFromContainer); |
|
24 | - self::assertEquals($sampleComponent, $sampleComponentFromContainer); |
|
25 | - } |
|
26 | - |
|
27 | - public function testGetInvalidComponent() |
|
28 | - { |
|
29 | - $container = new ComponentContainer(); |
|
30 | - $this->expectException(\Yoshi2889\Container\NotFoundException::class); |
|
31 | - $container->get(\Yoshi2889\Container\Tests\SampleInvalidComponent::class); |
|
32 | - } |
|
33 | - |
|
34 | - public function testGetInvalidType() |
|
35 | - { |
|
36 | - $container = new ComponentContainer(); |
|
37 | - $this->expectException(\Yoshi2889\Container\ContainerException::class); |
|
38 | - $container->get(10); |
|
39 | - } |
|
40 | - |
|
41 | - public function testGetNotFound() |
|
42 | - { |
|
43 | - $emptyContainer = new ComponentContainer(); |
|
44 | - $this->expectException(\Yoshi2889\Container\NotFoundException::class); |
|
45 | - \Yoshi2889\Container\Tests\SampleComponent::fromContainer($emptyContainer); |
|
46 | - } |
|
47 | - |
|
48 | - public function testContainerHas() |
|
49 | - { |
|
50 | - $container = new ComponentContainer(); |
|
51 | - |
|
52 | - self::assertFalse($container->has(\Yoshi2889\Container\Tests\SampleComponent::class)); |
|
53 | - |
|
54 | - $sampleComponent = new \Yoshi2889\Container\Tests\SampleComponent(); |
|
55 | - $container->add($sampleComponent); |
|
56 | - |
|
57 | - self::assertTrue($container->has(\Yoshi2889\Container\Tests\SampleComponent::class)); |
|
58 | - |
|
59 | - $this->expectException(\Yoshi2889\Container\ContainerException::class); |
|
60 | - $container->has(10); |
|
61 | - } |
|
14 | + public function testContainerGet() |
|
15 | + { |
|
16 | + $container = new ComponentContainer(); |
|
17 | + |
|
18 | + $sampleComponent = new \Yoshi2889\Container\Tests\SampleComponent(); |
|
19 | + $container->add($sampleComponent); |
|
20 | + |
|
21 | + $sampleComponentFromContainer = \Yoshi2889\Container\Tests\SampleComponent::fromContainer($container); |
|
22 | + |
|
23 | + self::assertInstanceOf(\Yoshi2889\Container\Tests\SampleComponent::class, $sampleComponentFromContainer); |
|
24 | + self::assertEquals($sampleComponent, $sampleComponentFromContainer); |
|
25 | + } |
|
26 | + |
|
27 | + public function testGetInvalidComponent() |
|
28 | + { |
|
29 | + $container = new ComponentContainer(); |
|
30 | + $this->expectException(\Yoshi2889\Container\NotFoundException::class); |
|
31 | + $container->get(\Yoshi2889\Container\Tests\SampleInvalidComponent::class); |
|
32 | + } |
|
33 | + |
|
34 | + public function testGetInvalidType() |
|
35 | + { |
|
36 | + $container = new ComponentContainer(); |
|
37 | + $this->expectException(\Yoshi2889\Container\ContainerException::class); |
|
38 | + $container->get(10); |
|
39 | + } |
|
40 | + |
|
41 | + public function testGetNotFound() |
|
42 | + { |
|
43 | + $emptyContainer = new ComponentContainer(); |
|
44 | + $this->expectException(\Yoshi2889\Container\NotFoundException::class); |
|
45 | + \Yoshi2889\Container\Tests\SampleComponent::fromContainer($emptyContainer); |
|
46 | + } |
|
47 | + |
|
48 | + public function testContainerHas() |
|
49 | + { |
|
50 | + $container = new ComponentContainer(); |
|
51 | + |
|
52 | + self::assertFalse($container->has(\Yoshi2889\Container\Tests\SampleComponent::class)); |
|
53 | + |
|
54 | + $sampleComponent = new \Yoshi2889\Container\Tests\SampleComponent(); |
|
55 | + $container->add($sampleComponent); |
|
56 | + |
|
57 | + self::assertTrue($container->has(\Yoshi2889\Container\Tests\SampleComponent::class)); |
|
58 | + |
|
59 | + $this->expectException(\Yoshi2889\Container\ContainerException::class); |
|
60 | + $container->has(10); |
|
61 | + } |
|
62 | 62 | } |