1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Date: 16.07.15 |
5
|
|
|
* Time: 12:41 |
6
|
|
|
* |
7
|
|
|
* @author : Korotkov Danila <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2016, Korotkov Danila |
9
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU GPLv3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Http; |
13
|
|
|
|
14
|
|
|
use Rudra\Container; |
15
|
|
|
use Rudra\Controller; |
16
|
|
|
use Rudra\IContainer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Module |
20
|
|
|
* |
21
|
|
|
* @package Main |
22
|
|
|
*/ |
23
|
|
|
class BaseController extends Controller |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param IContainer $container |
28
|
|
|
* @param string $templateEngine |
29
|
|
|
*/ |
30
|
|
|
public function init(IContainer $container, string $templateEngine) |
31
|
|
|
{ |
32
|
|
|
parent::init($container, $templateEngine); |
33
|
|
|
$this->setData('Rudra', 'title'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Метод выполняется перед вызовом контроллера |
38
|
|
|
*/ |
39
|
|
|
public function before() |
40
|
|
|
{ |
41
|
|
|
$this->container()->get('auth')->check(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function after() |
45
|
|
|
{ |
46
|
|
|
// Очищаем сессию от alert |
47
|
|
|
$this->container()->unsetSession('value'); |
48
|
|
|
$this->container()->unsetSession('alert'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function templateEngine(string $config): void |
52
|
|
|
{ |
53
|
|
|
parent::templateEngine($config); |
54
|
|
|
|
55
|
|
|
$d = new \Twig_SimpleFunction('d', function ($var) { |
56
|
|
|
return d($var); |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
$this->getTwig()->addFunction($d); |
60
|
|
|
|
61
|
|
|
$date = new \Twig_SimpleFunction('date', function ($var) { |
62
|
|
|
return date($var); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
$this->getTwig()->addFunction($date); |
66
|
|
|
|
67
|
|
|
$auth = new \Twig_SimpleFunction('auth', function () { |
68
|
|
|
return $this->container()->get('auth')->regularAccess(); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
$this->getTwig()->addFunction($auth); |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
$active = new \Twig_SimpleFunction('active', function ($var) { |
75
|
|
|
return $this->container()->get('pagination')->activeClass($var); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
$this->getTwig()->addFunction($active); |
79
|
|
|
|
80
|
|
|
$strstr = new \Twig_SimpleFunction('strstr', function ($var) { |
81
|
|
|
return strstr($var, '<a id="strstr" name="strstr"></a>', true); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
$this->getTwig()->addFunction($strstr); |
85
|
|
|
|
86
|
|
|
if (DEV) { |
87
|
|
|
$debugbarRenderer = Container::$app->get('debugbar')->getJavascriptRenderer(); |
88
|
|
|
$this->getTwig()->addGlobal('debugbar', $debugbarRenderer); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
public function error404() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->container()->get('redirect')->responseCode('404'); |
96
|
|
|
|
97
|
|
|
return $this->twig('errors/error.html.twig', [ |
98
|
|
|
'title' => '404 Page Not Found :: ' . $this->getData('title'), |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function error503() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$this->container()->get('redirect')->responseCode('503'); |
105
|
|
|
|
106
|
|
|
return $this->twig('errors/error.html.twig', [ |
107
|
|
|
'title' => '503 Service Unavailable :: ' . $this->getData('title'), |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
public function error500() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$this->container()->get('redirect')->responseCode('503'); |
114
|
|
|
|
115
|
|
|
return $this->twig('errors/503.html.twig', [ |
116
|
|
|
'title' => '503 Service Unavailable :: ' . $this->getData('title'), |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.