Skip to content

Commit 528c1b8

Browse files
committed
Add typed controller properties and simplify layout handling
1 parent cd6350d commit 528c1b8

7 files changed

Lines changed: 31 additions & 31 deletions

File tree

src/Controller.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract class Controller implements RequestHandlerInterface
4040
*
4141
* @var string
4242
*/
43-
public $id = '';
43+
public string $id = '';
4444

4545
/**
4646
* The name of the layout to be applied to this controller's views.
@@ -49,28 +49,28 @@ abstract class Controller implements RequestHandlerInterface
4949
* Defaults to null, meaning the actual layout value should inherit that from module's layout value.
5050
* If false, no layout will be applied.
5151
*
52-
* @var null|string|false
52+
* @var string|false
5353
*/
54-
public $layout;
54+
public string|false $layout = false;
5555

5656
/**
5757
* The root directory that contains view files for this controller.
5858
*
5959
* @var string
6060
*/
61-
public $viewPath;
61+
public string $viewPath;
6262

6363
/**
6464
* The module that this controller belongs to.
6565
*
6666
* @var Module
6767
*/
68-
public $module;
68+
public Module $module;
6969

7070
/**
71-
* @var ViewInterface
71+
* @var ViewInterface|null
7272
*/
73-
protected $view;
73+
protected ?ViewInterface $view = null;
7474

7575
/**
7676
* The dependency injection container, injected by Module::createController().
@@ -90,13 +90,13 @@ abstract class Controller implements RequestHandlerInterface
9090
*
9191
* @var ServerRequestInterface
9292
*/
93-
protected $request;
93+
protected ServerRequestInterface $request;
9494

9595
/**
9696
*
9797
* @var ResponseInterface
9898
*/
99-
protected $response;
99+
protected ResponseInterface $response;
100100

101101
public function handle(ServerRequestInterface $request): ResponseInterface
102102
{
@@ -140,15 +140,12 @@ private function runAction(string $id, array $params = []): ResponseInterface
140140

141141
$view = $this->getView();
142142

143-
if ($view instanceof View) {
143+
if ($view instanceof View && $this->layout !== false) {
144144
$app = $this->module->getApplication();
145-
146-
if ($this->layout !== false) {
147-
$layout = $this->layout === null ? $app->defaultLayout : $this->layout;
148-
$path = $this->module->layoutPath ?? $app->defaultLayoutPath ;
149-
$view->paths[] = $path;
150-
$response = $view->render($layout, ['content' => $response]);
151-
}
145+
$layout = $this->layout == false ? $app->defaultLayout : $this->layout;
146+
$path = $this->module->layoutPath ?? $app->defaultLayoutPath ;
147+
$view->paths[] = $path;
148+
$response = $view->render($layout, ['content' => $response]);
152149
}
153150

154151
$response = $this->response->withBody((new StreamFactory())->createStream($response));
@@ -172,7 +169,7 @@ private function getMethodArguments(string $methodName, array $data = []): array
172169
$actionParams = [];
173170

174171
foreach ($method->getParameters() as $param) {
175-
/* @var $param \ReflectionParameter */
172+
/** @var \ReflectionParameter $param */
176173
$name = (string) $param->getName();
177174

178175
if (isset($data[$name])) {
@@ -265,8 +262,10 @@ protected function getView(): ?ViewInterface
265262

266263
try {
267264
$view = $container->get(View::class);
268-
assert($view instanceof View);
269-
$view->attachBehavior('getUrl', [$this, 'getUrl']);
265+
266+
if ($view instanceof View) {
267+
$view->attachBehavior('getUrl', [$this, 'getUrl']);
268+
}
270269
} catch (NotFoundExceptionInterface $e) {
271270
try {
272271
$view = $container->get(ViewInterface::class);
@@ -275,8 +274,9 @@ protected function getView(): ?ViewInterface
275274
}
276275
}
277276

278-
assert($view instanceof ViewInterface);
279-
$this->view = $view;
277+
if ($view instanceof ViewInterface) {
278+
$this->view = $view;
279+
}
280280
}
281281

282282
return $this->view;

tests/modules/test/controllers/DefaultController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class DefaultController extends \Piko\Controller
55
{
6-
public $layout = false;
6+
public string|false $layout = false;
77

88
public function indexAction()
99
{
@@ -14,4 +14,4 @@ public function errorAction($exception)
1414
{
1515
return $exception->getMessage();
1616
}
17-
}
17+
}

tests/modules/test/controllers/IndexController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class IndexController extends \Piko\Controller
77
{
8-
public $layout = false;
8+
public string|false $layout = false;
99

1010
public function setRequest(ServerRequestInterface $request): void
1111
{

tests/modules/test/controllers/TestController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class TestController extends \Piko\Controller
1010
{
11-
public $layout = false;
11+
public string|false $layout = false;
1212

1313
protected \PDO $db;
1414

tests/modules/test/controllers/User2Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class User2Controller extends \Piko\Controller
77
{
8-
public $layout = false;
8+
public string|false $layout = false;
99

1010
public function __construct(protected ?UserService $user = null)
1111
{

tests/modules/test/sub/controllers/TestController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
class TestController extends \Piko\Controller
66
{
7-
public $layout = false;
7+
public string|false $layout = false;
88

99
public function indexAction()
1010
{
1111
return 'TestModule::SubModule::TestController::indexAction';
1212
}
13-
}
13+
}

tests/modules/test/sub/til/controllers/TestController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
class TestController extends \Piko\Controller
55
{
6-
public $layout = false;
6+
public string|false $layout = false;
77

88
public function indexAction()
99
{
1010
return 'TestModule::SubModule::SubtilModule::TestController::indexAction';
1111
}
12-
}
12+
}

0 commit comments

Comments
 (0)