1 | <?php |
||
2 | /** |
||
3 | * DronePHP (http://www.dronephp.com) |
||
4 | * |
||
5 | * @link http://github.com/Pleets/DronePHP |
||
6 | * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
||
7 | * @license http://www.dronephp.com/license |
||
8 | * @author Darío Rivera <[email protected]> |
||
9 | */ |
||
10 | |||
11 | namespace Drone\Mvc; |
||
12 | |||
13 | /** |
||
14 | * AbstractModule class |
||
15 | * |
||
16 | * This is an abstract class to execute some code before method execution in a controller. |
||
17 | */ |
||
18 | abstract class AbstractModule |
||
19 | { |
||
20 | /** |
||
21 | * The module name |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $moduleName; |
||
26 | |||
27 | /** |
||
28 | * Config file for the module |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $configFile; |
||
33 | |||
34 | /** |
||
35 | * Defines method execution in the controller |
||
36 | * |
||
37 | * @var boolean |
||
38 | */ |
||
39 | private $executionAllowed = true; |
||
40 | |||
41 | /** |
||
42 | * Returns the moduleName attribute |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getModuleName() |
||
47 | { |
||
48 | return $this->moduleName; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Returns the configFile attribute |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | 1 | public function getConfigFile() |
|
57 | { |
||
58 | 1 | return $this->configFile; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Sets the moduleName attribute |
||
63 | * |
||
64 | * @param string $moduleName |
||
65 | * |
||
66 | * @return null |
||
67 | */ |
||
68 | public function setModuleName($moduleName) |
||
69 | { |
||
70 | $this->moduleName = $moduleName; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Sets the configFile attribute |
||
75 | * |
||
76 | * @param string $configFile |
||
77 | * |
||
78 | * @throws \RuntimeException |
||
79 | * |
||
80 | * @return null |
||
81 | */ |
||
82 | 2 | public function setConfigFile($configFile) |
|
83 | { |
||
84 | 2 | $this->configFile = $configFile; |
|
85 | 2 | } |
|
86 | |||
87 | /** |
||
88 | * Constructor |
||
89 | * |
||
90 | * @param string $moduleName |
||
91 | */ |
||
92 | 3 | public function __construct($moduleName) |
|
93 | { |
||
94 | 3 | $this->moduleName = $moduleName; |
|
95 | 3 | $this->init(); |
|
96 | 3 | } |
|
97 | |||
98 | /** |
||
99 | * Abstract method to be executed before each method execution in the controller |
||
100 | * |
||
101 | * @param AbstractController |
||
102 | */ |
||
103 | abstract public function init(); |
||
104 | |||
105 | /** |
||
106 | * Checks if executionAllowed is true |
||
107 | * |
||
108 | * @return null |
||
109 | */ |
||
110 | 2 | public function executionIsAllowed() |
|
111 | { |
||
112 | 2 | return $this->executionAllowed; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Disallow the method execution in a controller |
||
117 | * |
||
118 | * @return null |
||
119 | */ |
||
120 | 1 | public function disallowExecution() |
|
121 | { |
||
122 | 1 | $this->executionAllowed = false; |
|
123 | 1 | } |
|
124 | |||
125 | /** |
||
126 | * Allow the method execution in a controller |
||
127 | * |
||
128 | * @return null |
||
129 | */ |
||
130 | 1 | public function allowExecution() |
|
131 | { |
||
132 | 1 | $this->executionAllowed = true; |
|
133 | 1 | } |
|
134 | |||
135 | /** |
||
136 | * Returns the module configuration |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | 1 | public function getConfig() |
|
141 | { |
||
142 | 1 | $_file = $this->configFile; |
|
143 | |||
144 | 1 | if (!file_exists($_file)) { |
|
145 | throw new \RuntimeException("The config file '$_file' does not exists"); |
||
146 | } |
||
147 | |||
148 | 1 | return (array) include $this->configFile; |
|
149 | } |
||
150 | } |
||
151 |