15
15
namespace Bread \Configuration ;
16
16
17
17
use Bread \Caching \Cache ;
18
+ use Bread \Promises \When ;
18
19
use Exception ;
19
20
20
21
class Manager
@@ -24,30 +25,23 @@ class Manager
24
25
25
26
private static $ configurations = array ();
26
27
27
- public static function initialize ($ url )
28
+ public static function initialize ($ url, $ cache = false )
28
29
{
29
- switch (parse_url ($ url , PHP_URL_SCHEME )) {
30
+ switch ($ scheme = parse_url ($ url , PHP_URL_SCHEME )) {
30
31
case 'file ' :
31
32
$ directory = parse_url ($ url , PHP_URL_PATH );
32
- return Cache::instance ()->fetch (__METHOD__ )->then (null , function ($ key ) use ($ directory ) {
33
- $ configurations = array ();
34
- if (!is_dir ($ directory )) {
35
- throw new Exception ("Configuration directory $ directory is not valid. " );
36
- }
37
- foreach ((array ) scandir ($ directory ) as $ path ) {
38
- $ extension = pathinfo ($ path , PATHINFO_EXTENSION );
39
- $ path = $ directory . DIRECTORY_SEPARATOR . $ path ;
40
- if ($ Parser = static ::get (__CLASS__ , "parsers. $ extension " )) {
41
- $ configurations = array_replace_recursive ($ configurations , $ Parser ::parse ($ path ));
42
- }
43
- }
44
- return Cache::instance ()->store ($ key , $ configurations );
45
- })->then (function ($ configurations ) {
46
- return static ::$ configurations = array_merge ($ configurations , static ::$ configurations );
47
- });
33
+ if ($ cache ) {
34
+ return Cache::instance ()->fetch (__METHOD__ )->then (null , function ($ key ) use ($ directory ) {
35
+ return static ::parse ($ directory );
36
+ })->then (function ($ configurations ) {
37
+ return Cache::instance ()->store (__METHOD__ , $ configurations );
38
+ });
39
+ } else {
40
+ return When::resolve (static ::parse ($ directory ));
41
+ }
48
42
case 'mysql ' :
49
43
default :
50
- throw new Exception (' Configuration scheme currently not supported ' );
44
+ throw new Exception (" Configuration scheme ' $ scheme ' currently not supported" );
51
45
}
52
46
}
53
47
@@ -92,15 +86,36 @@ public static function get($class, $key = null)
92
86
return $ configuration ;
93
87
}
94
88
95
- public static function set ($ class , $ key , $ value )
89
+ public static function set ($ class , $ key , $ value = null )
96
90
{
97
91
if (!isset (static ::$ configurations [$ class ])) {
98
92
static ::$ configurations [$ class ] = array ();
99
93
}
100
94
$ configuration = static ::$ configurations [$ class ];
101
- static ::$ configurations [$ class ] = array_replace_recursive ($ configuration , Parsers \Initialization::parse (array (
102
- $ key => $ value
103
- ), false ));
95
+ if (is_array ($ key )) {
96
+ $ newConfiguration = $ key ;
97
+ } else {
98
+ $ newConfiguration = Parsers \Initialization::parse (array (
99
+ $ key => $ value
100
+ ), false );
101
+ }
102
+ static ::$ configurations [$ class ] = array_replace_recursive ($ configuration , $ newConfiguration );
103
+ }
104
+
105
+ protected static function parse ($ directory )
106
+ {
107
+ $ configurations = array ();
108
+ if (!is_dir ($ directory )) {
109
+ throw new Exception ("Configuration directory $ directory is not valid. " );
110
+ }
111
+ foreach ((array ) scandir ($ directory ) as $ path ) {
112
+ $ extension = pathinfo ($ path , PATHINFO_EXTENSION );
113
+ $ path = $ directory . DIRECTORY_SEPARATOR . $ path ;
114
+ if ($ Parser = static ::get (__CLASS__ , "parsers. $ extension " )) {
115
+ $ configurations = array_replace_recursive ($ configurations , $ Parser ::parse ($ path ));
116
+ }
117
+ }
118
+ return static ::$ configurations = array_merge ($ configurations , static ::$ configurations );
104
119
}
105
120
}
106
121
0 commit comments