Completed
Push — master ( c540ab...7bbae7 )
by Steve
03:57
created

Application::loadComponents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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\Dependency\Container;
16
use Fuel\Foundation\Event\AppStarted;
17
use League\Container\ContainerInterface;
18
use League\Event\Emitter;
19
20
class Application
21
{
22
	/**
23
	 * @var ContainerInterface
24
	 */
25
	protected $dependencyContainer;
26
27
	/**
28
	 * @var array
29
	 */
30
	protected $config;
31
32 6
	public static function init(array $config) : Application
33
	{
34 6
		return new static($config);
35
	}
36
37 6
	public function __construct(array $config, ContainerInterface $dependencyContainer = null)
38
	{
39
		// Ensure the DI entry exists
40 6
		$config['di'] = $config['di'] ?? [];
41
42 6
		$this->setDependencyContainer($dependencyContainer ?? new Container($config));
43 6
		$this->dependencyContainer->add('fuel.application', $this);
44 6
		$this->dependencyContainer->addServiceProvider(new ApplicationServicesProvider());
45
46
		// register any events from the config
47 6
		$config['events'] = $config['events'] ?? [];
48 6
		$this->registerEvents($config['events']);
49
50
		// Load components
51 6
		$config['components'] = $config['components'] ?? [];
52 6
		$this->loadComponents($config['components']);
53
54 6
		$this->config = $config;
55
56
		// trigger app created event
57 6
		$this->dependencyContainer
58 6
			->get('fuel.application.event')
59 6
			->emit(new AppStarted());
60 6
	}
61
62 6
	public function setDependencyContainer(ContainerInterface $dependencyContainer)
63
	{
64 6
		$this->dependencyContainer = $dependencyContainer;
65 6
	}
66
67 6
	public function getDependencyContainer() : ContainerInterface
68
	{
69 6
		return $this->dependencyContainer;
70
	}
71
72
	public function run()
73
	{
74
		// TODO: trigger request started event
75
76
		// TODO: route to and call controller
77
78
		// TODO: trigger request ended event
79
80
		// TODO: trigger response started event
81
82
		// TODO: generate and send response
83
84
		// TODO: send shutdown event
85
	}
86
87
	/**
88
	 * @param array $events
89
	 */
90 6
	protected function registerEvents(array $events)
91
	{
92
		/** @var Emitter $eventContainer */
93 6
		$eventContainer = $this->dependencyContainer->get('fuel.application.event');
94
95 6
		foreach ($events as $event)
96
		{
97 2
			$eventContainer->addListener(
98 2
				$event['name'],
99 2
				$event['listener'],
100 2
				$event['priority'] ?? $eventContainer::P_NORMAL
101
			);
102
		}
103 6
	}
104
105
	/**
106
	 * @param string[] $components
107
	 */
108 6
	protected function loadComponents(array $components)
109
	{
110
		/** @var ComponentManagerInterface $componentManager */
111 6
		$componentManager = $this->getDependencyContainer()
112 6
			->get('fuel.application.component_manager');
113
114 6
		foreach ($components as $component)
115
		{
116 1
			$componentManager->load($component);
117
		}
118 6
	}
119
120
}
121