Skip to content

Commit 779ffba

Browse files
author
Giovanni Lovato
committed
Initial commit
0 parents  commit 779ffba

File tree

6 files changed

+447
-0
lines changed

6 files changed

+447
-0
lines changed

README.md

Whitespace-only changes.

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name" : "bread/configuration",
3+
"description" : "Bread configuration library",
4+
"type" : "library",
5+
"authors" : [{
6+
"name" : "Giovanni Lovato",
7+
"email" : "heruan@aldu.net"
8+
}
9+
],
10+
"keywords" : [
11+
"bread",
12+
"configuration"
13+
],
14+
"homepage" : "https://github.com/breadphp/configuration",
15+
"license" : [
16+
"CC-BY-3.0"
17+
],
18+
"require" : {
19+
"bread/caching" : "~1.0",
20+
"php" : ">=5.4"
21+
},
22+
"autoload" : {
23+
"psr-0" : {
24+
"Bread\\Configuration" : "src"
25+
}
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Bread PHP Framework (http://github.com/saiv/Bread)
4+
* Copyright 2010-2012, SAIV Development Team <development@saiv.it>
5+
*
6+
* Licensed under a Creative Commons Attribution 3.0 Unported License.
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @copyright Copyright 2010-2012, SAIV Development Team <development@saiv.it>
10+
* @link http://github.com/saiv/Bread Bread PHP Framework
11+
* @package Bread
12+
* @since Bread PHP Framework
13+
* @license http://creativecommons.org/licenses/by/3.0/
14+
*/
15+
namespace Bread\Configuration\Interfaces;
16+
17+
/**
18+
* The interface for configuration parsers.
19+
*
20+
* @author Giovanni Lovato <heruan@aldu.net>
21+
*/
22+
interface Parser
23+
{
24+
25+
/**
26+
* Parses a configuration file and returns an array with configuration entries.
27+
*
28+
* @param string $filename
29+
* The file to parse
30+
* @return array The configuration array
31+
*/
32+
public static function parse($filename);
33+
}

src/Bread/Configuration/Manager.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Bread PHP Framework (http://github.com/saiv/Bread)
4+
* Copyright 2010-2012, SAIV Development Team <development@saiv.it>
5+
*
6+
* Licensed under a Creative Commons Attribution 3.0 Unported License.
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @copyright Copyright 2010-2012, SAIV Development Team <development@saiv.it>
10+
* @link http://github.com/saiv/Bread Bread PHP Framework
11+
* @package Bread
12+
* @since Bread PHP Framework
13+
* @license http://creativecommons.org/licenses/by/3.0/
14+
*/
15+
namespace Bread\Configuration;
16+
17+
use Bread\Caching\Cache;
18+
use Exception;
19+
20+
class Manager
21+
{
22+
23+
private static $defaults = array();
24+
25+
private static $configurations = array();
26+
27+
public static function initialize($url)
28+
{
29+
$directory = parse_url($url, PHP_URL_PATH);
30+
return Cache::instance()->fetch(__METHOD__)->then(null, function ($key) use ($directory) {
31+
$configurations = array();
32+
if (!is_dir($directory)) {
33+
throw new Exception("Configuration directory $directory is not valid.");
34+
}
35+
foreach ((array) scandir($directory) as $path) {
36+
$extension = pathinfo($path, PATHINFO_EXTENSION);
37+
$path = $directory . DIRECTORY_SEPARATOR . $path;
38+
if ($Parser = static::get(__CLASS__, "parsers.$extension")) {
39+
$configurations = array_replace_recursive($configurations, $Parser::parse($path));
40+
}
41+
}
42+
Cache::instance()->store($key, $configurations);
43+
return $configurations;
44+
})->then(function ($configurations) {
45+
static::$configurations = array_merge($configurations, static::$configurations);
46+
});
47+
}
48+
49+
public static function defaults($class, $configuration = array())
50+
{
51+
if ($parent = get_parent_class($class)) {
52+
$configuration = array_replace_recursive(static::get($parent), $configuration);
53+
}
54+
foreach (class_uses($class) as $trait) {
55+
$configuration = array_replace_recursive(static::get($trait), $configuration);
56+
}
57+
if (isset(static::$configurations[$class])) {
58+
$configuration = array_replace_recursive($configuration, static::$configurations[$class]);
59+
}
60+
static::$configurations[$class] = $configuration;
61+
}
62+
63+
public static function configure($class, $configuration = array())
64+
{
65+
if (!isset(static::$configurations[$class])) {
66+
static::$configurations[$class] = array();
67+
}
68+
static::$configurations[$class] = array_replace_recursive(static::$configurations[$class], $configuration);
69+
}
70+
71+
public static function get($class, $key = null)
72+
{
73+
static::defaults($class);
74+
if (!isset(static::$configurations[$class])) {
75+
return null;
76+
}
77+
$configuration = static::$configurations[$class];
78+
if (null === $key) {
79+
return $configuration;
80+
}
81+
foreach (explode('.', $key) as $key) {
82+
if (!isset($configuration[$key])) {
83+
return null;
84+
}
85+
$configuration = $configuration[$key];
86+
}
87+
return $configuration;
88+
}
89+
90+
public static function set($class, $key, $value)
91+
{
92+
if (!isset(static::$configurations[$class])) {
93+
static::$configurations[$class] = array();
94+
}
95+
$configuration = static::$configurations[$class];
96+
static::$configurations[$class] = array_replace_recursive($configuration, Parsers\Initialization::parse(array(
97+
$key => $value
98+
), false));
99+
}
100+
}
101+
102+
Manager::defaults('Bread\Configuration\Manager', array(
103+
'parsers' => array(
104+
'ini' => 'Bread\Configuration\Parsers\Initialization',
105+
'php' => 'Bread\Configuration\Parsers\PHP'
106+
)
107+
));

0 commit comments

Comments
 (0)