-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
144 lines (125 loc) · 4.02 KB
/
Controller.php
File metadata and controls
144 lines (125 loc) · 4.02 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Controllers;
use Psr\Http\Message\ResponseInterface;
use Arikaim\Core\Controllers\Traits\Base\BaseController;
use Arikaim\Core\Controllers\Traits\Base\PageErrors;
use Arikaim\Core\Controllers\Traits\Base\Multilanguage;
use Arikaim\Core\Controllers\Traits\Base\UserAccess;
/**
* Base class for all Controllers
*/
class Controller
{
use
BaseController,
Multilanguage,
UserAccess,
PageErrors;
/**
* Container ref
* @var Container|null
*/
protected $container;
/**
* Constructor
*
* @param object|null $container
*/
public function __construct(?object &$container = null)
{
$this->container = $container;
$this->init();
}
/**
* Init controller, override this method in child classes
*
* @return void
*/
public function init()
{
}
/**
* Call
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
$name .= 'Page';
if (\method_exists($this,$name) == true) {
$this->resolveRouteParams($arguments[0]);
$result = ([$this,$name])($arguments[0],$arguments[1],$arguments[2]);
if ($result === false) {
return $this->pageNotFound($arguments[1],$arguments[2]->toArray());
}
return (empty($result) == true) ? $this->pageLoad($arguments[0],$arguments[1],$arguments[2]) : $result;
}
return $this->pageNotFound($arguments[1],$arguments[2]->toArray());
}
/**
* Load page
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param \Arikaim\Core\Collection\Interfaces\CollectionInterface|array $data
* @param string|null $pageName
* @param boolean $resolveParams
* @param string|null $language
* @param bool $dispatchEvent
* @return \Psr\Http\Message\ResponseInterface
*/
public function pageLoad(
$request,
$response,
$data,
?string $pageName = null,
?string $language = null,
bool $dispatchEvent = false
)
{
$this->resolveRouteParams($request);
$language = $language ?? $this->getPageLanguage($data);
$data = (\is_object($data) == true) ? $data->toArray() : $data;
if (empty($pageName) == true) {
$pageName = $data['page_name'] ?? $this->pageName;
}
if (empty($pageName) == true) {
return $this->pageNotFound($response,$data);
}
// current url path
$data['current_path'] = $request->getUri()->getPath();
$component = $this->get('page')->render($pageName,$data,$language);
$response->getBody()->write($component->getHtmlCode());
if ($dispatchEvent === true) {
$this->container->get('event')->dispatch('core.page.load',[
'page_name' => $pageName,
'language' => $this->get('page')->getLanguage(),
'query_params' => $request->getQueryParams(),
'data' => $data
]);
}
return $response;
}
/**
* Write XML to reponse body
*
* @param ResponseInterface $response
* @param string $xml
* @return ResponseInterface
*/
public function writeXml(ResponseInterface $response, string $xml)
{
$response->getBody()->write($xml);
return $response->withHeader('Content-Type','text/xml');
}
}