From 2523373c1cd3d77da73126a7ed60311b6e251d3a Mon Sep 17 00:00:00 2001 From: Aleksey Date: Tue, 29 May 2018 22:21:42 +0300 Subject: [PATCH] #4 Little code adaptation for PHP v5.6 Model::use() renamed to Model::set() AppBuilder::use() renamed to AppBuilder::useAppContext() --- README.md | 2 +- src/AppBuilder.php | 21 ++++++++++++++------- src/AppContext.php | 6 +++--- src/Html.php | 2 +- src/HttpResponseBase.php | 2 +- src/Info.php | 2 +- src/InternalHelper.php | 4 ++-- src/Model.php | 2 +- tests/HtmlTest.php | 18 +++++++++--------- tests/mvc/controllers/AccountController.php | 2 +- 10 files changed, 34 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 4efe38f..03851ec 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ use PhpMvc\Model; class AccountController extends Controller { public function __construct() { - Model::use('join', 'join'); + Model::set('join', 'join'); Model::required('join', 'username'); Model::required('join', 'email'); diff --git a/src/AppBuilder.php b/src/AppBuilder.php index b1de8a0..0707fa3 100644 --- a/src/AppBuilder.php +++ b/src/AppBuilder.php @@ -145,13 +145,13 @@ public static function useValidation($validators) { * * @param callback $callback * For example: - * AppBuilder::use(function($appContext: AppContext) { + * AppBuilder::useAppContext(function(\PhpMvc\AppContext $appContext) { * // ... * }); * * @return void */ - public static function use($callback) { + public static function useAppContext($callback) { self::$config['customHandlers'] = $callback; } @@ -210,7 +210,7 @@ public static function routes($routes) { public static function build() { try { self::init(); - self::include(); + self::dependencies(); $route = self::canRoute(); @@ -259,7 +259,7 @@ private static function init() { if (isset(self::$config['customHandlers'])) { if (is_callable(self::$config['customHandlers'])) { - self::$config['customHandlers'](self::$appContext); + call_user_func(self::$config['customHandlers'], self::$appContext); } else { throw new \Exception('Function is expected.'); @@ -884,7 +884,9 @@ private static function validation() { // request verification token $antiForgeryTokenForAction = InternalHelper::getStaticPropertyValue('\\PhpMvc\\ValidateAntiForgeryToken', 'enable'); if ($antiForgeryTokenForAction === true || (($validators === true || (!isset($validators['antiForgeryToken']) || $validators['antiForgeryToken'] === true)) && $antiForgeryTokenForAction !== false)) { - if (($request = self::$config['httpContext']->getRequest())->isPost()) { + $request = self::$config['httpContext']->getRequest(); + + if ($request->isPost()) { $post = $request->post(); $expected = $request->cookies('__requestVerificationToken'); @@ -936,7 +938,7 @@ private static function validation() { * * @return void */ - private static function include() { + private static function dependencies() { require_once PHPMVC_CORE_PATH . 'Loader.php'; require_once PHPMVC_CORE_PATH . 'Controller.php'; } @@ -1092,7 +1094,7 @@ private static function makeActionState($actionContext) { unset($postData['__requestVerificationToken']); } - if ($param->hasType()) { + if (method_exists($param, 'hasType') && $param->hasType()) { if ($param->getType()->isBuiltin()) { $arguments[$name] = ($param->isOptional() ? $param->getDefaultValue() : null); continue; @@ -1102,6 +1104,11 @@ private static function makeActionState($actionContext) { $paramTypeName = $paramTypeName->getName(); } } + else { + if (($paramTypeName = $param->getClass()) !== null) { + $paramTypeName = $paramTypeName->getName(); + } + } if (!empty($postData)) { InternalHelper::arrayToObject($postData, $post, $paramTypeName); diff --git a/src/AppContext.php b/src/AppContext.php index db1622a..0829d53 100644 --- a/src/AppContext.php +++ b/src/AppContext.php @@ -28,7 +28,7 @@ /** * Represents the application context. */ -class AppContext { +final class AppContext { /** * The config of the application. @@ -271,10 +271,10 @@ public function add($eventName, $eventHandler, $key = null) { } if ($key !== null) { - $this->$eventName[$key] = $eventHandler; + $this->{$eventName}[$key] = $eventHandler; } else { - $this->$eventName[] = $eventHandler; + $this->{$eventName}[] = $eventHandler; } } diff --git a/src/Html.php b/src/Html.php index 275b5cc..2ba6620 100644 --- a/src/Html.php +++ b/src/Html.php @@ -295,7 +295,7 @@ public static function actionLink($linkText, $actionName, $controllerName = null */ public static function antiForgeryToken($dynamic = false) { if (self::$token === null) { - self::$token = bin2hex(random_bytes(64)); + self::$token = bin2hex(function_exists('random_bytes') ? random_bytes(64) : uniqid('', true)); $response = self::$viewContext->getHttpContext()->getResponse(); $response->addCookie('__requestVerificationToken', self::$token, 0, '/', '', false, true); } diff --git a/src/HttpResponseBase.php b/src/HttpResponseBase.php index 72fa621..704255f 100644 --- a/src/HttpResponseBase.php +++ b/src/HttpResponseBase.php @@ -264,7 +264,7 @@ public function getHeaders() { * * @return void */ - public function addCookie(string $name, string $value = '', int $expire = 0, string $path = '', string $domain = '', bool $secure = false, bool $httponly = false) { + public function addCookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false, $httponly = false) { if ($this->canSetHeaders()) { $this->cookies[] = func_get_args(); } diff --git a/src/Info.php b/src/Info.php index aaa9241..9ef54ca 100644 --- a/src/Info.php +++ b/src/Info.php @@ -30,6 +30,6 @@ */ final class Info { - const VERSION = '1.1.1'; + const VERSION = '1.2.0'; } \ No newline at end of file diff --git a/src/InternalHelper.php b/src/InternalHelper.php index 3c81ae0..0417d5e 100644 --- a/src/InternalHelper.php +++ b/src/InternalHelper.php @@ -264,11 +264,11 @@ public static function arrayToObject($array, &$object = null, $type = null) { if (is_array($value)) { foreach ($value as $v) { - $activeObject->$key[] = $v; + $activeObject->{$key}[] = $v; } } else { - $activeObject->$key[] = $value; + $activeObject->{$key}[] = $value; } } else { diff --git a/src/Model.php b/src/Model.php index d65f4e6..198afdd 100644 --- a/src/Model.php +++ b/src/Model.php @@ -59,7 +59,7 @@ final class Model { * * @return void */ - public static function use($actionName, $modelType) { + public static function set($actionName, $modelType) { if (empty($actionName)) { throw new \Exception('$actionName must not be empty.'); } diff --git a/tests/HtmlTest.php b/tests/HtmlTest.php index c6ab5ab..062ba43 100644 --- a/tests/HtmlTest.php +++ b/tests/HtmlTest.php @@ -43,7 +43,7 @@ public function testDisplay(): void { $viewContext->model = $this->getModelA('Hello, world!'); - Model::use('.', 'test'); + Model::set('.', 'test'); Model::display('test', 'text', 'Program', 'The program name.'); Model::display('test', 'number', 'Lines', 'The lines of program code.'); @@ -64,7 +64,7 @@ public function testDisplay(): void { $viewContext->model = $this->getModelB(null, 'Hello, world!'); - Model::use('.', 'test'); + Model::set('.', 'test'); Model::display('test', array('a', 'text'), 'Program', 'The program name.'); Model::display('test', array('a', 'number'), 'Lines', 'The lines of program code.'); @@ -85,7 +85,7 @@ public function testValidationSummary(): void { $viewContext->model = $this->getModelA(); - Model::use('.', 'test'); + Model::set('.', 'test'); Model::required('test', 'text'); Model::validation('test', 'number', function($value, &$errorMessage) { if ($value != 555) { @@ -119,7 +119,7 @@ public function testValidationSummary(): void { $viewContext = $this->setContext('justModel', 'Home', 'POST', array('text' => '', 'number' => 555)); - Model::use('.', 'test'); + Model::set('.', 'test'); Model::required('test', 'text'); Model::validation('test', 'number', function($value, &$errorMessage) { if ($value != 555) { @@ -143,7 +143,7 @@ public function testValidationSummary(): void { $viewContext = $this->setContext('justModel', 'Home', 'POST', array('text' => 'Hello, world!', 'number' => 555)); - Model::use('.', 'test'); + Model::set('.', 'test'); Model::required('test', 'text'); Model::required('test', 'number'); @@ -161,7 +161,7 @@ public function testValidationSummary(): void { $viewContext->model = $this->getModelB(); $viewContext->model->a->number = 123; - Model::use('.', 'test'); + Model::set('.', 'test'); Model::required('test', array('a', 'text')); Model::validation('test', array('a', 'number'), function($value, &$errorMessage) { if ($value != 555) { @@ -189,7 +189,7 @@ public function testValidationMessage(): void { $viewContext->model = $this->getModelA(); - Model::use('.', 'test'); + Model::set('.', 'test'); Model::required('test', 'text'); Model::validation('test', 'number', function($value, &$errorMessage) { if ($value != 555) { @@ -227,7 +227,7 @@ public function testValidationMessage(): void { $viewContext->model->b1 = new ModelB(); $viewContext->model->b1->a = new ModelA(); - Model::use('.', 'test'); + Model::set('.', 'test'); Model::required('test', array('b1', 'a', 'text')); Model::validation('test', array('b1', 'a', 'number'), function($value, &$errorMessage) { if ($value != 555) { @@ -267,7 +267,7 @@ public function testValidationMessage(): void { $viewContext->model->b1->a->text = 'hello, world!'; $viewContext->model->b1->a->number = 555; - Model::use('.', 'test'); + Model::set('.', 'test'); Model::required('test', array('b1', 'a', 'text')); Model::validation('test', array('b1', 'a', 'number'), function($value, &$errorMessage) { if ($value != 555) { diff --git a/tests/mvc/controllers/AccountController.php b/tests/mvc/controllers/AccountController.php index 993f5b7..3180c2b 100644 --- a/tests/mvc/controllers/AccountController.php +++ b/tests/mvc/controllers/AccountController.php @@ -10,7 +10,7 @@ class AccountController extends Controller { public function __construct() { - Model::use('login', 'login'); + Model::set('login', 'login'); Model::required('login', 'username'); Model::required('login', 'password', 'Password is required.');