|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @license MIT |
|
4
|
|
|
* @author Igor Sorokin <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Dspbee\Core; |
|
7
|
|
|
|
|
8
|
|
|
use Dspbee\Bundle\Template\Native; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Process request and get response. |
|
12
|
|
|
* |
|
13
|
|
|
* Class Application |
|
14
|
|
|
* @package Dspbee\Core |
|
15
|
|
|
*/ |
|
16
|
|
|
class Application |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Application constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $packageRoot |
|
22
|
|
|
* @param array $languageList |
|
23
|
|
|
* @param array $packageList |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($packageRoot, array $languageList = [], array $packageList = []) |
|
26
|
|
|
{ |
|
27
|
|
|
$packageRoot = rtrim($packageRoot, '/') . '/'; |
|
28
|
|
|
|
|
29
|
|
|
self::$packagePath = $packageRoot; |
|
30
|
|
|
self::$languageList = $languageList; |
|
31
|
|
|
self::$packageList = $packageList; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Register autoload to app/$package/src dir's. |
|
35
|
|
|
*/ |
|
36
|
|
|
spl_autoload_register(function ($path) use ($packageRoot) { |
|
37
|
|
|
$path = explode('\\', $path); |
|
38
|
|
|
array_shift($path); // Vendor |
|
39
|
|
|
$package = $packageRoot . array_shift($path); // Package |
|
40
|
|
|
$path = $package . '/src/' . implode('/', $path) . '.php'; |
|
41
|
|
|
if (file_exists($path)) { |
|
42
|
|
|
require_once $path; |
|
43
|
|
|
} |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Process request. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $packageClassList |
|
51
|
|
|
* @param null|string $url |
|
52
|
|
|
* |
|
53
|
|
|
* @return Response |
|
54
|
|
|
*/ |
|
55
|
|
|
public function run(array $packageClassList = [], $url = null): Response |
|
56
|
|
|
{ |
|
57
|
|
|
self::$request = new Request(self::$languageList, self::$packageList, $url); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
self::$packagePath .= self::$request->package() . '/'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Process request. |
|
63
|
|
|
*/ |
|
64
|
|
|
if (isset($packageClassList[self::$request->package()])) { |
|
65
|
|
|
/** |
|
66
|
|
|
* Custom routing. |
|
67
|
|
|
*/ |
|
68
|
|
|
/** |
|
69
|
|
|
* Path to router class. |
|
70
|
|
|
*/ |
|
71
|
|
|
$path = self::$packagePath . $packageClassList[self::$request->package()] . '.php'; |
|
72
|
|
|
if (file_exists($path)) { |
|
73
|
|
|
require $path; |
|
74
|
|
|
/** |
|
75
|
|
|
* Name of router class. |
|
76
|
|
|
*/ |
|
77
|
|
|
$route = self::$request->package() . '\\' . $packageClassList[self::$request->package()]; |
|
78
|
|
|
/** |
|
79
|
|
|
* @var DefaultRoute $route |
|
80
|
|
|
*/ |
|
81
|
|
|
$route = new $route(self::$packagePath, self::$request); |
|
82
|
|
|
if (null !== $route->getResponse()) { |
|
83
|
|
|
return $route->getResponse(); |
|
84
|
|
|
} |
|
85
|
|
|
} else { |
|
86
|
|
|
throw new \RuntimeException(sprintf('The file "%s" does not exist', $path)); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$response = (new DefaultRoute(self::$packagePath, self::$request))->getResponse(); |
|
91
|
|
|
if (null !== $response) { |
|
92
|
|
|
return $response; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* If not found. |
|
98
|
|
|
*/ |
|
99
|
|
|
$response = new Response(); |
|
100
|
|
|
$response->headerStatus(404); |
|
101
|
|
|
|
|
102
|
|
|
$content = '404 Not Found'; |
|
103
|
|
|
if (file_exists(self::$packagePath . '/view/404.html.php')) { |
|
104
|
|
|
$content = (new Native(self::$packagePath))->getContent('404.html.php'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$response->setContent($content); |
|
108
|
|
|
|
|
109
|
|
|
return $response; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public static $packagePath = ''; |
|
113
|
|
|
public static $languageList = []; |
|
114
|
|
|
public static $packageList = []; |
|
115
|
|
|
/** |
|
116
|
|
|
* @var null|Request |
|
117
|
|
|
*/ |
|
118
|
|
|
public static $request = null; |
|
119
|
|
|
/** |
|
120
|
|
|
* @var null|\mysqli |
|
121
|
|
|
*/ |
|
122
|
|
|
public static $mysql = null; |
|
123
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.