|
| 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