fastdlabs /
fastD
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @author jan huang <[email protected]> |
||
| 4 | * @copyright 2016 |
||
| 5 | * |
||
| 6 | * @see https://www.github.com/janhuang |
||
| 7 | * @see https://fastdlabs.com |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace FastD; |
||
| 11 | |||
| 12 | use ErrorException; |
||
| 13 | use Exception; |
||
| 14 | use FastD\Config\Config; |
||
| 15 | use FastD\Container\Container; |
||
| 16 | use FastD\Container\ServiceProviderInterface; |
||
| 17 | use FastD\Http\HttpException; |
||
| 18 | use FastD\Http\Response; |
||
| 19 | use FastD\Http\ServerRequest; |
||
| 20 | use FastD\Logger\Logger; |
||
| 21 | use FastD\ServiceProvider\ConfigServiceProvider; |
||
| 22 | use FastD\Utils\EnvironmentObject; |
||
| 23 | use Psr\Http\Message\ResponseInterface; |
||
| 24 | use Psr\Http\Message\ServerRequestInterface; |
||
| 25 | use Symfony\Component\Debug\Exception\FatalThrowableError; |
||
| 26 | use Throwable; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Class Application. |
||
| 30 | */ |
||
| 31 | class Application extends Container |
||
| 32 | { |
||
| 33 | const VERSION = 'v3.2.0'; |
||
| 34 | const MODE_FPM = 1; |
||
| 35 | const MODE_SWOOLE = 2; |
||
| 36 | const MODE_CLI = 3; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Application |
||
| 40 | */ |
||
| 41 | public static $app; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $path; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $name; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $booted = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | protected $mode; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * AppKernel constructor. |
||
| 65 | * |
||
| 66 | * @param $path |
||
| 67 | * @param $mode |
||
| 68 | */ |
||
| 69 | 26 | public function __construct($path, $mode = Application::MODE_FPM) |
|
|
0 ignored issues
–
show
|
|||
| 70 | { |
||
| 71 | 26 | $this->path = $path; |
|
| 72 | |||
| 73 | 26 | $this->mode = $mode; |
|
| 74 | |||
| 75 | 26 | static::$app = $this; |
|
| 76 | |||
| 77 | 26 | $this->add('app', $this); |
|
| 78 | |||
| 79 | 26 | $this->bootstrap(); |
|
| 80 | 26 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | 3 | public function getName() |
|
| 86 | { |
||
| 87 | 3 | return $this->name; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | 2 | public function isBooted() |
|
| 94 | { |
||
| 95 | 2 | return $this->booted; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | 26 | public function getPath() |
|
| 102 | { |
||
| 103 | 26 | return $this->path; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Application bootstrap. |
||
| 108 | */ |
||
| 109 | 26 | public function bootstrap() |
|
| 110 | { |
||
| 111 | 26 | if (!$this->booted) { |
|
| 112 | 26 | $config = load($this->path.'/config/app.php'); |
|
| 113 | 26 | $this->name = $config['name']; |
|
| 114 | |||
| 115 | 26 | date_default_timezone_set(isset($config['timezone']) ? $config['timezone'] : 'UTC'); |
|
| 116 | |||
| 117 | 26 | $this->add('config', new Config($config)); |
|
| 118 | 26 | $this->add('logger', new Logger($this->name)); |
|
| 119 | |||
| 120 | 26 | $this->registerExceptionHandler(); |
|
| 121 | 26 | $this->registerServicesProviders($config['services']); |
|
| 122 | |||
| 123 | 26 | unset($config); |
|
| 124 | 26 | $this->booted = true; |
|
| 125 | } |
||
| 126 | 26 | } |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Application reboot. |
||
| 130 | */ |
||
| 131 | public function reboot() |
||
| 132 | { |
||
| 133 | $this->booted = false; |
||
| 134 | |||
| 135 | $this->bootstrap(); |
||
| 136 | } |
||
| 137 | |||
| 138 | 26 | protected function registerExceptionHandler() |
|
| 139 | { |
||
| 140 | 26 | $level = config()->get('error_reporting', E_ALL); |
|
| 141 | 26 | error_reporting($level); |
|
| 142 | |||
| 143 | 26 | set_exception_handler([$this, 'handleException']); |
|
| 144 | |||
| 145 | 26 | set_error_handler(function ($level, $message, $file, $line) { |
|
| 146 | throw new ErrorException($message, 0, $level, $file, $line); |
||
| 147 | 26 | }, $level); |
|
| 148 | 26 | } |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param ServiceProviderInterface[] $services |
||
| 152 | */ |
||
| 153 | 26 | protected function registerServicesProviders(array $services) |
|
| 154 | { |
||
| 155 | 26 | $this->register(new ConfigServiceProvider()); |
|
| 156 | 26 | foreach ($services as $service) { |
|
| 157 | 26 | $this->register(new $service()); |
|
| 158 | } |
||
| 159 | 26 | } |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param ServerRequestInterface $request |
||
| 163 | * |
||
| 164 | * @return Response|\Symfony\Component\HttpFoundation\Response |
||
| 165 | * |
||
| 166 | * @throws Exception |
||
| 167 | */ |
||
| 168 | 4 | public function handleRequest(ServerRequestInterface $request) |
|
| 169 | { |
||
| 170 | try { |
||
| 171 | 4 | $this->add('request', $request); |
|
| 172 | |||
| 173 | 4 | return $this->get('dispatcher')->dispatch($request); |
|
| 174 | } catch (Exception $exception) { |
||
| 175 | return $this->handleException($exception); |
||
| 176 | } catch (Throwable $exception) { |
||
| 177 | $exception = new FatalThrowableError($exception); |
||
| 178 | |||
| 179 | return $this->handleException($exception); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param Response|\Symfony\Component\HttpFoundation\Response $response |
||
| 185 | */ |
||
| 186 | 2 | public function handleResponse($response) |
|
| 187 | { |
||
| 188 | 2 | $response->send(); |
|
| 189 | 2 | } |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param $e |
||
| 193 | * |
||
| 194 | * @return Response |
||
| 195 | * |
||
| 196 | * @throws FatalThrowableError |
||
| 197 | */ |
||
| 198 | 1 | public function handleException($e) |
|
| 199 | { |
||
| 200 | 1 | if (!$e instanceof Exception) { |
|
| 201 | $e = new FatalThrowableError($e); |
||
| 202 | } |
||
| 203 | |||
| 204 | try { |
||
| 205 | 1 | $trace = call_user_func(config()->get('exception.log'), $e); |
|
| 206 | } catch (Exception $exception) { |
||
| 207 | $trace = [ |
||
| 208 | 'original' => explode("\n", $e->getTraceAsString()), |
||
| 209 | 'handler' => explode("\n", $exception->getTraceAsString()), |
||
| 210 | ]; |
||
| 211 | } |
||
| 212 | |||
| 213 | 1 | logger()->log(Logger::ERROR, $e->getMessage(), $trace); |
|
| 214 | |||
| 215 | 1 | if (Application::MODE_CLI === $this->mode) { |
|
|
0 ignored issues
–
show
As per coding style,
self should be used for accessing local static members.
This check looks for accesses to local static members using the fully qualified name instead
of <?php
class Certificate {
const TRIPLEDES_CBC = 'ASDFGHJKL';
private $key;
public function __construct()
{
$this->key = Certificate::TRIPLEDES_CBC;
}
}
While this is perfectly valid, the fully qualified name of Loading history...
|
|||
| 216 | throw $e; |
||
| 217 | } |
||
| 218 | |||
| 219 | 1 | $status = ($e instanceof HttpException) ? $e->getStatusCode() : $e->getCode(); |
|
| 220 | |||
| 221 | 1 | if (!array_key_exists($status, Response::$statusTexts)) { |
|
| 222 | 1 | $status = Response::HTTP_INTERNAL_SERVER_ERROR; |
|
| 223 | } |
||
| 224 | |||
| 225 | 1 | $resposne = json(call_user_func(config()->get('exception.response'), $e), $status); |
|
| 226 | 1 | if (!$this->isBooted()) { |
|
| 227 | $this->handleResponse($resposne); |
||
| 228 | } |
||
| 229 | |||
| 230 | 1 | return $resposne; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param ServerRequestInterface $request |
||
| 235 | * @param ResponseInterface|\Symfony\Component\HttpFoundation\Response $response |
||
| 236 | * |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | 1 | public function shutdown(ServerRequestInterface $request, $response) |
|
| 240 | { |
||
| 241 | 1 | $this->offsetUnset('request'); |
|
| 242 | 1 | $this->offsetUnset('response'); |
|
| 243 | |||
| 244 | 1 | unset($request, $response); |
|
| 245 | |||
| 246 | 1 | return 0; |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return int |
||
| 251 | * |
||
| 252 | * @throws Exception |
||
| 253 | */ |
||
| 254 | public function run() |
||
| 255 | { |
||
| 256 | $request = ServerRequest::createServerRequestFromGlobals(); |
||
| 257 | |||
| 258 | $response = $this->handleRequest($request); |
||
| 259 | |||
| 260 | $this->handleResponse($response); |
||
| 261 | |||
| 262 | return $this->shutdown($request, $response); |
||
| 263 | } |
||
| 264 | } |
||
| 265 |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.