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
|
|
|
// Ensure the needed config entries exists |
35
|
6 |
|
$config['events'] = $config['events'] ?? []; |
36
|
6 |
|
$config['components'] = $config['components'] ?? []; |
37
|
|
|
|
38
|
6 |
|
return new static($config); |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
public function __construct(array $config, ContainerInterface $dependencyContainer = null) |
42
|
|
|
{ |
43
|
6 |
|
$this->config = $config; |
44
|
|
|
|
45
|
6 |
|
$this->setDependencyContainer($dependencyContainer ?? new Container()); |
46
|
6 |
|
$this->dependencyContainer->add('fuel.application', $this); |
47
|
6 |
|
$this->dependencyContainer->addServiceProvider(new ApplicationServicesProvider()); |
48
|
|
|
|
49
|
|
|
// register any events from the config |
50
|
6 |
|
$this->registerEvents($config['events']); |
51
|
|
|
|
52
|
|
|
// Load components |
53
|
6 |
|
$this->loadComponents($config['components']); |
54
|
|
|
|
55
|
|
|
// trigger app created event |
56
|
6 |
|
$this->dependencyContainer |
57
|
6 |
|
->get('fuel.application.event') |
58
|
6 |
|
->emit(new AppStarted()); |
59
|
6 |
|
} |
60
|
|
|
|
61
|
6 |
|
public function setDependencyContainer(ContainerInterface $dependencyContainer) |
62
|
|
|
{ |
63
|
6 |
|
$this->dependencyContainer = $dependencyContainer; |
64
|
6 |
|
} |
65
|
|
|
|
66
|
6 |
|
public function getDependencyContainer() : ContainerInterface |
67
|
|
|
{ |
68
|
6 |
|
return $this->dependencyContainer; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function run() |
72
|
|
|
{ |
73
|
|
|
// TODO: trigger request started event |
74
|
|
|
|
75
|
|
|
// TODO: route to and call controller |
76
|
|
|
|
77
|
|
|
// TODO: trigger request ended event |
78
|
|
|
|
79
|
|
|
// TODO: trigger response started event |
80
|
|
|
|
81
|
|
|
// TODO: generate and send response |
82
|
|
|
|
83
|
|
|
// TODO: send shutdown event |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $events |
88
|
|
|
*/ |
89
|
6 |
|
protected function registerEvents(array $events) |
90
|
|
|
{ |
91
|
|
|
/** @var Emitter $eventContainer */ |
92
|
6 |
|
$eventContainer = $this->dependencyContainer->get('fuel.application.event'); |
93
|
|
|
|
94
|
6 |
|
foreach ($events as $event) |
95
|
|
|
{ |
96
|
2 |
|
$eventContainer->addListener( |
97
|
2 |
|
$event['name'], |
98
|
2 |
|
$event['listener'], |
99
|
2 |
|
$event['priority'] ?? $eventContainer::P_NORMAL |
100
|
|
|
); |
101
|
|
|
} |
102
|
6 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string[] $components |
106
|
|
|
*/ |
107
|
6 |
|
protected function loadComponents(array $components) |
108
|
|
|
{ |
109
|
|
|
/** @var ComponentManagerInterface $componentManager */ |
110
|
6 |
|
$componentManager = $this->getDependencyContainer() |
111
|
6 |
|
->get('fuel.application.component_manager'); |
112
|
|
|
|
113
|
6 |
|
foreach ($components as $component) |
114
|
|
|
{ |
115
|
1 |
|
$componentManager->load($component); |
116
|
|
|
} |
117
|
6 |
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|