|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Http\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Html\Helper\ArrayHelper; |
|
8
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
|
9
|
|
|
use Closure; |
|
10
|
|
|
use Opulence\Environments\Environment; |
|
11
|
|
|
use Opulence\Http\Requests\Request; |
|
12
|
|
|
use Opulence\Http\Responses\Response; |
|
13
|
|
|
use Opulence\Routing\Middleware\IMiddleware; |
|
14
|
|
|
|
|
15
|
|
|
class EnvironmentWarning implements IMiddleware |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var ITranslator */ |
|
18
|
|
|
protected $translator; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $environment; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* EnvironmentWarning constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param ITranslator $translator |
|
27
|
|
|
* @param string $environment |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(ITranslator $translator, string $environment) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->translator = $translator; |
|
32
|
|
|
$this->environment = $environment; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Request $request |
|
37
|
|
|
* @param Closure $next |
|
38
|
|
|
* |
|
39
|
|
|
* @return Response |
|
40
|
|
|
*/ |
|
41
|
|
|
public function handle(Request $request, Closure $next): Response |
|
42
|
|
|
{ |
|
43
|
|
|
/** @var Response $response */ |
|
44
|
|
|
$response = $next($request); |
|
45
|
|
|
|
|
46
|
|
|
if ($this->environment == Environment::PRODUCTION) { |
|
47
|
|
|
return $response; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$warning = $this->getWarningHtml($this->environment); |
|
51
|
|
|
|
|
52
|
|
|
$response->setContent(preg_replace('/<body([^>]*)>/', '<body${1}>' . $warning, $response->getContent(), 1)); |
|
53
|
|
|
|
|
54
|
|
|
return $response; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $environmentName |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getWarningHtml(string $environmentName): string |
|
63
|
|
|
{ |
|
64
|
|
|
$styles = [ |
|
65
|
|
|
'color' => 'white', |
|
66
|
|
|
'line-height' => '1em', |
|
67
|
|
|
'font-weight' => 'bold', |
|
68
|
|
|
]; |
|
69
|
|
|
$warning = sprintf( |
|
70
|
|
|
'<p style="%s">%s</p>', |
|
71
|
|
|
ArrayHelper::toStyles($styles), |
|
72
|
|
|
$this->translator->translate('admin:environment', $environmentName) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$styles = [ |
|
76
|
|
|
'position' => 'fixed', |
|
77
|
|
|
'bottom' => '10px', |
|
78
|
|
|
'right' => '10px', |
|
79
|
|
|
'z-index' => '10000', |
|
80
|
|
|
'padding' => '1em 1em 0.5em', |
|
81
|
|
|
'margin' => '0 auto', |
|
82
|
|
|
'background' => '#ff5722', |
|
83
|
|
|
'cursor' => 'pointer', |
|
84
|
|
|
]; |
|
85
|
|
|
$onClick = '$(this).remove()'; |
|
86
|
|
|
|
|
87
|
|
|
return sprintf('<div style="%s" onclick="%s">%s</div>', ArrayHelper::toStyles($styles), $onClick, $warning); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|