Skip to content

Commit b35a618

Browse files
committed
Added centralized config file to set what things should be auto-discovered, and tweaked so that Registrars are auto-disovered.
1 parent 9bb7a52 commit b35a618

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

application/Config/Modules.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php namespace Config;
2+
3+
// Cannot extend BaseConfig or looping resources occurs.
4+
class Modules
5+
{
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Auto-discover Rules
9+
|--------------------------------------------------------------------------
10+
|
11+
| Lists the aliases of all discovery classes that will be active
12+
| and used during the current application request. If it is not
13+
| listed here, only the base application elements will be used.
14+
*/
15+
public $enabled = [
16+
'events',
17+
'registrars',
18+
'routes',
19+
'services',
20+
'views'
21+
];
22+
23+
/*
24+
|--------------------------------------------------------------------------
25+
| Cache Results?
26+
|--------------------------------------------------------------------------
27+
|
28+
| If true, the results of all discoveries will be cached and will be
29+
| not be searched for until the cache is cleared, increasing performance
30+
| at the cost of any additional discovery.
31+
|
32+
| This is a good setting to use in production where changes are infrequent.
33+
*/
34+
public $cache = false;
35+
36+
/**
37+
* Should the application auto-discover the requested resources.
38+
*
39+
* Valid values are:
40+
* - events
41+
* - registrars
42+
* - routes
43+
* - services
44+
* - views
45+
*
46+
* @param string $alias
47+
*
48+
* @return bool
49+
*/
50+
public function shouldDiscover(string $alias)
51+
{
52+
$alias = strtolower($alias);
53+
54+
return in_array($alias, $this->enabled);
55+
}
56+
}

system/Config/BaseConfig.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class BaseConfig
5454
*
5555
* @var array
5656
*/
57-
protected $registrars;
57+
protected static $registrars = [];
58+
59+
protected static $didDiscovery = false;
5860

5961
/**
6062
* Will attempt to get environment variables with names
@@ -165,13 +167,23 @@ protected function getEnvValue(string $property, string $prefix, string $shortPr
165167
*/
166168
protected function registerProperties()
167169
{
168-
if (empty($this->registrars))
170+
$config = config('Modules');
171+
172+
if (! $config->shouldDiscover('registrars'))
173+
{
169174
return;
175+
}
176+
177+
if (! static::$didDiscovery)
178+
{
179+
$locator = \Config\Services::locator();
180+
static::$registrars = $locator->search('Config/Registrar.php');
181+
}
170182

171183
$shortName = (new \ReflectionClass($this))->getShortName();
172184

173185
// Check the registrar class for a method named after this class' shortName
174-
foreach ($this->registrars as $callable)
186+
foreach (static::$registrars as $callable)
175187
{
176188
// ignore non-applicable registrars
177189
if ( ! method_exists($callable, $shortName))

0 commit comments

Comments
 (0)