bluzphp /
skeleton
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @copyright Bluz PHP Team |
||
| 5 | * @link https://github.com/bluzphp/skeleton |
||
| 6 | */ |
||
| 7 | |||
| 8 | declare(strict_types=1); |
||
| 9 | |||
| 10 | namespace Application; |
||
| 11 | |||
| 12 | use Application\Auth; |
||
| 13 | use Bluz\Application\Application; |
||
|
0 ignored issues
–
show
|
|||
| 14 | use Bluz\Auth\AuthException; |
||
|
0 ignored issues
–
show
The type
Bluz\Auth\AuthException was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 15 | use Bluz\Controller\Controller; |
||
|
0 ignored issues
–
show
The type
Bluz\Controller\Controller was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 16 | use Bluz\Http\Exception\ForbiddenException; |
||
|
0 ignored issues
–
show
The type
Bluz\Http\Exception\ForbiddenException was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 17 | use Bluz\Http\Exception\RedirectException; |
||
|
0 ignored issues
–
show
The type
Bluz\Http\Exception\RedirectException was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 18 | use Bluz\Proxy\Auth as AuthProxy; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Auth was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 19 | use Bluz\Proxy\Config; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Config was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 20 | use Bluz\Proxy\Layout; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Layout was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 21 | use Bluz\Proxy\Logger; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Logger was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 22 | use Bluz\Proxy\Messages; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Messages was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 23 | use Bluz\Proxy\Request; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Request was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 24 | use Bluz\Proxy\Response; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Response was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 25 | use Bluz\Proxy\Router; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Router was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 26 | use Bluz\Proxy\Session; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Session was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 27 | use Bluz\Proxy\Translator; |
||
|
0 ignored issues
–
show
The type
Bluz\Proxy\Translator was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 28 | |||
| 29 | /** |
||
| 30 | * Bootstrap |
||
| 31 | * |
||
| 32 | * @category Application |
||
| 33 | * @package Bootstrap |
||
| 34 | */ |
||
| 35 | class Bootstrap extends Application |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | protected function initTranslator(): void |
||
| 41 | { |
||
| 42 | $translator = new \Bluz\Translator\Translator(); |
||
|
0 ignored issues
–
show
The type
Bluz\Translator\Translator was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 43 | $translator->setOptions(Config::get('translator')); |
||
| 44 | |||
| 45 | // supported locales |
||
| 46 | // en_US, ru_RU, uk_UA |
||
| 47 | $locales = [ |
||
| 48 | 'en' => 'en_US', |
||
| 49 | 'ru' => 'ru_RU', |
||
| 50 | 'uk' => 'uk_UA', |
||
| 51 | ]; |
||
| 52 | |||
| 53 | // try to check locale from cookies |
||
| 54 | $locale = Request::getCookie('locale'); |
||
| 55 | |||
| 56 | // try to get locale from browser |
||
| 57 | if (!$locale) { |
||
| 58 | $languages = Request::getAcceptLanguage(); |
||
| 59 | foreach ($languages as $language => $priority) { |
||
| 60 | if (array_key_exists($language, $locales)) { |
||
| 61 | $locale = $language; |
||
| 62 | break; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | // normalize locale |
||
| 68 | if (array_key_exists($locale, $locales)) { |
||
| 69 | $translator->setLocale($locales[$locale]); |
||
| 70 | } |
||
| 71 | |||
| 72 | $translator->init(); |
||
| 73 | |||
| 74 | Translator::setInstance($translator); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | * |
||
| 80 | * @param Controller $controller |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | * @throws AuthException |
||
| 84 | * @throws Exception |
||
| 85 | * @throws \Bluz\Db\Exception\DbException |
||
| 86 | * @throws \Bluz\Db\Exception\InvalidPrimaryKeyException |
||
| 87 | */ |
||
| 88 | protected function preDispatch($controller): void |
||
| 89 | { |
||
| 90 | // example of setup default title |
||
| 91 | Layout::title('Bluz Skeleton'); |
||
| 92 | |||
| 93 | if (!AuthProxy::getIdentity() && $controller->getModule() !== Router::getErrorModule()) { |
||
| 94 | if ($token = Request::getCookie('Auth-Token')) { |
||
| 95 | // try to login by token from cookies |
||
| 96 | try { |
||
| 97 | Auth\Provider\Cookie::authenticate($token); |
||
| 98 | } catch (AuthException $e) { |
||
| 99 | $this->getResponse()->setCookie('Auth-Token', '', 1, '/'); |
||
| 100 | } |
||
| 101 | } elseif ($token = Request::getHeader('Auth-Token')) { |
||
| 102 | // try to login by token from headers |
||
| 103 | Auth\Provider\Token::authenticate($token); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | parent::preDispatch($controller); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Denied access |
||
| 111 | * |
||
| 112 | * @param ForbiddenException $exception |
||
| 113 | * |
||
| 114 | * @return \Bluz\Controller\Controller|null |
||
| 115 | */ |
||
| 116 | public function forbidden(ForbiddenException $exception): ?Controller |
||
| 117 | { |
||
| 118 | // for AJAX and API calls (over JSON) |
||
| 119 | $jsonOrApi = Request::isXmlHttpRequest() |
||
| 120 | || (Request::checkAccept([Request::TYPE_HTML, Request::TYPE_JSON]) === Request::TYPE_JSON); |
||
| 121 | |||
| 122 | // for guest, for requests |
||
| 123 | if (!$jsonOrApi && !AuthProxy::getIdentity()) { |
||
| 124 | // save URL to session and redirect make sense if presentation is null |
||
| 125 | Session::set('rollback', Request::getUri()->__toString()); |
||
| 126 | // add error notice |
||
| 127 | Messages::addError('You don\'t have permissions, please sign in'); |
||
| 128 | // redirect to Sign In page |
||
| 129 | $redirect = new RedirectException(); |
||
| 130 | $redirect->setUrl(Router::getUrl('users', 'signin')); |
||
| 131 | return $this->redirect($redirect); |
||
| 132 | } |
||
| 133 | return $this->error($exception); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Render with debug headers |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | public function render(): void |
||
| 142 | { |
||
| 143 | Logger::info('app:render'); |
||
| 144 | Logger::info('app:files:' . count(get_included_files())); |
||
| 145 | parent::render(); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Finish it |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function end(): void |
||
| 154 | { |
||
| 155 | if ($errors = Logger::get('error')) { |
||
| 156 | $this->sendErrors($errors); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * sendErrorBody |
||
| 162 | * |
||
| 163 | * @param array $errors |
||
| 164 | * |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | protected function sendErrors($errors): void |
||
| 168 | { |
||
| 169 | foreach ($errors as $message) { |
||
| 170 | errorLog(new \ErrorException($message, 0, E_USER_ERROR)); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths