Skip to content

Commit 2523373

Browse files
committed
#4 Little code adaptation for PHP v5.6
Model::use() renamed to Model::set() AppBuilder::use() renamed to AppBuilder::useAppContext()
1 parent 175f2b8 commit 2523373

File tree

10 files changed

+34
-27
lines changed

10 files changed

+34
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ use PhpMvc\Model;
185185
class AccountController extends Controller {
186186

187187
public function __construct() {
188-
Model::use('join', 'join');
188+
Model::set('join', 'join');
189189

190190
Model::required('join', 'username');
191191
Model::required('join', 'email');

src/AppBuilder.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ public static function useValidation($validators) {
145145
*
146146
* @param callback $callback
147147
* For example:
148-
* AppBuilder::use(function($appContext: AppContext) {
148+
* AppBuilder::useAppContext(function(\PhpMvc\AppContext $appContext) {
149149
* // ...
150150
* });
151151
*
152152
* @return void
153153
*/
154-
public static function use($callback) {
154+
public static function useAppContext($callback) {
155155
self::$config['customHandlers'] = $callback;
156156
}
157157

@@ -210,7 +210,7 @@ public static function routes($routes) {
210210
public static function build() {
211211
try {
212212
self::init();
213-
self::include();
213+
self::dependencies();
214214

215215
$route = self::canRoute();
216216

@@ -259,7 +259,7 @@ private static function init() {
259259

260260
if (isset(self::$config['customHandlers'])) {
261261
if (is_callable(self::$config['customHandlers'])) {
262-
self::$config['customHandlers'](self::$appContext);
262+
call_user_func(self::$config['customHandlers'], self::$appContext);
263263
}
264264
else {
265265
throw new \Exception('Function is expected.');
@@ -884,7 +884,9 @@ private static function validation() {
884884
// request verification token
885885
$antiForgeryTokenForAction = InternalHelper::getStaticPropertyValue('\\PhpMvc\\ValidateAntiForgeryToken', 'enable');
886886
if ($antiForgeryTokenForAction === true || (($validators === true || (!isset($validators['antiForgeryToken']) || $validators['antiForgeryToken'] === true)) && $antiForgeryTokenForAction !== false)) {
887-
if (($request = self::$config['httpContext']->getRequest())->isPost()) {
887+
$request = self::$config['httpContext']->getRequest();
888+
889+
if ($request->isPost()) {
888890
$post = $request->post();
889891
$expected = $request->cookies('__requestVerificationToken');
890892

@@ -936,7 +938,7 @@ private static function validation() {
936938
*
937939
* @return void
938940
*/
939-
private static function include() {
941+
private static function dependencies() {
940942
require_once PHPMVC_CORE_PATH . 'Loader.php';
941943
require_once PHPMVC_CORE_PATH . 'Controller.php';
942944
}
@@ -1092,7 +1094,7 @@ private static function makeActionState($actionContext) {
10921094
unset($postData['__requestVerificationToken']);
10931095
}
10941096

1095-
if ($param->hasType()) {
1097+
if (method_exists($param, 'hasType') && $param->hasType()) {
10961098
if ($param->getType()->isBuiltin()) {
10971099
$arguments[$name] = ($param->isOptional() ? $param->getDefaultValue() : null);
10981100
continue;
@@ -1102,6 +1104,11 @@ private static function makeActionState($actionContext) {
11021104
$paramTypeName = $paramTypeName->getName();
11031105
}
11041106
}
1107+
else {
1108+
if (($paramTypeName = $param->getClass()) !== null) {
1109+
$paramTypeName = $paramTypeName->getName();
1110+
}
1111+
}
11051112

11061113
if (!empty($postData)) {
11071114
InternalHelper::arrayToObject($postData, $post, $paramTypeName);

src/AppContext.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* Represents the application context.
3030
*/
31-
class AppContext {
31+
final class AppContext {
3232

3333
/**
3434
* The config of the application.
@@ -271,10 +271,10 @@ public function add($eventName, $eventHandler, $key = null) {
271271
}
272272

273273
if ($key !== null) {
274-
$this->$eventName[$key] = $eventHandler;
274+
$this->{$eventName}[$key] = $eventHandler;
275275
}
276276
else {
277-
$this->$eventName[] = $eventHandler;
277+
$this->{$eventName}[] = $eventHandler;
278278
}
279279
}
280280

src/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public static function actionLink($linkText, $actionName, $controllerName = null
295295
*/
296296
public static function antiForgeryToken($dynamic = false) {
297297
if (self::$token === null) {
298-
self::$token = bin2hex(random_bytes(64));
298+
self::$token = bin2hex(function_exists('random_bytes') ? random_bytes(64) : uniqid('', true));
299299
$response = self::$viewContext->getHttpContext()->getResponse();
300300
$response->addCookie('__requestVerificationToken', self::$token, 0, '/', '', false, true);
301301
}

src/HttpResponseBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public function getHeaders() {
264264
*
265265
* @return void
266266
*/
267-
public function addCookie(string $name, string $value = '', int $expire = 0, string $path = '', string $domain = '', bool $secure = false, bool $httponly = false) {
267+
public function addCookie($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false, $httponly = false) {
268268
if ($this->canSetHeaders()) {
269269
$this->cookies[] = func_get_args();
270270
}

src/Info.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
*/
3131
final class Info {
3232

33-
const VERSION = '1.1.1';
33+
const VERSION = '1.2.0';
3434

3535
}

src/InternalHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ public static function arrayToObject($array, &$object = null, $type = null) {
264264

265265
if (is_array($value)) {
266266
foreach ($value as $v) {
267-
$activeObject->$key[] = $v;
267+
$activeObject->{$key}[] = $v;
268268
}
269269
}
270270
else {
271-
$activeObject->$key[] = $value;
271+
$activeObject->{$key}[] = $value;
272272
}
273273
}
274274
else {

src/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ final class Model {
5959
*
6060
* @return void
6161
*/
62-
public static function use($actionName, $modelType) {
62+
public static function set($actionName, $modelType) {
6363
if (empty($actionName)) {
6464
throw new \Exception('$actionName must not be empty.');
6565
}

tests/HtmlTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testDisplay(): void {
4343

4444
$viewContext->model = $this->getModelA('Hello, world!');
4545

46-
Model::use('.', 'test');
46+
Model::set('.', 'test');
4747
Model::display('test', 'text', 'Program', 'The program name.');
4848
Model::display('test', 'number', 'Lines', 'The lines of program code.');
4949

@@ -64,7 +64,7 @@ public function testDisplay(): void {
6464

6565
$viewContext->model = $this->getModelB(null, 'Hello, world!');
6666

67-
Model::use('.', 'test');
67+
Model::set('.', 'test');
6868
Model::display('test', array('a', 'text'), 'Program', 'The program name.');
6969
Model::display('test', array('a', 'number'), 'Lines', 'The lines of program code.');
7070

@@ -85,7 +85,7 @@ public function testValidationSummary(): void {
8585

8686
$viewContext->model = $this->getModelA();
8787

88-
Model::use('.', 'test');
88+
Model::set('.', 'test');
8989
Model::required('test', 'text');
9090
Model::validation('test', 'number', function($value, &$errorMessage) {
9191
if ($value != 555) {
@@ -119,7 +119,7 @@ public function testValidationSummary(): void {
119119

120120
$viewContext = $this->setContext('justModel', 'Home', 'POST', array('text' => '', 'number' => 555));
121121

122-
Model::use('.', 'test');
122+
Model::set('.', 'test');
123123
Model::required('test', 'text');
124124
Model::validation('test', 'number', function($value, &$errorMessage) {
125125
if ($value != 555) {
@@ -143,7 +143,7 @@ public function testValidationSummary(): void {
143143

144144
$viewContext = $this->setContext('justModel', 'Home', 'POST', array('text' => 'Hello, world!', 'number' => 555));
145145

146-
Model::use('.', 'test');
146+
Model::set('.', 'test');
147147
Model::required('test', 'text');
148148
Model::required('test', 'number');
149149

@@ -161,7 +161,7 @@ public function testValidationSummary(): void {
161161
$viewContext->model = $this->getModelB();
162162
$viewContext->model->a->number = 123;
163163

164-
Model::use('.', 'test');
164+
Model::set('.', 'test');
165165
Model::required('test', array('a', 'text'));
166166
Model::validation('test', array('a', 'number'), function($value, &$errorMessage) {
167167
if ($value != 555) {
@@ -189,7 +189,7 @@ public function testValidationMessage(): void {
189189

190190
$viewContext->model = $this->getModelA();
191191

192-
Model::use('.', 'test');
192+
Model::set('.', 'test');
193193
Model::required('test', 'text');
194194
Model::validation('test', 'number', function($value, &$errorMessage) {
195195
if ($value != 555) {
@@ -227,7 +227,7 @@ public function testValidationMessage(): void {
227227
$viewContext->model->b1 = new ModelB();
228228
$viewContext->model->b1->a = new ModelA();
229229

230-
Model::use('.', 'test');
230+
Model::set('.', 'test');
231231
Model::required('test', array('b1', 'a', 'text'));
232232
Model::validation('test', array('b1', 'a', 'number'), function($value, &$errorMessage) {
233233
if ($value != 555) {
@@ -267,7 +267,7 @@ public function testValidationMessage(): void {
267267
$viewContext->model->b1->a->text = 'hello, world!';
268268
$viewContext->model->b1->a->number = 555;
269269

270-
Model::use('.', 'test');
270+
Model::set('.', 'test');
271271
Model::required('test', array('b1', 'a', 'text'));
272272
Model::validation('test', array('b1', 'a', 'number'), function($value, &$errorMessage) {
273273
if ($value != 555) {

tests/mvc/controllers/AccountController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class AccountController extends Controller {
1111

1212
public function __construct() {
13-
Model::use('login', 'login');
13+
Model::set('login', 'login');
1414

1515
Model::required('login', 'username');
1616
Model::required('login', 'password', 'Password is required.');

0 commit comments

Comments
 (0)