1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
6
|
|
|
use Ffcms\Core\Exception\JsonException; |
7
|
|
|
use Ffcms\Core\Exception\NativeException; |
8
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
9
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
10
|
|
|
use Ffcms\Core\Event\EventManager; |
11
|
|
|
use Ffcms\Core\Helper\Security; |
12
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
13
|
|
|
use Ffcms\Core\Helper\Type\Str; |
14
|
|
|
use Ffcms\Core\I18n\Translate; |
15
|
|
|
use Ffcms\Core\Network\Request; |
16
|
|
|
use Ffcms\Core\Network\Response; |
17
|
|
|
use Ffcms\Core\Arch\View; |
18
|
|
|
use Ffcms\Core\Debug\Manager as Debug; |
19
|
|
|
use Ffcms\Core\Cache\MemoryObject; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class App. Provide later static callbacks as entry point from any places of ffcms. |
23
|
|
|
* @package Ffcms\Core |
24
|
|
|
*/ |
25
|
|
|
class App |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** @var \Ffcms\Core\Network\Request */ |
29
|
|
|
public static $Request; |
30
|
|
|
|
31
|
|
|
/** @var \Ffcms\Core\Properties */ |
32
|
|
|
public static $Properties; |
33
|
|
|
|
34
|
|
|
/** @var \Ffcms\Core\Network\Response */ |
35
|
|
|
public static $Response; |
36
|
|
|
|
37
|
|
|
/** @var \Ffcms\Core\Alias */ |
38
|
|
|
public static $Alias; |
39
|
|
|
|
40
|
|
|
/** @var \Ffcms\Core\Arch\View */ |
41
|
|
|
public static $View; |
42
|
|
|
|
43
|
|
|
/** @var \Ffcms\Core\Debug\Manager|null */ |
44
|
|
|
public static $Debug; |
45
|
|
|
|
46
|
|
|
/** @var \Ffcms\Core\Helper\Security */ |
47
|
|
|
public static $Security; |
48
|
|
|
|
49
|
|
|
/** @var \Ffcms\Core\I18n\Translate */ |
50
|
|
|
public static $Translate; |
51
|
|
|
|
52
|
|
|
/** @var \Ffcms\Core\Interfaces\iUser */ |
53
|
|
|
public static $User; |
54
|
|
|
|
55
|
|
|
/** @var \Symfony\Component\HttpFoundation\Session\Session */ |
56
|
|
|
public static $Session; |
57
|
|
|
|
58
|
|
|
/** @var \Illuminate\Database\Capsule\Manager */ |
59
|
|
|
public static $Database; |
60
|
|
|
|
61
|
|
|
/** @var \Ffcms\Core\Cache\MemoryObject */ |
62
|
|
|
public static $Memory; |
63
|
|
|
|
64
|
|
|
/** @var \Swift_Mailer */ |
65
|
|
|
public static $Mailer; |
66
|
|
|
|
67
|
|
|
/** @var \Ffcms\Core\Interfaces\iCaptcha */ |
68
|
|
|
public static $Captcha; |
69
|
|
|
|
70
|
|
|
/** @var \BasePhpFastCache */ |
71
|
|
|
public static $Cache; |
72
|
|
|
|
73
|
|
|
/** @var EventManager */ |
74
|
|
|
public static $Event; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Prepare entry-point services |
78
|
|
|
* @param array|null $services |
79
|
|
|
* @param bool|object $loader |
80
|
|
|
* @throws NativeException |
81
|
|
|
*/ |
82
|
|
|
public static function init(array $services = null, $loader = false) |
83
|
|
|
{ |
84
|
|
|
// initialize default services - used in all apps type |
85
|
|
|
self::$Memory = MemoryObject::instance(); |
86
|
|
|
self::$Properties = new Properties(); |
87
|
|
|
self::$Request = Request::createFromGlobals(); |
88
|
|
|
self::$Security = new Security(); |
89
|
|
|
self::$Response = new Response(); |
90
|
|
|
self::$View = new View(); |
91
|
|
|
self::$Translate = new Translate(); |
92
|
|
|
self::$Alias = new Alias(); |
93
|
|
|
self::$Event = new EventManager(); |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
// check if debug is enabled and available for current session |
97
|
|
|
if (isset($services['Debug']) && $services['Debug'] === true && Debug::isEnabled() === true) { |
98
|
|
|
self::$Debug = new Debug(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$objects = App::$Properties->getAll('object'); |
102
|
|
|
// pass dynamic initialization |
103
|
|
|
self::dynamicServicePrepare($services, $objects); |
104
|
|
|
|
105
|
|
|
// initialize autoload, pass composer loader and auto-boot of static boot() methods in controllers |
106
|
|
|
self::$Event->makeBoot($loader); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Prepare dynamic services from object anonymous functions |
111
|
|
|
* @param array|null $services |
112
|
|
|
* @param null $objects |
113
|
|
|
* @throws NativeException |
114
|
|
|
*/ |
115
|
|
|
private static function dynamicServicePrepare(array $services = null, $objects = null) |
116
|
|
|
{ |
117
|
|
|
// check if object configuration is passed |
118
|
|
|
if (!Obj::isArray($objects)) { |
119
|
|
|
throw new NativeException('Object configurations is not loaded: /Private/Config/Object.php'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// each all objects as service_name => service_instance() |
123
|
|
|
foreach ($objects as $name => $instance) { |
|
|
|
|
124
|
|
|
// check if definition of object is exist and services list contains it or is null to auto build |
125
|
|
|
if (property_exists(get_called_class(), $name) && $instance instanceof \Closure && (isset($services[$name]) || $services === null)) { |
126
|
|
|
if ($services[$name] === true || $services === null) { // initialize from configs |
127
|
|
|
self::${$name} = $instance(); |
128
|
|
|
} elseif (is_callable($services[$name])) { // raw initialization from App::run() |
129
|
|
|
self::${$name} = $services[$name](); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Run applications and display output |
137
|
|
|
* @throws \DebugBar\DebugBarException |
138
|
|
|
*/ |
139
|
|
|
public static function run() |
140
|
|
|
{ |
141
|
|
|
$html = null; |
142
|
|
|
// lets try to get html full content to page render |
143
|
|
|
try { |
144
|
|
|
/** @var \Ffcms\Core\Arch\Controller $callClass */ |
145
|
|
|
$callClass = null; |
146
|
|
|
$callMethod = 'action' . self::$Request->getAction(); |
147
|
|
|
|
148
|
|
|
// founded callback injection alias |
149
|
|
|
if (self::$Request->getCallbackAlias() !== false) { |
150
|
|
|
$cName = self::$Request->getCallbackAlias(); |
151
|
|
View Code Duplication |
if (class_exists($cName)) { |
|
|
|
|
152
|
|
|
$callClass = new $cName; |
153
|
|
|
} else { |
154
|
|
|
throw new NotFoundException('Callback alias of class "' . App::$Security->strip_tags($cName) . '" is not founded'); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} else { // typical parsing of native apps |
157
|
|
|
$cName = '\Apps\Controller\\' . env_name . '\\' . self::$Request->getController(); |
158
|
|
|
|
159
|
|
|
// try to initialize class object |
160
|
|
View Code Duplication |
if (class_exists($cName)) { |
|
|
|
|
161
|
|
|
$callClass = new $cName; |
162
|
|
|
} else { |
163
|
|
|
throw new NotFoundException('Application can not be runned. Initialized class not founded: ' . App::$Security->strip_tags($cName)); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// try to call method of founded callback class |
168
|
|
|
if (method_exists($callClass, $callMethod)) { |
169
|
|
|
$actionQuery = []; |
170
|
|
|
// prepare action params for callback |
171
|
|
|
if (!Str::likeEmpty(self::$Request->getID())) { |
172
|
|
|
$actionQuery[] = self::$Request->getID(); |
173
|
|
|
if (!Str::likeEmpty(self::$Request->getAdd())) { |
174
|
|
|
$actionQuery[] = self::$Request->getAdd(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// get controller method arguments count |
179
|
|
|
$reflection = new \ReflectionMethod($callClass, $callMethod); |
180
|
|
|
$argumentCount = 0; |
181
|
|
|
foreach ($reflection->getParameters() as $arg) { |
182
|
|
|
if (!$arg->isOptional()) { |
183
|
|
|
$argumentCount++; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// check method arguments count and current request count to prevent warnings |
188
|
|
|
if (count($actionQuery) < $argumentCount) { |
189
|
|
|
throw new NotFoundException(__('Arguments for method %method% is not enough. Expected: %required%, got: %current%.', [ |
190
|
|
|
'method' => $callMethod, |
191
|
|
|
'required' => $argumentCount, |
192
|
|
|
'current' => count($actionQuery) |
193
|
|
|
])); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// make callback call to action in controller and get response |
197
|
|
|
$actionResponse = call_user_func_array([$callClass, $callMethod], $actionQuery); |
198
|
|
|
|
199
|
|
|
if ($actionResponse !== null && !Str::likeEmpty($actionResponse)) { |
200
|
|
|
// set response to controller property object |
201
|
|
|
$callClass->setResponse($actionResponse); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// get full compiled response |
205
|
|
|
$html = $callClass->getOutput(); |
206
|
|
|
} else { |
207
|
|
|
throw new NotFoundException('Method "' . App::$Security->strip_tags($callMethod) . '()" not founded in "' . get_class($callClass) . '"'); |
208
|
|
|
} |
209
|
|
|
} catch (NotFoundException $e) { // catch exceptions and set output |
210
|
|
|
$html = $e->display(); |
211
|
|
|
} catch (ForbiddenException $e) { |
212
|
|
|
$html = $e->display(); |
213
|
|
|
} catch (SyntaxException $e) { |
214
|
|
|
$html = $e->display(); |
215
|
|
|
} catch (JsonException $e) { |
216
|
|
|
$html = $e->display(); |
217
|
|
|
} catch (NativeException $e) { |
218
|
|
|
$html = $e->display(); |
219
|
|
|
} catch (\Exception $e) { // catch all other exceptions |
220
|
|
|
$html = (new NativeException($e->getMessage()))->display(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// set full rendered content to response builder |
224
|
|
|
self::$Response->setContent($html); |
225
|
|
|
// echo full response to user via http foundation |
226
|
|
|
self::$Response->send(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
} |