@@ -10,22 +10,22 @@ |
||
| 10 | 10 | class Exception extends \Exception |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $template = ''; |
|
| 13 | + /** |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $template = ''; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @param string $message |
|
| 20 | - * @param int $code |
|
| 21 | - * @param null|\Exception $previous |
|
| 22 | - */ |
|
| 23 | - public function __construct($message, $code = 0, \Exception $previous = null) |
|
| 24 | - { |
|
| 25 | - if (is_array($message)) { |
|
| 26 | - $message = vsprintf($this->template, $message); |
|
| 27 | - } |
|
| 18 | + /** |
|
| 19 | + * @param string $message |
|
| 20 | + * @param int $code |
|
| 21 | + * @param null|\Exception $previous |
|
| 22 | + */ |
|
| 23 | + public function __construct($message, $code = 0, \Exception $previous = null) |
|
| 24 | + { |
|
| 25 | + if (is_array($message)) { |
|
| 26 | + $message = vsprintf($this->template, $message); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - parent::__construct($message, $code, $previous); |
|
| 30 | - } |
|
| 29 | + parent::__construct($message, $code, $previous); |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -7,8 +7,7 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | * @package Zortje\MVC\Common\Exception |
| 9 | 9 | */ |
| 10 | -class Exception extends \Exception |
|
| 11 | -{ |
|
| 10 | +class Exception extends \Exception { |
|
| 12 | 11 | |
| 13 | 12 | /** |
| 14 | 13 | * @var string |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | * @param int $code |
| 21 | 20 | * @param null|\Exception $previous |
| 22 | 21 | */ |
| 23 | - public function __construct($message, $code = 0, \Exception $previous = null) |
|
| 24 | - { |
|
| 22 | + public function __construct($message, $code = 0, \Exception $previous = null) { |
|
| 25 | 23 | if (is_array($message)) { |
| 26 | 24 | $message = vsprintf($this->template, $message); |
| 27 | 25 | } |
@@ -12,17 +12,17 @@ |
||
| 12 | 12 | class ControllerActionNonexistentException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Controller %s action %s is nonexistent'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Controller %s action %s is nonexistent'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Controller\Exception |
| 11 | 11 | */ |
| 12 | -class ControllerActionNonexistentException extends Exception |
|
| 13 | -{ |
|
| 12 | +class ControllerActionNonexistentException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | |
@@ -12,17 +12,17 @@ |
||
| 12 | 12 | class ControllerActionPrivateInsufficientAuthenticationException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Controller %s private action %s requires authentication'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Controller %s private action %s requires authentication'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Controller\Exception |
| 11 | 11 | */ |
| 12 | -class ControllerActionPrivateInsufficientAuthenticationException extends Exception |
|
| 13 | -{ |
|
| 12 | +class ControllerActionPrivateInsufficientAuthenticationException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | |
@@ -12,17 +12,17 @@ |
||
| 12 | 12 | class ControllerActionProtectedInsufficientAuthenticationException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Controller %s protected action %s requires authentication'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Controller %s protected action %s requires authentication'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Controller\Exception |
| 11 | 11 | */ |
| 12 | -class ControllerActionProtectedInsufficientAuthenticationException extends Exception |
|
| 13 | -{ |
|
| 12 | +class ControllerActionProtectedInsufficientAuthenticationException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | |
@@ -12,17 +12,17 @@ |
||
| 12 | 12 | class ControllerInvalidSuperclassException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Controller %s is not a subclass of Controller'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Controller %s is not a subclass of Controller'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Controller\Exception |
| 11 | 11 | */ |
| 12 | -class ControllerInvalidSuperclassException extends Exception |
|
| 13 | -{ |
|
| 12 | +class ControllerInvalidSuperclassException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | |
@@ -12,17 +12,17 @@ |
||
| 12 | 12 | class ControllerNonexistentException extends Exception |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * {@inheritdoc} |
|
| 17 | - */ |
|
| 18 | - protected $template = 'Controller %s is nonexistent'; |
|
| 15 | + /** |
|
| 16 | + * {@inheritdoc} |
|
| 17 | + */ |
|
| 18 | + protected $template = 'Controller %s is nonexistent'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * {@inheritdoc} |
|
| 22 | - */ |
|
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 25 | - parent::__construct($message); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * {@inheritdoc} |
|
| 22 | + */ |
|
| 23 | + public function __construct($message) |
|
| 24 | + { |
|
| 25 | + parent::__construct($message); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | } |
@@ -9,8 +9,7 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @package Zortje\MVC\Controller\Exception |
| 11 | 11 | */ |
| 12 | -class ControllerNonexistentException extends Exception |
|
| 13 | -{ |
|
| 12 | +class ControllerNonexistentException extends Exception { |
|
| 14 | 13 | |
| 15 | 14 | /** |
| 16 | 15 | * {@inheritdoc} |
@@ -20,8 +19,7 @@ discard block |
||
| 20 | 19 | /** |
| 21 | 20 | * {@inheritdoc} |
| 22 | 21 | */ |
| 23 | - public function __construct($message) |
|
| 24 | - { |
|
| 22 | + public function __construct($message) { |
|
| 25 | 23 | parent::__construct($message); |
| 26 | 24 | } |
| 27 | 25 | |
@@ -16,283 +16,283 @@ |
||
| 16 | 16 | class Controller |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Controller action is publicly accessible |
|
| 21 | - */ |
|
| 22 | - const ACTION_PUBLIC = 0; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Controller action requires authentication |
|
| 26 | - * Will redirect to login page if not authenticated |
|
| 27 | - */ |
|
| 28 | - const ACTION_PROTECTED = 1; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Controller action requires authentication |
|
| 32 | - * Will result in an 404 if not authenticated |
|
| 33 | - */ |
|
| 34 | - const ACTION_PRIVATE = 2; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var array Controller action access rules |
|
| 38 | - */ |
|
| 39 | - protected $access = []; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var \PDO PDO |
|
| 43 | - */ |
|
| 44 | - protected $pdo; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string App file path |
|
| 48 | - */ |
|
| 49 | - protected $appPath; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var null|User User |
|
| 53 | - */ |
|
| 54 | - protected $user; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var string Controller action |
|
| 58 | - */ |
|
| 59 | - protected $action; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var array View variables |
|
| 63 | - */ |
|
| 64 | - protected $variables = []; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var bool Should render view for controller action |
|
| 68 | - */ |
|
| 69 | - protected $render = true; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string File path for layout template file |
|
| 73 | - */ |
|
| 74 | - protected $layout; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var string File path for view template file |
|
| 78 | - */ |
|
| 79 | - protected $view; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var array Headers for output |
|
| 83 | - * |
|
| 84 | - * @todo JSON content type |
|
| 85 | - * |
|
| 86 | - * Content-Type: application/javascript; charset=utf-8 |
|
| 87 | - */ |
|
| 88 | - protected $headers = [ |
|
| 89 | - 'content-type' => 'Content-Type: text/html; charset=utf-8' |
|
| 90 | - ]; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @return string Controller name without namespace |
|
| 94 | - */ |
|
| 95 | - public function getShortName() |
|
| 96 | - { |
|
| 97 | - return str_replace('Controller', null, (new \ReflectionClass($this))->getShortName()); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @param string $action Controller action |
|
| 102 | - * |
|
| 103 | - * @throws ControllerActionNonexistentException |
|
| 104 | - * @throws ControllerActionPrivateInsufficientAuthenticationException |
|
| 105 | - * @throws ControllerActionProtectedInsufficientAuthenticationException |
|
| 106 | - */ |
|
| 107 | - public function setAction($action) |
|
| 108 | - { |
|
| 109 | - /** |
|
| 110 | - * Check if method exists and that access has been defined |
|
| 111 | - */ |
|
| 112 | - if (!method_exists($this, $action) || !isset($this->access[$action])) { |
|
| 113 | - throw new ControllerActionNonexistentException([get_class($this), $action]); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Check controller action access level if user is not authenticated |
|
| 118 | - */ |
|
| 119 | - if (!$this->user) { |
|
| 120 | - if ($this->access[$action] === self::ACTION_PRIVATE) { |
|
| 121 | - throw new ControllerActionPrivateInsufficientAuthenticationException([get_class($this), $action]); |
|
| 122 | - } elseif ($this->access[$action] === self::ACTION_PROTECTED) { |
|
| 123 | - throw new ControllerActionProtectedInsufficientAuthenticationException([get_class($this), $action]); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Set controller action |
|
| 129 | - */ |
|
| 130 | - $this->action = $action; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Call action |
|
| 135 | - * |
|
| 136 | - * @return array<string,array|string>|false Headers and output if render is enabled, otherwise FALSE |
|
| 137 | - * |
|
| 138 | - * @throws \LogicException If controller action is not set |
|
| 139 | - */ |
|
| 140 | - public function callAction() |
|
| 141 | - { |
|
| 142 | - if (!isset($this->action)) { |
|
| 143 | - throw new \LogicException('Controller action must be set before being called'); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Before controller action hook |
|
| 148 | - */ |
|
| 149 | - $this->beforeAction(); |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Call controller action |
|
| 153 | - */ |
|
| 154 | - $action = $this->action; |
|
| 155 | - |
|
| 156 | - $this->$action(); |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * After controller action hook |
|
| 160 | - */ |
|
| 161 | - $this->afterAction(); |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Render view |
|
| 165 | - */ |
|
| 166 | - if ($this->render) { |
|
| 167 | - $render = new HtmlRender($this->variables); |
|
| 168 | - |
|
| 169 | - $output = $render->render(['_view' => $this->getViewTemplate(), '_layout' => $this->getLayoutTemplate()]); |
|
| 170 | - |
|
| 171 | - return [ |
|
| 172 | - 'headers' => $this->headers, |
|
| 173 | - 'output' => $output |
|
| 174 | - ]; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return false; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Before controller action hook |
|
| 182 | - * |
|
| 183 | - * Called right before controller action is called |
|
| 184 | - */ |
|
| 185 | - protected function beforeAction() |
|
| 186 | - { |
|
| 187 | - /** |
|
| 188 | - * Set New Relic transaction name |
|
| 189 | - */ |
|
| 190 | - if (extension_loaded('newrelic')) { |
|
| 191 | - newrelic_name_transaction(sprintf('%s/%s', $this->getShortName(), $this->action)); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * After controller action hook |
|
| 197 | - * |
|
| 198 | - * Called right after controller action is called, but before rendering of the view |
|
| 199 | - */ |
|
| 200 | - protected function afterAction() |
|
| 201 | - { |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Set view variable |
|
| 206 | - * |
|
| 207 | - * @param string $variable |
|
| 208 | - * @param mixed $value |
|
| 209 | - */ |
|
| 210 | - protected function set($variable, $value) |
|
| 211 | - { |
|
| 212 | - $this->variables[$variable] = $value; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Get layout template |
|
| 217 | - * |
|
| 218 | - * @return string Layout template file path |
|
| 219 | - */ |
|
| 220 | - protected function getLayoutTemplate() |
|
| 221 | - { |
|
| 222 | - $layout = $this->layout; |
|
| 223 | - |
|
| 224 | - if (empty($layout)) { |
|
| 225 | - $layout = 'View/Layout/default'; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - return "{$this->appPath}$layout.layout"; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Get view template |
|
| 233 | - * |
|
| 234 | - * @return string View template file path |
|
| 235 | - */ |
|
| 236 | - protected function getViewTemplate() |
|
| 237 | - { |
|
| 238 | - $view = $this->view; |
|
| 239 | - |
|
| 240 | - if (empty($view)) { |
|
| 241 | - $view = sprintf('View/%s/%s', $this->getShortName(), $this->action); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - return "{$this->appPath}$view.view"; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Set response code |
|
| 249 | - * |
|
| 250 | - * Supports 200 OK, 403 Forbidden, 404 Not Found & 500 Internal Server Error |
|
| 251 | - * |
|
| 252 | - * @param int $code HTTP response code |
|
| 253 | - * |
|
| 254 | - * @throws \InvalidArgumentException If unsupported code is provided |
|
| 255 | - */ |
|
| 256 | - protected function setResponseCode($code) |
|
| 257 | - { |
|
| 258 | - switch ($code) { |
|
| 259 | - case 200: |
|
| 260 | - $text = 'OK'; |
|
| 261 | - break; |
|
| 262 | - |
|
| 263 | - case 403: |
|
| 264 | - $text = 'Forbidden'; |
|
| 265 | - break; |
|
| 266 | - |
|
| 267 | - case 404: |
|
| 268 | - $text = 'Not Found'; |
|
| 269 | - break; |
|
| 270 | - |
|
| 271 | - case 500: |
|
| 272 | - $text = 'Internal Server Error'; |
|
| 273 | - break; |
|
| 274 | - |
|
| 275 | - default: |
|
| 276 | - throw new \InvalidArgumentException("HTTP status '$code' is not implemented"); |
|
| 277 | - break; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Set header |
|
| 282 | - */ |
|
| 283 | - // @todo test that running response code multiple times only results in one response code header |
|
| 284 | - $this->headers['response_code'] = "HTTP/1.1 $code $text"; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * @param \PDO $pdo |
|
| 289 | - * @param string $appPath |
|
| 290 | - * @param null|User $user |
|
| 291 | - */ |
|
| 292 | - public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
| 293 | - { |
|
| 294 | - $this->pdo = $pdo; |
|
| 295 | - $this->appPath = $appPath; |
|
| 296 | - $this->user = $user; |
|
| 297 | - } |
|
| 19 | + /** |
|
| 20 | + * Controller action is publicly accessible |
|
| 21 | + */ |
|
| 22 | + const ACTION_PUBLIC = 0; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Controller action requires authentication |
|
| 26 | + * Will redirect to login page if not authenticated |
|
| 27 | + */ |
|
| 28 | + const ACTION_PROTECTED = 1; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Controller action requires authentication |
|
| 32 | + * Will result in an 404 if not authenticated |
|
| 33 | + */ |
|
| 34 | + const ACTION_PRIVATE = 2; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var array Controller action access rules |
|
| 38 | + */ |
|
| 39 | + protected $access = []; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var \PDO PDO |
|
| 43 | + */ |
|
| 44 | + protected $pdo; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string App file path |
|
| 48 | + */ |
|
| 49 | + protected $appPath; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var null|User User |
|
| 53 | + */ |
|
| 54 | + protected $user; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var string Controller action |
|
| 58 | + */ |
|
| 59 | + protected $action; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var array View variables |
|
| 63 | + */ |
|
| 64 | + protected $variables = []; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var bool Should render view for controller action |
|
| 68 | + */ |
|
| 69 | + protected $render = true; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string File path for layout template file |
|
| 73 | + */ |
|
| 74 | + protected $layout; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var string File path for view template file |
|
| 78 | + */ |
|
| 79 | + protected $view; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var array Headers for output |
|
| 83 | + * |
|
| 84 | + * @todo JSON content type |
|
| 85 | + * |
|
| 86 | + * Content-Type: application/javascript; charset=utf-8 |
|
| 87 | + */ |
|
| 88 | + protected $headers = [ |
|
| 89 | + 'content-type' => 'Content-Type: text/html; charset=utf-8' |
|
| 90 | + ]; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @return string Controller name without namespace |
|
| 94 | + */ |
|
| 95 | + public function getShortName() |
|
| 96 | + { |
|
| 97 | + return str_replace('Controller', null, (new \ReflectionClass($this))->getShortName()); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @param string $action Controller action |
|
| 102 | + * |
|
| 103 | + * @throws ControllerActionNonexistentException |
|
| 104 | + * @throws ControllerActionPrivateInsufficientAuthenticationException |
|
| 105 | + * @throws ControllerActionProtectedInsufficientAuthenticationException |
|
| 106 | + */ |
|
| 107 | + public function setAction($action) |
|
| 108 | + { |
|
| 109 | + /** |
|
| 110 | + * Check if method exists and that access has been defined |
|
| 111 | + */ |
|
| 112 | + if (!method_exists($this, $action) || !isset($this->access[$action])) { |
|
| 113 | + throw new ControllerActionNonexistentException([get_class($this), $action]); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Check controller action access level if user is not authenticated |
|
| 118 | + */ |
|
| 119 | + if (!$this->user) { |
|
| 120 | + if ($this->access[$action] === self::ACTION_PRIVATE) { |
|
| 121 | + throw new ControllerActionPrivateInsufficientAuthenticationException([get_class($this), $action]); |
|
| 122 | + } elseif ($this->access[$action] === self::ACTION_PROTECTED) { |
|
| 123 | + throw new ControllerActionProtectedInsufficientAuthenticationException([get_class($this), $action]); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Set controller action |
|
| 129 | + */ |
|
| 130 | + $this->action = $action; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Call action |
|
| 135 | + * |
|
| 136 | + * @return array<string,array|string>|false Headers and output if render is enabled, otherwise FALSE |
|
| 137 | + * |
|
| 138 | + * @throws \LogicException If controller action is not set |
|
| 139 | + */ |
|
| 140 | + public function callAction() |
|
| 141 | + { |
|
| 142 | + if (!isset($this->action)) { |
|
| 143 | + throw new \LogicException('Controller action must be set before being called'); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Before controller action hook |
|
| 148 | + */ |
|
| 149 | + $this->beforeAction(); |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Call controller action |
|
| 153 | + */ |
|
| 154 | + $action = $this->action; |
|
| 155 | + |
|
| 156 | + $this->$action(); |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * After controller action hook |
|
| 160 | + */ |
|
| 161 | + $this->afterAction(); |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Render view |
|
| 165 | + */ |
|
| 166 | + if ($this->render) { |
|
| 167 | + $render = new HtmlRender($this->variables); |
|
| 168 | + |
|
| 169 | + $output = $render->render(['_view' => $this->getViewTemplate(), '_layout' => $this->getLayoutTemplate()]); |
|
| 170 | + |
|
| 171 | + return [ |
|
| 172 | + 'headers' => $this->headers, |
|
| 173 | + 'output' => $output |
|
| 174 | + ]; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return false; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Before controller action hook |
|
| 182 | + * |
|
| 183 | + * Called right before controller action is called |
|
| 184 | + */ |
|
| 185 | + protected function beforeAction() |
|
| 186 | + { |
|
| 187 | + /** |
|
| 188 | + * Set New Relic transaction name |
|
| 189 | + */ |
|
| 190 | + if (extension_loaded('newrelic')) { |
|
| 191 | + newrelic_name_transaction(sprintf('%s/%s', $this->getShortName(), $this->action)); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * After controller action hook |
|
| 197 | + * |
|
| 198 | + * Called right after controller action is called, but before rendering of the view |
|
| 199 | + */ |
|
| 200 | + protected function afterAction() |
|
| 201 | + { |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Set view variable |
|
| 206 | + * |
|
| 207 | + * @param string $variable |
|
| 208 | + * @param mixed $value |
|
| 209 | + */ |
|
| 210 | + protected function set($variable, $value) |
|
| 211 | + { |
|
| 212 | + $this->variables[$variable] = $value; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Get layout template |
|
| 217 | + * |
|
| 218 | + * @return string Layout template file path |
|
| 219 | + */ |
|
| 220 | + protected function getLayoutTemplate() |
|
| 221 | + { |
|
| 222 | + $layout = $this->layout; |
|
| 223 | + |
|
| 224 | + if (empty($layout)) { |
|
| 225 | + $layout = 'View/Layout/default'; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + return "{$this->appPath}$layout.layout"; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Get view template |
|
| 233 | + * |
|
| 234 | + * @return string View template file path |
|
| 235 | + */ |
|
| 236 | + protected function getViewTemplate() |
|
| 237 | + { |
|
| 238 | + $view = $this->view; |
|
| 239 | + |
|
| 240 | + if (empty($view)) { |
|
| 241 | + $view = sprintf('View/%s/%s', $this->getShortName(), $this->action); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + return "{$this->appPath}$view.view"; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Set response code |
|
| 249 | + * |
|
| 250 | + * Supports 200 OK, 403 Forbidden, 404 Not Found & 500 Internal Server Error |
|
| 251 | + * |
|
| 252 | + * @param int $code HTTP response code |
|
| 253 | + * |
|
| 254 | + * @throws \InvalidArgumentException If unsupported code is provided |
|
| 255 | + */ |
|
| 256 | + protected function setResponseCode($code) |
|
| 257 | + { |
|
| 258 | + switch ($code) { |
|
| 259 | + case 200: |
|
| 260 | + $text = 'OK'; |
|
| 261 | + break; |
|
| 262 | + |
|
| 263 | + case 403: |
|
| 264 | + $text = 'Forbidden'; |
|
| 265 | + break; |
|
| 266 | + |
|
| 267 | + case 404: |
|
| 268 | + $text = 'Not Found'; |
|
| 269 | + break; |
|
| 270 | + |
|
| 271 | + case 500: |
|
| 272 | + $text = 'Internal Server Error'; |
|
| 273 | + break; |
|
| 274 | + |
|
| 275 | + default: |
|
| 276 | + throw new \InvalidArgumentException("HTTP status '$code' is not implemented"); |
|
| 277 | + break; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Set header |
|
| 282 | + */ |
|
| 283 | + // @todo test that running response code multiple times only results in one response code header |
|
| 284 | + $this->headers['response_code'] = "HTTP/1.1 $code $text"; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * @param \PDO $pdo |
|
| 289 | + * @param string $appPath |
|
| 290 | + * @param null|User $user |
|
| 291 | + */ |
|
| 292 | + public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
| 293 | + { |
|
| 294 | + $this->pdo = $pdo; |
|
| 295 | + $this->appPath = $appPath; |
|
| 296 | + $this->user = $user; |
|
| 297 | + } |
|
| 298 | 298 | } |
@@ -13,8 +13,7 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | * @package Zortje\MVC\Controller |
| 15 | 15 | */ |
| 16 | -class Controller |
|
| 17 | -{ |
|
| 16 | +class Controller { |
|
| 18 | 17 | |
| 19 | 18 | /** |
| 20 | 19 | * Controller action is publicly accessible |
@@ -92,8 +91,7 @@ discard block |
||
| 92 | 91 | /** |
| 93 | 92 | * @return string Controller name without namespace |
| 94 | 93 | */ |
| 95 | - public function getShortName() |
|
| 96 | - { |
|
| 94 | + public function getShortName() { |
|
| 97 | 95 | return str_replace('Controller', null, (new \ReflectionClass($this))->getShortName()); |
| 98 | 96 | } |
| 99 | 97 | |
@@ -104,8 +102,7 @@ discard block |
||
| 104 | 102 | * @throws ControllerActionPrivateInsufficientAuthenticationException |
| 105 | 103 | * @throws ControllerActionProtectedInsufficientAuthenticationException |
| 106 | 104 | */ |
| 107 | - public function setAction($action) |
|
| 108 | - { |
|
| 105 | + public function setAction($action) { |
|
| 109 | 106 | /** |
| 110 | 107 | * Check if method exists and that access has been defined |
| 111 | 108 | */ |
@@ -137,8 +134,7 @@ discard block |
||
| 137 | 134 | * |
| 138 | 135 | * @throws \LogicException If controller action is not set |
| 139 | 136 | */ |
| 140 | - public function callAction() |
|
| 141 | - { |
|
| 137 | + public function callAction() { |
|
| 142 | 138 | if (!isset($this->action)) { |
| 143 | 139 | throw new \LogicException('Controller action must be set before being called'); |
| 144 | 140 | } |
@@ -182,8 +178,7 @@ discard block |
||
| 182 | 178 | * |
| 183 | 179 | * Called right before controller action is called |
| 184 | 180 | */ |
| 185 | - protected function beforeAction() |
|
| 186 | - { |
|
| 181 | + protected function beforeAction() { |
|
| 187 | 182 | /** |
| 188 | 183 | * Set New Relic transaction name |
| 189 | 184 | */ |
@@ -197,8 +192,7 @@ discard block |
||
| 197 | 192 | * |
| 198 | 193 | * Called right after controller action is called, but before rendering of the view |
| 199 | 194 | */ |
| 200 | - protected function afterAction() |
|
| 201 | - { |
|
| 195 | + protected function afterAction() { |
|
| 202 | 196 | } |
| 203 | 197 | |
| 204 | 198 | /** |
@@ -207,8 +201,7 @@ discard block |
||
| 207 | 201 | * @param string $variable |
| 208 | 202 | * @param mixed $value |
| 209 | 203 | */ |
| 210 | - protected function set($variable, $value) |
|
| 211 | - { |
|
| 204 | + protected function set($variable, $value) { |
|
| 212 | 205 | $this->variables[$variable] = $value; |
| 213 | 206 | } |
| 214 | 207 | |
@@ -217,8 +210,7 @@ discard block |
||
| 217 | 210 | * |
| 218 | 211 | * @return string Layout template file path |
| 219 | 212 | */ |
| 220 | - protected function getLayoutTemplate() |
|
| 221 | - { |
|
| 213 | + protected function getLayoutTemplate() { |
|
| 222 | 214 | $layout = $this->layout; |
| 223 | 215 | |
| 224 | 216 | if (empty($layout)) { |
@@ -233,8 +225,7 @@ discard block |
||
| 233 | 225 | * |
| 234 | 226 | * @return string View template file path |
| 235 | 227 | */ |
| 236 | - protected function getViewTemplate() |
|
| 237 | - { |
|
| 228 | + protected function getViewTemplate() { |
|
| 238 | 229 | $view = $this->view; |
| 239 | 230 | |
| 240 | 231 | if (empty($view)) { |
@@ -253,8 +244,7 @@ discard block |
||
| 253 | 244 | * |
| 254 | 245 | * @throws \InvalidArgumentException If unsupported code is provided |
| 255 | 246 | */ |
| 256 | - protected function setResponseCode($code) |
|
| 257 | - { |
|
| 247 | + protected function setResponseCode($code) { |
|
| 258 | 248 | switch ($code) { |
| 259 | 249 | case 200: |
| 260 | 250 | $text = 'OK'; |
@@ -289,8 +279,7 @@ discard block |
||
| 289 | 279 | * @param string $appPath |
| 290 | 280 | * @param null|User $user |
| 291 | 281 | */ |
| 292 | - public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
| 293 | - { |
|
| 282 | + public function __construct(\PDO $pdo, $appPath, User $user = null) { |
|
| 294 | 283 | $this->pdo = $pdo; |
| 295 | 284 | $this->appPath = $appPath; |
| 296 | 285 | $this->user = $user; |
@@ -14,56 +14,56 @@ |
||
| 14 | 14 | class ControllerFactory |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var \PDO PDO |
|
| 19 | - */ |
|
| 20 | - protected $pdo; |
|
| 17 | + /** |
|
| 18 | + * @var \PDO PDO |
|
| 19 | + */ |
|
| 20 | + protected $pdo; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var string App file path |
|
| 24 | - */ |
|
| 25 | - protected $appPath; |
|
| 22 | + /** |
|
| 23 | + * @var string App file path |
|
| 24 | + */ |
|
| 25 | + protected $appPath; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var null|User User |
|
| 29 | - */ |
|
| 30 | - protected $user; |
|
| 27 | + /** |
|
| 28 | + * @var null|User User |
|
| 29 | + */ |
|
| 30 | + protected $user; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Initialize controller |
|
| 34 | - * |
|
| 35 | - * @param string $controller Controller class name |
|
| 36 | - * |
|
| 37 | - * @return Controller Controller object |
|
| 38 | - * |
|
| 39 | - * @throws ControllerInvalidSuperclassException |
|
| 40 | - * @throws ControllerNonexistentException |
|
| 41 | - */ |
|
| 42 | - public function create($controller) |
|
| 43 | - { |
|
| 44 | - if (!class_exists($controller)) { |
|
| 45 | - throw new ControllerNonexistentException([$controller]); |
|
| 46 | - } elseif (!is_subclass_of($controller, Controller::class)) { |
|
| 47 | - throw new ControllerInvalidSuperclassException([$controller]); |
|
| 48 | - } |
|
| 32 | + /** |
|
| 33 | + * Initialize controller |
|
| 34 | + * |
|
| 35 | + * @param string $controller Controller class name |
|
| 36 | + * |
|
| 37 | + * @return Controller Controller object |
|
| 38 | + * |
|
| 39 | + * @throws ControllerInvalidSuperclassException |
|
| 40 | + * @throws ControllerNonexistentException |
|
| 41 | + */ |
|
| 42 | + public function create($controller) |
|
| 43 | + { |
|
| 44 | + if (!class_exists($controller)) { |
|
| 45 | + throw new ControllerNonexistentException([$controller]); |
|
| 46 | + } elseif (!is_subclass_of($controller, Controller::class)) { |
|
| 47 | + throw new ControllerInvalidSuperclassException([$controller]); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @var Controller $controller |
|
| 52 | - */ |
|
| 53 | - $controller = new $controller($this->pdo, $this->appPath, $this->user); |
|
| 50 | + /** |
|
| 51 | + * @var Controller $controller |
|
| 52 | + */ |
|
| 53 | + $controller = new $controller($this->pdo, $this->appPath, $this->user); |
|
| 54 | 54 | |
| 55 | - return $controller; |
|
| 56 | - } |
|
| 55 | + return $controller; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param \PDO $pdo |
|
| 60 | - * @param string $appPath |
|
| 61 | - * @param null|User $user |
|
| 62 | - */ |
|
| 63 | - public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
| 64 | - { |
|
| 65 | - $this->pdo = $pdo; |
|
| 66 | - $this->appPath = $appPath; |
|
| 67 | - $this->user = $user; |
|
| 68 | - } |
|
| 58 | + /** |
|
| 59 | + * @param \PDO $pdo |
|
| 60 | + * @param string $appPath |
|
| 61 | + * @param null|User $user |
|
| 62 | + */ |
|
| 63 | + public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
| 64 | + { |
|
| 65 | + $this->pdo = $pdo; |
|
| 66 | + $this->appPath = $appPath; |
|
| 67 | + $this->user = $user; |
|
| 68 | + } |
|
| 69 | 69 | } |
@@ -11,8 +11,7 @@ discard block |
||
| 11 | 11 | * |
| 12 | 12 | * @package Zortje\MVC\Controller |
| 13 | 13 | */ |
| 14 | -class ControllerFactory |
|
| 15 | -{ |
|
| 14 | +class ControllerFactory { |
|
| 16 | 15 | |
| 17 | 16 | /** |
| 18 | 17 | * @var \PDO PDO |
@@ -39,8 +38,7 @@ discard block |
||
| 39 | 38 | * @throws ControllerInvalidSuperclassException |
| 40 | 39 | * @throws ControllerNonexistentException |
| 41 | 40 | */ |
| 42 | - public function create($controller) |
|
| 43 | - { |
|
| 41 | + public function create($controller) { |
|
| 44 | 42 | if (!class_exists($controller)) { |
| 45 | 43 | throw new ControllerNonexistentException([$controller]); |
| 46 | 44 | } elseif (!is_subclass_of($controller, Controller::class)) { |
@@ -60,8 +58,7 @@ discard block |
||
| 60 | 58 | * @param string $appPath |
| 61 | 59 | * @param null|User $user |
| 62 | 60 | */ |
| 63 | - public function __construct(\PDO $pdo, $appPath, User $user = null) |
|
| 64 | - { |
|
| 61 | + public function __construct(\PDO $pdo, $appPath, User $user = null) { |
|
| 65 | 62 | $this->pdo = $pdo; |
| 66 | 63 | $this->appPath = $appPath; |
| 67 | 64 | $this->user = $user; |
@@ -10,12 +10,12 @@ |
||
| 10 | 10 | class NotFoundController extends Controller |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - protected $access = [ |
|
| 14 | - 'index' => Controller::ACTION_PUBLIC |
|
| 15 | - ]; |
|
| 13 | + protected $access = [ |
|
| 14 | + 'index' => Controller::ACTION_PUBLIC |
|
| 15 | + ]; |
|
| 16 | 16 | |
| 17 | - protected function index() |
|
| 18 | - { |
|
| 19 | - $this->setResponseCode(404); |
|
| 20 | - } |
|
| 17 | + protected function index() |
|
| 18 | + { |
|
| 19 | + $this->setResponseCode(404); |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -7,15 +7,13 @@ |
||
| 7 | 7 | * |
| 8 | 8 | * @package Zortje\MVC\Controller |
| 9 | 9 | */ |
| 10 | -class NotFoundController extends Controller |
|
| 11 | -{ |
|
| 10 | +class NotFoundController extends Controller { |
|
| 12 | 11 | |
| 13 | 12 | protected $access = [ |
| 14 | 13 | 'index' => Controller::ACTION_PUBLIC |
| 15 | 14 | ]; |
| 16 | 15 | |
| 17 | - protected function index() |
|
| 18 | - { |
|
| 16 | + protected function index() { |
|
| 19 | 17 | $this->setResponseCode(404); |
| 20 | 18 | } |
| 21 | 19 | } |