-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
98 lines (85 loc) · 2.5 KB
/
Controller.php
File metadata and controls
98 lines (85 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\Routing;
use Opulence\Http\Requests\Request;
use Opulence\Http\Responses\Response;
use Opulence\Views\Compilers\ICompiler;
use Opulence\Views\Factories\IViewFactory;
use Opulence\Views\IView;
/**
* Defines a base controller
*/
class Controller
{
/** @var IView The view used in the response */
protected $view = null;
/** @var ICompiler The view compiler to use */
protected $viewCompiler = null;
/** @var IViewFactory The view factory to use */
protected $viewFactory = null;
/** @var Request The HTTP request */
protected $request = null;
/**
* Actually calls the method in the controller
* Rather than calling the method directly from the route dispatcher, call this method
*
* @param string $methodName The name of the method in $this to call
* @param array $parameters The list of parameters to pass into the action method
* @return Response The HTTP response returned by the method
*/
public function callMethod(string $methodName, array $parameters) : Response
{
$this->setUpView();
/** @var Response $response */
$response = $this->$methodName(...$parameters);
if ($response === null || is_string($response)) {
$response = new Response($response === null ? '' : $response);
if ($this->viewCompiler instanceof ICompiler && $this->view !== null) {
$response->setContent($this->viewCompiler->compile($this->view));
}
}
return $response;
}
/**
* @return IView|null
*/
public function getView()
{
return $this->view;
}
/**
* @param Request $request
*/
public function setRequest(Request $request)
{
$this->request = $request;
}
/**
* @param ICompiler $viewCompiler
*/
public function setViewCompiler(ICompiler $viewCompiler)
{
$this->viewCompiler = $viewCompiler;
}
/**
* @param IViewFactory $viewFactory
*/
public function setViewFactory(IViewFactory $viewFactory)
{
$this->viewFactory = $viewFactory;
}
/**
* Sets up the view
* Useful for setting up a view's components that are the same across controller methods
*/
protected function setUpView()
{
// Don't do anything
}
}