Completed
Push — master ( 42ba51...fbaf1f )
by Steve
03:48
created

Application::registerEvents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
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 League\Container\ContainerInterface;
17
use League\Event\Emitter;
18
19
class Application
20
{
21
	/**
22
	 * @var ContainerInterface
23
	 */
24
	protected $dependencyContainer;
25
26 5
	public static function init(array $config) : Application
27
	{
28 5
		return new static($config);
29
	}
30
31 5
	public function __construct(array $config, ContainerInterface $dependencyContainer = null)
32
	{
33
		// Ensure the DI entry exists
34 5
		$config['di'] = $config['di'] ?? [];
35
36 5
		$this->setDependencyContainer($dependencyContainer ?? new Container($config));
37 5
		$this->dependencyContainer->add('fuel.application', $this);
38 5
		$this->dependencyContainer->addServiceProvider(new ApplicationServicesProvider());
39
40
		// register any events from the config
41 5
		$this->registerEvents($config);
42
43
		// TODO: Load components
44
45
		// TODO: trigger app created event
46 5
	}
47
48 5
	public function setDependencyContainer(ContainerInterface $dependencyContainer)
49
	{
50 5
		$this->dependencyContainer = $dependencyContainer;
51 5
	}
52
53 5
	public function getDependencyContainer() : ContainerInterface
54
	{
55 5
		return $this->dependencyContainer;
56
	}
57
58
	public function run()
59
	{
60
		// TODO: trigger request started event
61
62
		// TODO: route to and call controller
63
64
		// TODO: trigger request ended event
65
66
		// TODO: trigger response started event
67
68
		// TODO: generate and send response
69
70
		// TODO: send shutdown event
71
	}
72
73
	/**
74
	 * @param array $config
75
	 */
76 5
	protected function registerEvents(array $config)
77
	{
78
		/** @var Emitter $eventContainer */
79 5
		$eventContainer = $this->dependencyContainer->get('fuel.application.event');
80
81 5
		foreach ($config['events'] ?? [] as $event)
82
		{
83 1
			$eventContainer->addListener(
84 1
				$event['name'],
85 1
				$event['listener'],
86 1
				$event['priority'] ?? $eventContainer::P_NORMAL
87
			);
88
		}
89 5
	}
90
91
}
92