1
|
|
|
<?php |
2
|
|
|
namespace samsonphp\core; |
3
|
|
|
|
4
|
|
|
use samsonframework\core\SystemInterface; |
5
|
|
|
use samsonphp\config\Scheme; |
6
|
|
|
use samsonphp\event\Event; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Core |
10
|
|
|
* |
11
|
|
|
* @package samsonphp/core |
12
|
|
|
* @author Vitaly Iegorov <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Core implements SystemInterface |
15
|
|
|
{ |
16
|
|
|
/** @var string Current system environment */ |
17
|
|
|
protected $environment; |
18
|
|
|
|
19
|
|
|
/** @var Module[] Loaded modules collection */ |
20
|
|
|
protected $modules; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Core constructor |
24
|
|
|
*/ |
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
// Fire core creation event |
28
|
|
|
Event::fire('core.created', array(&$this)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Change current system working environment. |
33
|
|
|
* |
34
|
|
|
* @param string $environment Environment identifier |
35
|
|
|
* |
36
|
|
|
* @return $this Chaining |
37
|
|
|
*/ |
38
|
|
|
public function environment($environment = Scheme::BASE) |
39
|
|
|
{ |
40
|
|
|
$this->environment = $environment; |
41
|
|
|
|
42
|
|
|
// Signal core environment change |
43
|
|
|
Event::signal('core.environment.change', array($environment, &$this)); |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Load module. |
50
|
|
|
* |
51
|
|
|
* @param ModuleInterface $instance Module ancestor for loading |
52
|
|
|
* |
53
|
|
|
* @return $this Chaining |
54
|
|
|
*/ |
55
|
|
|
public function load($instance, $alias = null) |
56
|
|
|
{ |
57
|
|
|
// Store module instance by alias or class name |
58
|
|
|
$this->modules[$alias ?: get_class($instance)] = $instance; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Start SamsonPHP framework. |
65
|
|
|
* |
66
|
|
|
* @param string $default Default module identifier |
67
|
|
|
*/ |
68
|
|
|
public function start($default) |
69
|
|
|
{ |
70
|
|
|
// Fire core started event |
71
|
|
|
Event::fire('core.started'); |
72
|
|
|
|
73
|
|
|
// Security layer |
74
|
|
|
$securityResult = true; |
75
|
|
|
// Fire core security event |
76
|
|
|
Event::fire('core.security', array(&$this, &$securityResult)); |
77
|
|
|
|
78
|
|
|
/** @var mixed $result External route controller action result */ |
79
|
|
|
$result = false; |
80
|
|
|
|
81
|
|
|
// If we have passed security application layer |
82
|
|
|
if ($securityResult) { |
83
|
|
|
// Fire core routing event - go to routing application layer |
84
|
|
|
Event::signal('core.routing', array(&$this, &$result, $default)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// If no one has passed back routing callback |
88
|
|
|
if ($result === false) { |
89
|
|
|
// TODO: Needs to change |
90
|
|
|
// Fire core e404 - routing failed event |
91
|
|
|
$result = Event::signal('core.e404'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Response |
95
|
|
|
$output = ''; |
96
|
|
|
|
97
|
|
|
// If this is not asynchronous response and controller has been executed |
98
|
|
|
if ($result !== false) { |
99
|
|
|
// Fire after render event |
100
|
|
|
Event::fire('core.rendered', array(&$output)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Output results to client |
104
|
|
|
echo $output; |
105
|
|
|
|
106
|
|
|
// Fire ended event |
107
|
|
|
Event::fire('core.ended', array(&$output)); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|