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
|
6 |
|
|
33
|
|
|
public static function init(array $config) : Application |
34
|
|
|
{ |
35
|
6 |
|
// Ensure the needed config entries exists |
36
|
6 |
|
$config['events'] = $config['events'] ?? []; |
37
|
|
|
$config['components'] = $config['components'] ?? []; |
38
|
6 |
|
|
39
|
|
|
return new static($config); |
40
|
|
|
} |
41
|
6 |
|
|
42
|
|
|
public function __construct(array $config, ContainerInterface $dependencyContainer = null) |
43
|
6 |
|
{ |
44
|
|
|
$this->initDependencyContainer($config, $dependencyContainer); |
45
|
6 |
|
|
46
|
6 |
|
// 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
|
|
|
->get('fuel.application.event') |
55
|
|
|
->emit(new AppStarted($this)); |
56
|
6 |
|
} |
57
|
6 |
|
|
58
|
6 |
|
public function setDependencyContainer(ContainerInterface $dependencyContainer) |
59
|
6 |
|
{ |
60
|
|
|
$this->dependencyContainer = $dependencyContainer; |
61
|
6 |
|
} |
62
|
|
|
|
63
|
6 |
|
public function getDependencyContainer() : ContainerInterface |
64
|
6 |
|
{ |
65
|
|
|
return $this->dependencyContainer; |
66
|
6 |
|
} |
67
|
|
|
|
68
|
6 |
|
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
|
|
|
protected function registerEvents(array $events) |
87
|
|
|
{ |
88
|
|
|
/** @var Emitter $eventContainer */ |
89
|
6 |
|
$eventContainer = $this->dependencyContainer->get('fuel.application.event'); |
90
|
|
|
|
91
|
|
|
foreach ($events as $event) |
92
|
6 |
|
{ |
93
|
|
|
$eventContainer->addListener( |
94
|
6 |
|
$event['name'], |
95
|
|
|
$event['listener'], |
96
|
2 |
|
$event['priority'] ?? $eventContainer::P_NORMAL |
97
|
2 |
|
); |
98
|
2 |
|
} |
99
|
2 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
6 |
|
* @param string[] $components |
103
|
|
|
*/ |
104
|
|
|
protected function loadComponents(array $components) |
105
|
|
|
{ |
106
|
|
|
/** @var ComponentManagerInterface $componentManager */ |
107
|
6 |
|
$componentManager = $this->getDependencyContainer() |
108
|
|
|
->get('fuel.application.component_manager'); |
109
|
|
|
|
110
|
6 |
|
foreach ($components as $component) |
111
|
6 |
|
{ |
112
|
|
|
$componentManager->load($component); |
113
|
6 |
|
} |
114
|
|
|
} |
115
|
1 |
|
|
116
|
|
|
/** |
117
|
6 |
|
* @param array $config |
118
|
|
|
* @param ContainerInterface $dependencyContainer |
119
|
|
|
*/ |
120
|
|
|
protected function initDependencyContainer(array $config, ContainerInterface $dependencyContainer = null) |
121
|
|
|
{ |
122
|
|
|
$this->setDependencyContainer($dependencyContainer ?? new DependencyContainer()); |
123
|
|
|
$this->dependencyContainer->add('fuel.application', $this); |
124
|
|
|
$this->dependencyContainer->add('fuel.application.config', $config); |
125
|
|
|
$this->dependencyContainer->addServiceProvider(new ApplicationServicesProvider()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|