Skip to content

Commit f0b312d

Browse files
author
Giovanni Lovato
committed
New $cache parameter
1 parent b07f7fe commit f0b312d

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
"CC-BY-3.0"
1717
],
1818
"require" : {
19-
"bread/caching" : "~1.0",
20-
"php" : ">=5.4"
19+
"php" : ">=5.4",
20+
"bread/caching" : "~1.0"
2121
},
2222
"autoload" : {
2323
"psr-0" : {
2424
"Bread\\Configuration" : "src"
2525
}
2626
}
27-
}
27+
}

src/Bread/Configuration/Manager.php

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Bread\Configuration;
1616

1717
use Bread\Caching\Cache;
18+
use Bread\Promises\When;
1819
use Exception;
1920

2021
class Manager
@@ -24,30 +25,23 @@ class Manager
2425

2526
private static $configurations = array();
2627

27-
public static function initialize($url)
28+
public static function initialize($url, $cache = false)
2829
{
29-
switch (parse_url($url, PHP_URL_SCHEME)) {
30+
switch ($scheme = parse_url($url, PHP_URL_SCHEME)) {
3031
case 'file':
3132
$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+
}
4842
case 'mysql':
4943
default:
50-
throw new Exception('Configuration scheme currently not supported');
44+
throw new Exception("Configuration scheme '$scheme' currently not supported");
5145
}
5246
}
5347

@@ -92,15 +86,36 @@ public static function get($class, $key = null)
9286
return $configuration;
9387
}
9488

95-
public static function set($class, $key, $value)
89+
public static function set($class, $key, $value = null)
9690
{
9791
if (!isset(static::$configurations[$class])) {
9892
static::$configurations[$class] = array();
9993
}
10094
$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);
104119
}
105120
}
106121

0 commit comments

Comments
 (0)