|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Phauthentic\Presentation\Service; |
|
5
|
|
|
|
|
6
|
|
|
use Phauthentic\Presentation\View\View; |
|
7
|
|
|
use Phauthentic\Presentation\View\ViewInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Maps the request automatically to the view DTO objects properties |
|
12
|
|
|
* |
|
13
|
|
|
* You must configure your router or in some middleware some request attributes. |
|
14
|
|
|
* The defaults are `module`, `component` and `action`. |
|
15
|
|
|
* |
|
16
|
|
|
* module: |
|
17
|
|
|
* A module is a plugin or a whole - separate - module of the application. This |
|
18
|
|
|
* request attribute can be empty! It's optional! |
|
19
|
|
|
* |
|
20
|
|
|
* component: |
|
21
|
|
|
* A component would be in the MVC context a "controller". It can be also thougt |
|
22
|
|
|
* as a request handling object. No matter if it handles a single or multiple |
|
23
|
|
|
* different requests |
|
24
|
|
|
* |
|
25
|
|
|
* action: |
|
26
|
|
|
* An action is the actual method or name of the handler that gets called |
|
27
|
|
|
* |
|
28
|
|
|
* Examples: |
|
29
|
|
|
* |
|
30
|
|
|
* URL: /my/module/example/view/1 |
|
31
|
|
|
* - Module: /my/module |
|
32
|
|
|
* - Component: /example |
|
33
|
|
|
* - Action: /view |
|
34
|
|
|
* |
|
35
|
|
|
* Will be composed to: |
|
36
|
|
|
* - {basePath}/{module}/{component}/{action} |
|
37
|
|
|
* - /var/www/my-app/resources/templates/my/module/example/view.tpl |
|
38
|
|
|
*/ |
|
39
|
|
|
class RequestToTemplateMapperService implements RequestToTemplateMapperServiceInterface |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Default view class |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $viewClass = View::class; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Config |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $config = [ |
|
55
|
|
|
'module' => 'module', // "plugin", "extension" |
|
56
|
|
|
'component' => 'component', // "controller", "handler" |
|
57
|
|
|
'action' => 'action', |
|
58
|
|
|
'triggerWarnings' => false |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Constructor |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface |
|
65
|
|
|
* @param array $config |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct(array $config = []) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->config = array_merge($this->config, $config); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Sets the template for the view object up based on the request information |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Server Request Object |
|
76
|
|
|
* @param \Phauthentic\Presentation\View\ViewInterface $view View DTO |
|
77
|
|
|
* @return \Phauthentic\Presentation\View\ViewInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
public function mapRequestToView(ServerRequestInterface $request, ViewInterface $view): ViewInterface |
|
80
|
|
|
{ |
|
81
|
|
|
$view->setTemplatePath($this->getTemplatePathFromRequest($request)); |
|
82
|
|
|
$view->setTemplate($this->getTemplateFromRequest($request)); |
|
83
|
|
|
|
|
84
|
|
|
return $view; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Gets the template path from the request data |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getTemplatePathFromRequest(ServerRequestInterface $request): string |
|
93
|
|
|
{ |
|
94
|
|
|
$module = (string)$request->getAttribute('module'); |
|
95
|
|
|
$component = (string)$request->getAttribute('component'); |
|
96
|
|
|
|
|
97
|
|
|
$module = empty($module) ? '' : $module; |
|
98
|
|
|
$component = empty($component) ? 'default' : $component; |
|
99
|
|
|
|
|
100
|
|
|
if (empty($component)) { |
|
101
|
|
|
trigger_error( |
|
102
|
|
|
sprintf('Could not get the template path from the request data. The request attribute `%s` was not found.', $this->config['component']), |
|
103
|
|
|
E_USER_WARNING |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (!empty($module)) { |
|
108
|
|
|
return $module . DIRECTORY_SEPARATOR . $component; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $component; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Gets the template name from the request |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getTemplateFromRequest(ServerRequestInterface $request): string |
|
120
|
|
|
{ |
|
121
|
|
|
$action = (string)$request->getAttribute('action'); |
|
122
|
|
|
|
|
123
|
|
|
if (empty($action)) { |
|
124
|
|
|
trigger_error( |
|
125
|
|
|
sprintf('Could not get the template name from the request data. The request attribute `%s` was not found.', $this->config['action']), |
|
126
|
|
|
E_USER_WARNING |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $action; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|