|
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 OldController 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
|
|
|
public function __construct(ServerRequestInterface $request) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($request); |
|
36
|
|
|
$this->initTranslator(); |
|
37
|
|
|
$this->initLogs(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return MailService |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getMailService() |
|
44
|
|
|
{ |
|
45
|
|
|
if (!$this->mailService instanceof MailService) { |
|
46
|
|
|
$this->initMailService(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->mailService; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function initMailService() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->mailService = new MailService(); |
|
55
|
|
|
$options = Registry::ahoy()->get('mail'); |
|
56
|
|
|
if (isset($options['name']) && isset($options['host']) && isset($options['port']) ) { |
|
57
|
|
|
$transport = new Smtp(); |
|
58
|
|
|
$options = new SmtpOptions($options); |
|
59
|
|
|
$transport->setOptions($options); |
|
60
|
|
|
$this->mailService->setTransport($transport); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* runs before th' controller action |
|
66
|
|
|
*/ |
|
67
|
|
|
public function init() |
|
68
|
|
|
{ |
|
69
|
|
|
// extend this t' initialise th' controller |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* runs after yer work is done |
|
74
|
|
|
*/ |
|
75
|
|
|
public function postDispatch() |
|
76
|
|
|
{ |
|
77
|
|
|
// extend this t' run code after yer controller is finished |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
public function errorAction() |
|
82
|
|
|
{ |
|
83
|
|
|
$this->disableView(); |
|
84
|
|
|
$this->disableLayout(); |
|
85
|
|
|
$this->setBody('500 Page Error.'); |
|
86
|
|
|
return new TextResponse($this->getBody(), 500); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function notFoundAction() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->disableView(); |
|
92
|
|
|
$this->disableLayout(); |
|
93
|
|
|
$this->setBody('404 Page Not Found.'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $channel |
|
98
|
|
|
* @return Logger |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getLog($channel = 'default'): Logger |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$this->log) { |
|
|
|
|
|
|
103
|
|
|
throw new LogicException('No log config found'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (!isset($this->log[$channel])) { |
|
107
|
|
|
throw new InvalidArgumentException('No log channel with that name found'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->log[$channel]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @throws \Exception |
|
115
|
|
|
*/ |
|
116
|
|
|
private function initLogs() |
|
117
|
|
|
{ |
|
118
|
|
|
$config = Registry::ahoy()->get('log'); |
|
119
|
|
|
if (is_array($config)) { |
|
120
|
|
|
$factory = new LoggerFactory(); |
|
121
|
|
|
$logs = $factory->createLoggers($config); |
|
122
|
|
|
$this->log = $logs; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return \Zend\I18n\Translator\Translator |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getTranslator() |
|
130
|
|
|
{ |
|
131
|
|
|
if (!$this->translator) { |
|
132
|
|
|
throw new LogicException('No i18n config found'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $this->translator; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function initTranslator() |
|
139
|
|
|
{ |
|
140
|
|
|
$config = Registry::ahoy()->get('i18n'); |
|
141
|
|
|
if (is_array($config) && !$this->translator) { |
|
142
|
|
|
|
|
143
|
|
|
$factory = new TranslatorFactory(); |
|
144
|
|
|
$translator = $factory->createTranslator($config); |
|
145
|
|
|
|
|
146
|
|
|
$engine = $this->getViewEngine(); |
|
147
|
|
|
if ($engine instanceof Engine) { |
|
148
|
|
|
$engine->loadExtension(new Translate($translator)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$defaultLocale = $config['default_locale'] ?: 'en_GB'; |
|
152
|
|
|
$locale = $this->getParam('locale', $defaultLocale); |
|
153
|
|
|
if (!in_array($locale, $config['supported_locales'])) { |
|
154
|
|
|
$locale = $defaultLocale; |
|
155
|
|
|
} |
|
156
|
|
|
$translator->setLocale($locale); |
|
157
|
|
|
|
|
158
|
|
|
$this->translator = $translator; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
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.