1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone\Mvc; |
4
|
|
|
|
5
|
|
|
use Bone\Mvc\View\Extension\Plates\Translate; |
6
|
|
|
use Bone\Service\LoggerFactory; |
7
|
|
|
use Bone\Service\MailService; |
8
|
|
|
use Bone\Service\TranslatorFactory; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use League\Plates\Engine; |
11
|
|
|
use LogicException; |
12
|
|
|
use Monolog\Logger; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use Zend\Diactoros\Response\TextResponse; |
15
|
|
|
use Zend\Mail\Transport\Smtp; |
16
|
|
|
use Zend\Mail\Transport\SmtpOptions; |
17
|
|
|
|
18
|
|
|
class Controller extends AbstractController |
19
|
|
|
{ |
20
|
|
|
/** @var MailService $mailService */ |
21
|
|
|
private $mailService; |
22
|
|
|
|
23
|
|
|
/** @var Logger[] $log */ |
24
|
|
|
protected $log; |
25
|
|
|
|
26
|
|
|
protected $translator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Controller constructor. |
30
|
|
|
* @param ServerRequestInterface $request |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
33 |
|
public function __construct(ServerRequestInterface $request) |
34
|
33 |
|
{ |
35
|
33 |
|
parent::__construct($request); |
36
|
33 |
|
$this->initTranslator(); |
37
|
33 |
|
$this->initLogs(); |
38
|
33 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return MailService |
42
|
|
|
*/ |
43
|
1 |
|
public function getMailService() |
44
|
1 |
|
{ |
45
|
1 |
|
if (!$this->mailService instanceof MailService) { |
46
|
1 |
|
$this->initMailService(); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return $this->mailService; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
private function initMailService() |
53
|
1 |
|
{ |
54
|
1 |
|
$this->mailService = new MailService(); |
55
|
1 |
|
$options = Registry::ahoy()->get('mail'); |
56
|
1 |
|
if (isset($options['name']) && isset($options['host']) && isset($options['port']) ) { |
57
|
1 |
|
$transport = new Smtp(); |
58
|
1 |
|
$options = new SmtpOptions($options); |
59
|
1 |
|
$transport->setOptions($options); |
60
|
1 |
|
$this->mailService->setTransport($transport); |
61
|
|
|
} |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* runs before th' controller action |
66
|
|
|
*/ |
67
|
24 |
|
public function init() |
68
|
24 |
|
{ |
69
|
|
|
// extend this t' initialise th' controller |
70
|
24 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* runs after yer work is done |
74
|
|
|
*/ |
75
|
24 |
|
public function postDispatch() |
76
|
24 |
|
{ |
77
|
|
|
// extend this t' run code after yer controller is finished |
78
|
24 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
1 |
|
public function indexAction() |
84
|
1 |
|
{ |
85
|
1 |
|
return ['message' => 'Override this method']; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
public function errorAction() |
89
|
2 |
|
{ |
90
|
2 |
|
$this->disableView(); |
91
|
2 |
|
$this->disableLayout(); |
92
|
2 |
|
$this->setBody('500 Page Error.'); |
93
|
2 |
|
return new TextResponse($this->getBody(), 500); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function notFoundAction() |
97
|
1 |
|
{ |
98
|
1 |
|
$this->disableView(); |
99
|
1 |
|
$this->disableLayout(); |
100
|
1 |
|
$this->setBody('404 Page Not Found.'); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $channel |
105
|
|
|
* @return Logger |
106
|
|
|
*/ |
107
|
2 |
|
public function getLog($channel = 'default'): Logger |
108
|
2 |
|
{ |
109
|
2 |
|
if (!$this->log) { |
|
|
|
|
110
|
1 |
|
throw new LogicException('No log config found'); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
if (!isset($this->log[$channel])) { |
114
|
|
|
throw new InvalidArgumentException('No log channel with that name found'); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $this->log[$channel]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @throws \Exception |
122
|
|
|
*/ |
123
|
33 |
|
private function initLogs() |
124
|
33 |
|
{ |
125
|
33 |
|
$config = Registry::ahoy()->get('log'); |
126
|
33 |
|
if (is_array($config)) { |
127
|
2 |
|
$factory = new LoggerFactory(); |
128
|
2 |
|
$logs = $factory->createLoggers($config); |
129
|
1 |
|
$this->log = $logs; |
130
|
|
|
} |
131
|
33 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return \Zend\I18n\Translator\Translator |
135
|
|
|
*/ |
136
|
3 |
|
public function getTranslator() |
137
|
3 |
|
{ |
138
|
3 |
|
if (!$this->translator) { |
139
|
|
|
throw new LogicException('No i18n config found'); |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
return $this->translator; |
143
|
|
|
} |
144
|
|
|
|
145
|
33 |
|
private function initTranslator() |
146
|
33 |
|
{ |
147
|
33 |
|
$config = Registry::ahoy()->get('i18n'); |
148
|
33 |
|
if (is_array($config) && !$this->translator) { |
149
|
|
|
|
150
|
14 |
|
$factory = new TranslatorFactory(); |
151
|
14 |
|
$translator = $factory->createTranslator($config); |
152
|
|
|
|
153
|
14 |
|
$engine = $this->getViewEngine(); |
154
|
14 |
|
if ($engine instanceof Engine) { |
155
|
14 |
|
$engine->loadExtension(new Translate($translator)); |
156
|
|
|
} |
157
|
|
|
|
158
|
14 |
|
$defaultLocale = $config['default_locale'] ?: 'en_GB'; |
159
|
14 |
|
$locale = $this->getParam('locale', $defaultLocale); |
160
|
14 |
|
if (!in_array($locale, $config['supported_locales'])) { |
161
|
|
|
$locale = $defaultLocale; |
162
|
|
|
} |
163
|
14 |
|
$translator->setLocale($locale); |
164
|
|
|
|
165
|
14 |
|
$this->translator = $translator; |
166
|
|
|
} |
167
|
33 |
|
} |
168
|
|
|
} |
169
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.