Skip to content

Commit f957e54

Browse files
committed
static instead of access proxies
1 parent 126c7a2 commit f957e54

25 files changed

+625
-524
lines changed

src/Commands/stubs/MakeModule/Module.php.stub

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use ItvisionSy\Laravel\Modules\Module as BaseModule;
77
class {{class}} extends BaseModule
88
{
99

10-
protected $moduleId='{{id}}';
11-
protected $moduleName='{{name}}';
12-
protected $moduleRouteNamePrefix='{{url_name}}';
13-
protected $moduleUrlPrefix='{{url_name}}';
10+
static protected $moduleId='{{id}}';
11+
static protected $moduleName='{{name}}';
12+
static protected $moduleRouteNamePrefix='{{url_name}}';
13+
static protected $moduleUrlPrefix='{{url_name}}';
1414

1515
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<?php /** @var {{namespace}}\{{id}}\{{class}} $this_module */ ?>
1+
<?php /* @var $this_module {{namespace}}\{{id}}\{{class}} */ ?>
22
Module {{$this_module->name()}}

src/Module.php

Lines changed: 87 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Created by PhpStorm.
45
* User: Muhannad Shelleh <muhannad.shelleh@live.com>
@@ -13,134 +14,140 @@
1314
use ItvisionSy\Laravel\Modules\Traits\StaticAndInstanceAccessTrait;
1415
use ItvisionSy\Laravel\Modules\Traits\StaticFactory;
1516

16-
abstract class Module implements StaticAndInstanceAccessInterface
17-
{
17+
abstract class Module implements StaticAndInstanceAccessInterface {
1818

1919
use StaticAndInstanceAccessTrait;
2020
use StaticFactory;
2121

22-
static protected $_routes = [];
22+
/**
23+
*
24+
* @return Module|$this|static|self
25+
*/
26+
protected static function this() {
27+
if (!array_key_exists(get_called_class(), static::$startedUp)) {
28+
static::$startedUp[get_called_class()] = static::make();
29+
}
30+
return static::$startedUp[get_called_class()];
31+
}
2332

24-
protected $moduleId;
25-
protected $moduleName;
26-
protected $moduleRouteNamePrefix;
27-
protected $moduleUrlPrefix;
33+
static protected $_routes = [];
34+
static protected $moduleId;
35+
static protected $moduleName;
36+
static protected $moduleRouteNamePrefix;
37+
static protected $moduleUrlPrefix;
38+
static protected $startedUp = [];
2839

29-
public static function grantAccess()
30-
{
31-
return ['id', 'name', 'urlPrefix', 'routePrefix',
32-
'isEnabled', 'isDisabled',
33-
'disableModule', 'enableModule',
34-
'modulePath', 'viewsPath', 'getViewName', 'renderView', 'getRoutePath', 'getRouteName', 'getPathForRoute',
35-
'getStoredValue', 'setStoredValue'
36-
];
40+
public static function grantAccess() {
41+
return ['registerViewsPath', 'registerRoutes', 'registerFrameworkResources', 'registerMigrationsPath'];
3742
}
3843

39-
public function __construct()
40-
{
41-
$this->startup();
44+
public function __construct() {
45+
if (array_key_exists(get_called_class(), static::$startedUp) === false) {
46+
static::startup();
47+
static::$startedUp[get_called_class()] = $this;
48+
}
4249
}
4350

4451
/**
4552
* @return mixed Loads helpers and dependencies
4653
*/
47-
public function startup()
48-
{
54+
public static function startup() {
4955

5056
}
5157

5258
/**
5359
* Returns a path to the routes file, or false if no routes is required
5460
* @return bool|string
5561
*/
56-
public function routesPath()
57-
{
58-
$defaultPath = $this->modulePath(join(DIRECTORY_SEPARATOR, ['', 'Http', 'routes.php']));
62+
public static function routesPath() {
63+
$defaultPath = static::modulePath(join(DIRECTORY_SEPARATOR, ['', 'Http', 'routes.php']));
5964
return file_exists($defaultPath) ? $defaultPath : false;
6065
}
6166

6267
/**
6368
* @return string unique identifier of the module
6469
*/
65-
protected function id()
66-
{
67-
return $this->moduleId;
70+
public static function id() {
71+
return static::$moduleId;
6872
}
6973

7074
/**
7175
* @return string display name of the module
7276
*/
73-
protected function name()
74-
{
75-
return $this->moduleName;
77+
public static function name() {
78+
return static::$moduleName;
7679
}
7780

7881
/**
7982
* @return mixed
8083
*/
81-
protected function routePrefix()
82-
{
83-
return $this->moduleRouteNamePrefix ?: $this->id();
84+
public static function routePrefix() {
85+
return static::$moduleRouteNamePrefix ?: static::id();
8486
}
8587

8688
/**
8789
* @return mixed
8890
*/
89-
protected function urlPrefix()
90-
{
91-
return $this->moduleUrlPrefix ?: $this->id();
91+
public static function urlPrefix() {
92+
return static::$moduleUrlPrefix ?: static::id();
9293
}
9394

9495
/**
9596
* @return boolean
9697
*/
97-
protected function isEnabled()
98-
{
99-
return Modules::isModuleEnabled($this);
98+
public static function isEnabled() {
99+
return Modules::isModuleEnabled(static::this());
100100
}
101101

102102
/**
103103
* @return bool
104104
*/
105-
protected function isDisabled()
106-
{
107-
return !$this->isEnabled();
105+
public static function isDisabled() {
106+
return !static::isEnabled();
108107
}
109108

110109
/**
111110
* @return mixed
112111
*/
113-
protected function enableModule()
114-
{
115-
return Modules::enableModule($this);
112+
public static function enableModule() {
113+
return Modules::enableModule(static::this());
116114
}
117115

118116
/**
119117
* @return mixed
120118
*/
121-
protected function disableModule()
122-
{
123-
return Modules::disableModule($this);
119+
public static function disableModule() {
120+
return Modules::disableModule(static::this());
124121
}
125122

126123
/**
127124
* Path relative to module root
128125
* @param $path
129126
* @return string
130127
*/
131-
protected function modulePath($path)
132-
{
133-
$class = explode("\\", get_class($this))[2];
128+
public static function modulePath($path) {
129+
$class = explode("\\", get_class(static::this()))[2];
134130
$fullPath = rtrim(config('modules.directory'), "\\") . DIRECTORY_SEPARATOR . $class . DIRECTORY_SEPARATOR . ($path ? $path : "");
135131
return $fullPath;
136132
}
137133

134+
/**
135+
*
136+
* @return string|null
137+
*/
138+
public static function migrationsPath() {
139+
$dir = static::modulePath("Migrations" . DIRECTORY_SEPARATOR);
140+
if (is_dir($dir)) {
141+
return $dir;
142+
}
143+
return null;
144+
}
145+
138146
/**
139147
* @return null|string path to views resources.
140148
*/
141-
protected function viewsPath()
142-
{
143-
$dir = $this->modulePath("Views" . DIRECTORY_SEPARATOR);
149+
public static function viewsPath() {
150+
$dir = static::modulePath("Views" . DIRECTORY_SEPARATOR);
144151
if (is_dir($dir)) {
145152
return $dir;
146153
}
@@ -152,9 +159,8 @@ protected function viewsPath()
152159
* @param $name
153160
* @return string
154161
*/
155-
protected function getViewName($name)
156-
{
157-
return $this->id() . "::" . $name;
162+
public static function getViewName($name) {
163+
return static::id() . "::" . $name;
158164
}
159165

160166
/**
@@ -163,9 +169,8 @@ protected function getViewName($name)
163169
* @param array $mergeData
164170
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
165171
*/
166-
protected function renderView($viewName, array $data = [], array $mergeData = [])
167-
{
168-
return \view($this->getViewName($viewName), $data, $mergeData + ['this_module' => $this, '__this_module' => $this]);
172+
public static function renderView($viewName, array $data = [], array $mergeData = []) {
173+
return \view(static::getViewName($viewName), $data, $mergeData + ['this_module' => static::this(), '__this_module' => static::this()]);
169174
}
170175

171176
/**
@@ -175,9 +180,8 @@ protected function renderView($viewName, array $data = [], array $mergeData = []
175180
* @param $path
176181
* @return mixed
177182
*/
178-
protected function getRoutePath($path)
179-
{
180-
return trim(str_replace('//', '/', join('/', [config('modules.route_prefix'), $this->urlPrefix(), $path])), '/');
183+
public static function getRoutePath($path) {
184+
return trim(str_replace('//', '/', join('/', [config('modules.route_prefix'), static::urlPrefix(), $path])), '/');
181185
}
182186

183187
/**
@@ -187,9 +191,8 @@ protected function getRoutePath($path)
187191
* @param $name
188192
* @return mixed
189193
*/
190-
protected function getRouteName($name)
191-
{
192-
return trim(str_replace('..', '.', join('.', [config('modules.route_name_prefix'), $this->routePrefix(), $name])), '.');
194+
public static function getRouteName($name) {
195+
return trim(str_replace('..', '.', join('.', [config('modules.route_name_prefix'), static::routePrefix(), $name])), '.');
193196
}
194197

195198
/**
@@ -200,54 +203,48 @@ protected function getRouteName($name)
200203
* @param Route|null $route
201204
* @return Route
202205
*/
203-
protected function getPathForRoute($routeName, array $params = [], $absolute = true, Route $route = null)
204-
{
205-
return route($this->getRouteName($routeName), $params, $absolute, $route);
206+
public static function getPathForRoute($routeName, array $params = [], $absolute = true, Route $route = null) {
207+
return route(static::getRouteName($routeName), $params, $absolute, $route);
206208
}
207209

208-
public function registerViewsPath($app = null)
209-
{
210-
if (!$this->viewsPath()) {
210+
protected static function registerViewsPath($app = null) {
211+
if (!static::viewsPath()) {
211212
return;
212213
}
213214
$app = $app ?: app();
214-
if (is_dir($appPath = $app->basePath() . '/resources/views/vendor/' . $this->id())) {
215-
$app['view']->addNamespace($this->id(), $appPath);
215+
if (is_dir($appPath = $app->basePath() . '/resources/views/vendor/' . static::id())) {
216+
$app['view']->addNamespace(static::id(), $appPath);
216217
}
217218

218-
$app['view']->addNamespace($this->id(), $this->viewsPath());
219+
$app['view']->addNamespace(static::id(), static::viewsPath());
219220
}
220221

221-
public function registerRoutes($app = null)
222-
{
223-
if (!$this->routesPath()) {
222+
protected static function registerRoutes($app = null) {
223+
if (!static::routesPath()) {
224224
return;
225225
}
226226
$app = $app ?: app();
227227
if ($app->routesAreCached()) {
228228
Artisan::call('cache:clear');
229229
}
230-
require_once $this->routesPath();
230+
require_once static::routesPath();
231231
}
232232

233-
public function registerFrameworkResources($app = null)
234-
{
235-
if ($this->isDisabled()) {
233+
protected static function registerFrameworkResources($app = null) {
234+
if (static::isDisabled()) {
236235
return;
237236
}
238237
$app = $app ?: app();
239-
$this->registerViewsPath($app);
240-
$this->registerRoutes($app);
238+
static::registerViewsPath($app);
239+
static::registerRoutes($app);
241240
}
242241

243-
protected function setStoreValue($key, $value = null)
244-
{
245-
Modules::setStoredValue($key, $value, $this);
242+
protected static function setStoreValue($key, $value = null) {
243+
Modules::setStoredValue($key, $value, static::this());
246244
}
247245

248-
protected function getStoreValue($key, $default = null)
249-
{
250-
Modules::getStoredValue($key, $default, $this);
246+
protected static function getStoreValue($key, $default = null) {
247+
Modules::getStoredValue($key, $default, static::this());
251248
}
252249

253-
}
250+
}

tests/codeCoverage/Commands/InitiateDatabaseTable.php.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ <h4>Legend</h4>
199199
<span class="warning"><strong>Dead Code</strong></span>
200200
</p>
201201
<p>
202-
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Mar 25 14:45:58 UTC 2017.</small>
202+
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Apr 1 13:16:39 UTC 2017.</small>
203203
</p>
204204
<a title="Back to the top" id="toplink" href="#"><span class="glyphicon glyphicon-arrow-up"></span></a>
205205
</footer>

tests/codeCoverage/Commands/MakeModule.php.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ <h4>Legend</h4>
311311
<span class="warning"><strong>Dead Code</strong></span>
312312
</p>
313313
<p>
314-
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Mar 25 14:45:58 UTC 2017.</small>
314+
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Apr 1 13:16:39 UTC 2017.</small>
315315
</p>
316316
<a title="Back to the top" id="toplink" href="#"><span class="glyphicon glyphicon-arrow-up"></span></a>
317317
</footer>

tests/codeCoverage/Commands/dashboard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ <h3>Project Risks</h3>
141141
<footer>
142142
<hr/>
143143
<p>
144-
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Mar 25 14:45:58 UTC 2017.</small>
144+
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Apr 1 13:16:39 UTC 2017.</small>
145145
</p>
146146
</footer>
147147
</div>

tests/codeCoverage/Commands/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ <h4>Legend</h4>
137137
<span class="success"><strong>High</strong>: 90% to 100%</span>
138138
</p>
139139
<p>
140-
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Mar 25 14:45:58 UTC 2017.</small>
140+
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Apr 1 13:16:39 UTC 2017.</small>
141141
</p>
142142
</footer>
143143
</div>

tests/codeCoverage/Controller.php.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ <h4>Legend</h4>
210210
<span class="warning"><strong>Dead Code</strong></span>
211211
</p>
212212
<p>
213-
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Mar 25 14:45:58 UTC 2017.</small>
213+
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Apr 1 13:16:39 UTC 2017.</small>
214214
</p>
215215
<a title="Back to the top" id="toplink" href="#"><span class="glyphicon glyphicon-arrow-up"></span></a>
216216
</footer>

tests/codeCoverage/Interfaces/KeyValueStoreInterface.php.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ <h4>Legend</h4>
9090
<span class="warning"><strong>Dead Code</strong></span>
9191
</p>
9292
<p>
93-
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Mar 25 14:45:58 UTC 2017.</small>
93+
<small>Generated by <a href="https://github.com/sebastianbergmann/php-code-coverage" target="_top">php-code-coverage 4.0.7</a> using <a href="https://secure.php.net/" target="_top">PHP 5.6.29</a> with <a href="https://xdebug.org/">Xdebug 2.2.5</a> and <a href="https://phpunit.de/">PHPUnit 5.7.16</a> at Sat Apr 1 13:16:39 UTC 2017.</small>
9494
</p>
9595
<a title="Back to the top" id="toplink" href="#"><span class="glyphicon glyphicon-arrow-up"></span></a>
9696
</footer>

0 commit comments

Comments
 (0)