Completed
Push — master ( 27c6b5...777874 )
by Emlyn
05:09
created

ComponentManager::addPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @package    Fuel\Foundation
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2016 Fuel Development Team
8
 * @link       http://fuelphp.com
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fuel\Foundation;
14
15
use Fuel\Config\ContainerInterface;
16
use Fuel\FileSystem\Finder;
17
use Fuel\Foundation\Exception\ComponentLoad;
18
19
/**
20
 * Keeps track of fuel's components
21
 *
22
 * @package Fuel\Foundation
23
 */
24
class ComponentManager implements ComponentManagerInterface
25
{
26
	/**
27
	 * Name of the component class to look for.
28
	 *
29
	 * @var string
30
	 */
31
	protected static $componentClassName  = 'FuelComponent';
32
33
	/**
34
	 * @var ComponentInterface[]
35
	 */
36
	protected $loadedComponents = [];
37
38
	/**
39
	 * @var Finder
40
	 */
41
	protected $finder;
42
43 13
	public function __construct(Finder $finder)
44
	{
45 13
		$this->finder = $finder;
46 13
	}
47
48
	/**
49
	 * {@inheritdoc}
50
	 */
51 1
	public function get(string $name) : ComponentInterface
52
	{
53 1
		if ($this->loaded($name))
54
		{
55 1
			return $this->loadedComponents[$name];
56
		}
57
58 1
		return $this->load($name);
59
	}
60
61
	/**
62
	 * {@inheritdoc}
63
	 */
64 8
	public function load(string $name) : ComponentInterface
65
	{
66 8
		$fullName = $name . '\\' . static::$componentClassName;
67
68
		// Check if component class exists
69 8
		if ( ! class_exists($fullName))
70
		{
71 1
			throw new ComponentLoad("FOU-001: Unable to load [$fullName]: Class not found");
72
		}
73
74
		// Load the component
75 7
		$component = new $fullName();
76
77
		// Check if it implements the correct interface
78 7
		if ( ! $component instanceof ComponentInterface)
79
		{
80 1
			throw new ComponentLoad("FOU-002: Unable to load [$fullName]: Does not implement ComponentInterface");
81
		}
82
83 6
		$this->loadedComponents[$name] = $component;
84
85 6
		$this->addPath($component);
86
87 6
		return $component;
88
	}
89
90 6
	protected function addPath(ComponentInterface $component)
91
	{
92 6
		$this->finder->addPath($component->getPath());
93 6
	}
94
95
	/**
96
	 * {@inheritdoc}
97
	 */
98 3
	public function loaded(string $name) : bool
99
	{
100 3
		return isset($this->loadedComponents[$name]);
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106 1
	public function unload(string $name) : ComponentManagerInterface
107
	{
108 1
		if ($this->loaded($name))
109
		{
110 1
			unset($this->loadedComponents[$name]);
111
		}
112
113 1
		return $this;
114
	}
115
116
}
117