Completed
Push — master ( 0cb618...205443 )
by Emlyn
11:02
created

Application::performRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 12
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 1
nc 1
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\Config\Container as ConfigContainer;
16
use Fuel\Dependency\Container as DependencyContainer;
17
use Fuel\Foundation\Event\AppStarted;
18
use Fuel\Foundation\Request\RequestInterface;
19
use Fuel\Foundation\Response\ResponseInterface;
20
use League\Container\ContainerInterface;
21
use League\Event\Emitter;
22
23
class Application
24
{
25
	/**
26
	 * @var ContainerInterface
27
	 */
28
	protected $dependencyContainer;
29
30
	/**
31
	 * @var array
32
	 */
33
	protected $config;
34
35 6
	public static function init(array $config) : Application
36
	{
37
		// Ensure the needed config entries exists
38 6
		$config['events'] = $config['events'] ?? [];
39 6
		$config['components'] = $config['components'] ?? [];
40
41 6
		return new static($config);
42
	}
43
44 6
	public function __construct(array $config, ContainerInterface $dependencyContainer = null)
45
	{
46 6
		$this->initDependencyContainer($config, $dependencyContainer);
47
48
		// register any events from the config
49 6
		$this->registerEvents($config['events']);
50
51
		// Load components
52 6
		$this->loadComponents($config['components']);
53
54
		// trigger app created event
55 6
		$this->dependencyContainer
56 6
			->get('fuel.application.event')
57 6
			->emit(new AppStarted($this));
58 6
	}
59
60 6
	public function setDependencyContainer(ContainerInterface $dependencyContainer)
61
	{
62 6
		$this->dependencyContainer = $dependencyContainer;
63 6
	}
64
65 6
	public function getDependencyContainer() : ContainerInterface
66
	{
67 6
		return $this->dependencyContainer;
68
	}
69
70
	public function run()
71
	{
72
		// TODO: make sure the URI is set
73
74
		// TODO: send shutdown event
75
	}
76
77
	public function performRequest(RequestInterface $request) : ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
	{
79
		// TODO: trigger request started event
80
81
		// TODO: route to and call controller
82
83
		// TODO: trigger request ended event
84
85
		// TODO: trigger response started event
86
87
		// TODO: generate and send response
88
	}
89
90
	/**
91
	 * @param array $events
92
	 */
93 6
	protected function registerEvents(array $events)
94
	{
95
		/** @var Emitter $eventContainer */
96 6
		$eventContainer = $this->dependencyContainer->get('fuel.application.event');
97
98 6
		foreach ($events as $event)
99
		{
100 2
			$eventContainer->addListener(
101 2
				$event['name'],
102 2
				$event['listener'],
103 2
				$event['priority'] ?? $eventContainer::P_NORMAL
104
			);
105
		}
106 6
	}
107
108
	/**
109
	 * @param string[] $components
110
	 */
111 6
	protected function loadComponents(array $components)
112
	{
113
		/** @var ComponentManagerInterface $componentManager */
114 6
		$componentManager = $this->getDependencyContainer()
115 6
			->get('fuel.application.component_manager');
116
117 6
		foreach ($components as $component)
118
		{
119 1
			$componentManager->load($component);
120
		}
121 6
	}
122
123
	/**
124
	 * @param array              $config
125
	 * @param ContainerInterface $dependencyContainer
126
	 */
127 6
	protected function initDependencyContainer(array $config, ContainerInterface $dependencyContainer = null)
128
	{
129 6
		$this->setDependencyContainer($dependencyContainer ?? new DependencyContainer());
130
		// So our application can be fetched
131 6
		$this->dependencyContainer->add('fuel.application', $this);
132
		// And our application config if needed
133 6
		$this->dependencyContainer->add('fuel.application.config', $config);
134
		// Also create a config container for our services
135 6
		$this->dependencyContainer->add('fuel.config', new ConfigContainer());
136
		// Finally add all our application level services
137 6
		$this->dependencyContainer->addServiceProvider(new ApplicationServicesProvider());
138 6
	}
139
140
}
141