diff --git a/src/Linna/DI/DIResolver.php b/src/Linna/DI/DIResolver.php index 7c50eda1..8a9c14df 100644 --- a/src/Linna/DI/DIResolver.php +++ b/src/Linna/DI/DIResolver.php @@ -61,13 +61,13 @@ public function rules(array $rules) public function resolve(string $class, array $rules = []) { $this->rules = array_merge($this->rules, $rules); - - $class = (strpos($class, '\\') !== 0) ? '\\' . $class : $class; - + + $class = (strpos($class, '\\') !== 0) ? '\\'.$class : $class; + $this->buildTree($class); - + $this->buildObjects(); - + return $this->cache[$class] ?? null; } @@ -153,7 +153,7 @@ private function buildTree(string $class) private function buildObjects() { //deep dependency level, start to array end for not use array_reverse - for($i = count($this->dependencyTree) - 1; $i >= 0; $i--) { + for ($i = count($this->dependencyTree) - 1; $i >= 0; $i--) { //class foreach ($this->dependencyTree[$i] as $class => $dependency) { diff --git a/src/Linna/Http/FrontController.php b/src/Linna/Http/FrontController.php index 210fcd3d..40271f84 100644 --- a/src/Linna/Http/FrontController.php +++ b/src/Linna/Http/FrontController.php @@ -39,17 +39,17 @@ class FrontController * @var object Contain controller object */ private $route; - + /** * @var string Contain Controller and View action name */ private $routeAction; - + /** * @var array Paremeter passed to Controller */ private $routeParam; - + /** * Constructor. * @@ -61,10 +61,10 @@ class FrontController public function __construct(RouteInterface $route, Model $model, View $view, Controller $controller) { $this->route = $route; - + $this->routeAction = $route->getAction(); $this->routeParam = $route->getParam(); - + $this->model = $model; $this->view = $view; $this->controller = $controller; @@ -80,22 +80,22 @@ public function run() //run action before controller $this->beforeAfterControllerAction('before'); - + //run controller $this->runController(); //run action after controller $this->beforeAfterControllerAction('after'); - + //notify model changes to view $this->model->notify(); //run view $this->runView(); } - + /** - * Run action before or after controller execution + * Run action before or after controller execution. */ private function beforeAfterControllerAction(string $when) { @@ -103,15 +103,14 @@ private function beforeAfterControllerAction(string $when) if (method_exists($this->controller, $when)) { $this->controller->before(); } - + $actionMethod = $when.ucfirst($this->routeAction); - - if (method_exists($this->controller, $actionMethod) && $actionMethod !== $when) - { + + if (method_exists($this->controller, $actionMethod) && $actionMethod !== $when) { call_user_func([$this->controller, $actionMethod]); } } - + /** * Run controller. */ @@ -124,6 +123,7 @@ private function runController() //action - call controller passing params if (count($routeParam) > 0 && $routeAction !== '') { call_user_func_array([$this->controller, $routeAction], $routeParam); + return; } diff --git a/tests/FOO/FOOController.php b/tests/FOO/FOOController.php index 0ffe0ef4..7d74a2e7 100644 --- a/tests/FOO/FOOController.php +++ b/tests/FOO/FOOController.php @@ -19,12 +19,12 @@ public function __construct(FOOModel $model) { parent::__construct($model); } - + public function modifyData() { $this->model->modifyData(); } - + public function modifyDataFromParam($passedData) { $this->model->modifyDataFromParam($passedData); diff --git a/tests/FOO/FOOControllerBeforeAfter.php b/tests/FOO/FOOControllerBeforeAfter.php index 47c5ae7c..8311aa1e 100644 --- a/tests/FOO/FOOControllerBeforeAfter.php +++ b/tests/FOO/FOOControllerBeforeAfter.php @@ -24,7 +24,7 @@ public function beforeModifyDataTimed() { $this->model->addToData('before'); } - + public function modifyDataTimed() { $this->model->modifyDataTimed(); @@ -34,7 +34,7 @@ public function afterModifyDataTimed() { $this->model->addToData('after'); } - + public function before() { //do nothing diff --git a/tests/FOO/FOOModel.php b/tests/FOO/FOOModel.php index 424c776b..55615add 100644 --- a/tests/FOO/FOOModel.php +++ b/tests/FOO/FOOModel.php @@ -22,20 +22,20 @@ public function __construct() public function addToData(string $when) { - if (!isset($this->getUpdate['data'])) - { + if (!isset($this->getUpdate['data'])) { $this->getUpdate = ['data' => 100]; + return; } - + $this->getUpdate['data'] += 3; } - + public function modifyDataTimed() { $this->getUpdate['data'] += 20; } - + public function modifyData() { $this->getUpdate = ['data' => 1234]; diff --git a/tests/FOO/FOOView.php b/tests/FOO/FOOView.php index 148cfa74..2bcfbb64 100644 --- a/tests/FOO/FOOView.php +++ b/tests/FOO/FOOView.php @@ -33,7 +33,7 @@ public function modifyData() { //$this->template = $this->htmlTemplate; } - + public function modifyDataTimed() { //$this->template = $this->htmlTemplate; diff --git a/tests/Http/FrontControllerTest.php b/tests/Http/FrontControllerTest.php index cecfb0f3..9dd43c27 100644 --- a/tests/Http/FrontControllerTest.php +++ b/tests/Http/FrontControllerTest.php @@ -23,7 +23,7 @@ class FrontControllerTest extends TestCase protected $routes; protected $router; - + public function setUp() { $routes = []; @@ -37,7 +37,7 @@ public function setUp() 'controller' => 'FOOController', 'action' => '', ]; - + $routes[] = [ 'name' => 'Foo', 'method' => 'GET', @@ -47,7 +47,7 @@ public function setUp() 'controller' => 'FOOController', 'action' => '', ]; - + $routes[] = [ 'name' => 'Foo', 'method' => 'GET', @@ -57,8 +57,8 @@ public function setUp() 'controller' => 'FOOController', 'action' => '', ]; - - $routes[] = [ + + $routes[] = [ 'name' => 'Foo', 'method' => 'GET', 'url' => '/Foo/(modifyDataTimed)', @@ -67,7 +67,7 @@ public function setUp() 'controller' => 'FOOControllerBeforeAfter', 'action' => '', ]; - + //start router $this->router = new Router($routes, [ 'basePath' => '/', @@ -193,8 +193,8 @@ public function testModelDetach() $this->assertInstanceOf(stdClass::class, $test); $this->assertEquals(false, isset($test->data)); } - - /** + + /** * @depends testNewFrontController */ public function testRunFrontControllerWithActions() @@ -217,9 +217,9 @@ public function testRunFrontControllerWithActions() $FrontController->response(); $test = json_decode(ob_get_contents()); - + var_dump($test); - + ob_end_clean(); $this->assertInstanceOf(stdClass::class, $test);