Completed
Push — master ( 2c84d0...dc4ad7 )
by Steve
04:23
created

Application   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 113
ccs 38
cts 40
cp 0.95
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setDependencyContainer() 0 4 1
A init() 0 8 1
A __construct() 0 15 1
A getDependencyContainer() 0 4 1
A run() 0 14 1
A registerEvents() 0 14 2
A loadComponents() 0 11 2
A initDependencyContainer() 0 12 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\Container as ConfigContainer;
16
use Fuel\Dependency\Container as DependencyContainer;
17
use Fuel\Foundation\Event\AppStarted;
18
use League\Container\ContainerInterface;
19
use League\Event\Emitter;
20
21
class Application
22
{
23
	/**
24
	 * @var ContainerInterface
25
	 */
26
	protected $dependencyContainer;
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $config;
32
33 6
	public static function init(array $config) : Application
34
	{
35
		// Ensure the needed config entries exists
36 6
		$config['events'] = $config['events'] ?? [];
37 6
		$config['components'] = $config['components'] ?? [];
38
39 6
		return new static($config);
40
	}
41
42 6
	public function __construct(array $config, ContainerInterface $dependencyContainer = null)
43
	{
44 6
		$this->initDependencyContainer($config, $dependencyContainer);
45
46
		// register any events from the config
47 6
		$this->registerEvents($config['events']);
48
49
		// Load components
50 6
		$this->loadComponents($config['components']);
51
52
		// trigger app created event
53 6
		$this->dependencyContainer
54 6
			->get('fuel.application.event')
55 6
			->emit(new AppStarted($this));
56 6
	}
57
58 6
	public function setDependencyContainer(ContainerInterface $dependencyContainer)
59
	{
60 6
		$this->dependencyContainer = $dependencyContainer;
61 6
	}
62
63 6
	public function getDependencyContainer() : ContainerInterface
64
	{
65 6
		return $this->dependencyContainer;
66
	}
67
68
	public function run()
69
	{
70
		// TODO: trigger request started event
71
72
		// TODO: route to and call controller
73
74
		// TODO: trigger request ended event
75
76
		// TODO: trigger response started event
77
78
		// TODO: generate and send response
79
80
		// TODO: send shutdown event
81
	}
82
83
	/**
84
	 * @param array $events
85
	 */
86 6
	protected function registerEvents(array $events)
87
	{
88
		/** @var Emitter $eventContainer */
89 6
		$eventContainer = $this->dependencyContainer->get('fuel.application.event');
90
91 6
		foreach ($events as $event)
92
		{
93 2
			$eventContainer->addListener(
94 2
				$event['name'],
95 2
				$event['listener'],
96 2
				$event['priority'] ?? $eventContainer::P_NORMAL
97
			);
98
		}
99 6
	}
100
101
	/**
102
	 * @param string[] $components
103
	 */
104 6
	protected function loadComponents(array $components)
105
	{
106
		/** @var ComponentManagerInterface $componentManager */
107 6
		$componentManager = $this->getDependencyContainer()
108 6
			->get('fuel.application.component_manager');
109
110 6
		foreach ($components as $component)
111
		{
112 1
			$componentManager->load($component);
113
		}
114 6
	}
115
116
	/**
117
	 * @param array              $config
118
	 * @param ContainerInterface $dependencyContainer
119
	 */
120 6
	protected function initDependencyContainer(array $config, ContainerInterface $dependencyContainer = null)
121
	{
122 6
		$this->setDependencyContainer($dependencyContainer ?? new DependencyContainer());
123
		// So our application can be fetched
124 6
		$this->dependencyContainer->add('fuel.application', $this);
125
		// And our application config if needed
126 6
		$this->dependencyContainer->add('fuel.application.config', $config);
127
		// Also create a config container for our services
128 6
		$this->dependencyContainer->add('fuel.config', new ConfigContainer);
129
		// Finally add all our application level services
130 6
		$this->dependencyContainer->addServiceProvider(new ApplicationServicesProvider());
131 6
	}
132
133
}
134