Skip to content

Commit

Permalink
#4 Little code adaptation for PHP v5.6
Browse files Browse the repository at this point in the history
Model::use() renamed to Model::set()
AppBuilder::use() renamed to AppBuilder::useAppContext()
  • Loading branch information
meet-aleksey committed May 29, 2018
1 parent 175f2b8 commit 2523373
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
21 changes: 14 additions & 7 deletions src/AppBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -210,7 +210,7 @@ public static function routes($routes) {
public static function build() {
try {
self::init();
self::include();
self::dependencies();

$route = self::canRoute();

Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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';
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/AppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* Represents the application context.
*/
class AppContext {
final class AppContext {

/**
* The config of the application.
Expand Down Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/HttpResponseBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
*/
final class Info {

const VERSION = '1.1.1';
const VERSION = '1.2.0';

}
4 changes: 2 additions & 2 deletions src/InternalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
18 changes: 9 additions & 9 deletions tests/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');

Expand All @@ -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.');

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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');

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/mvc/controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down

0 comments on commit 2523373

Please sign in to comment.