1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie;
use ICanBoogie\Storage\Storage;
/**
* Provides synthesized low-level configurations.
*/
class Config implements \ArrayAccess
{
static private $require_cache = [];
static private function isolated_require($__FILE__)
{
if (isset(self::$require_cache[$__FILE__]))
{
return self::$require_cache[$__FILE__];
}
return self::$require_cache[$__FILE__] = require $__FILE__;
}
/**
* An array of key/value where _key_ is a path to a config directory and _value_ is its weight.
* The array is sorted according to the weight of the paths.
*
* @var array
*/
protected $paths = [];
/**
* Callbacks to synthesize the configurations.
*
* @var array
*/
protected $synthesizers = [];
/**
* Synthesized configurations.
*
* @var array
*/
protected $synthesized = [];
/**
* A cache to store and retrieve the synthesized configurations.
*
* @var Storage
*/
public $cache;
/**
* Initialize the {@link $paths}, {@link $synthesizers}, and {@link $cache} properties.
*
* @param array $paths An array of key/value pairs where _key_ is the path to a config
* directory and _value_ is the weight of that path.
* @param array $synthesizers
* @param Storage $cache A cache for synthesized configurations.
*/
public function __construct(array $paths, array $synthesizers = [], Storage $cache = null)
{
$this->synthesizers = $synthesizers;
$this->cache = $cache;
$this->add($paths);
}
/**
* @inheritdoc
*
* @throws OffsetNotWritable in attempt to set a configuration.
*/
public function offsetSet($offset, $value)
{
throw new OffsetNotWritable([ $offset, $this ]);
}
/**
* Checks if a config has been synthesized.
*
* @param string $id The identifier of the config.
*
* @return bool `true` if the config has been synthesized, `false` otherwise.
*/
public function offsetExists($id)
{
return isset($this->synthesized[$id]);
}
/**
* @inheritdoc
*
* @throws OffsetNotWritable in attempt to unset an offset.
*/
public function offsetUnset($offset)
{
throw new OffsetNotWritable([ $offset, $this ]);
}
/**
* Returns the specified synthesized configuration.
*
* @param string $id The identifier of the config.
*
* @return mixed
*
* @throws \InvalidArgumentException in attempt to obtain an undefined config.
*/
public function offsetGet($id)
{
if ($this->offsetExists($id))
{
return $this->synthesized[$id];
}
if (empty($this->synthesizers[$id]))
{
throw new \InvalidArgumentException("There is no constructor defined to build the $id config.");
}
list($synthesizer, $from) = $this->synthesizers[$id] + [ 1 => $id ];
$started_at = microtime(true);
$config = $this->synthesize($id, $synthesizer, $from);
ConfigProfiler::add($started_at, $id, $synthesizer);
return $config;
}
/**
* @var string
*/
private $cache_key;
/**
* Build a cache key according to the current paths and the config name.
*
* @param string $name
*
* @return string
*/
private function get_cache_key($name)
{
if (!$this->cache_key)
{
$this->cache_key = substr(sha1(implode('|', array_keys($this->paths))), 0, 8);
}
return $this->cache_key . '_' . $name;
}
/**
* Revokes the synthesized configs and the cache key.
*
* The method is usually called after the config paths have been modified.
*/
protected function revoke()
{
$this->synthesized = [];
$this->cache_key = null;
}
/**
* Adds a path or several paths to the config.
*
* Paths are sorted according to their weight. The order in which they were defined is
* preserved for paths with the same weight.
*
* <pre>
* <?php
*
* $config->add('/path/to/config', 10);
* $config->add([
*
* '/path1/to/config' => 10,
* '/path2/to/config' => 10,
* '/path2/to/config' => -10
*
* ]);
* </pre>
*
* @param string|array $path
* @param int $weight Weight of the path. The argument is discarded if `$path` is an array.
*
* @throws \InvalidArgumentException if the path is empty.
*/
public function add($path, $weight = 0)
{
if (!$path)
{
throw new \InvalidArgumentException('$path is empty.');
}
$paths = $this->paths;
if (is_array($path))
{
$paths = array_merge($paths, $path);
}
else
{
$paths[$path] = $weight;
}
stable_sort($paths);
$this->paths = $paths;
$this->revoke();
}
/**
* Returns the fragments of a configuration.
*
* @param string $name Name of the configuration.
*
* @return array Where _key_ is the pathname to the fragment file and _value_ the value
* returned when the file was required.
*/
public function get_fragments($name)
{
$fragments = [];
$filename = $name . '.php';
foreach ($this->paths as $path => $weight)
{
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$pathname = $path . $filename;
if (!file_exists($pathname))
{
continue;
}
$fragments[$path . $filename] = self::isolated_require($pathname);
}
return $fragments;
}
/**
* Synthesize a configuration.
*
* @param string $name Name of the configuration to synthesize.
* @param string|array $synthesizer Callback for the synthesis.
* @param null|string $from If the configuration is a derivative $from is the name
* of the source configuration.
*
* @return mixed
*/
public function synthesize($name, $synthesizer, $from = null)
{
if (array_key_exists($name, $this->synthesized))
{
return $this->synthesized[$name];
}
$cache = $this->cache;
$cache_key = $this->get_cache_key($name);
if ($cache)
{
$config = $cache->retrieve($cache_key);
if ($config !== null)
{
return $this->synthesized[$name] = $config;
}
}
$config = $this->synthesize_for_real($from ?: $name, $synthesizer);
if ($cache)
{
$cache->store($cache_key, $config);
}
return $this->synthesized[$name] = $config;
}
private function synthesize_for_real($name, $synthesizer)
{
$fragments = $this->get_fragments($name);
if (!$fragments)
{
return null;
}
if ($synthesizer == 'merge')
{
return call_user_func_array('array_merge', $fragments);
}
if ($synthesizer == 'recursive merge')
{
return call_user_func_array('ICanBoogie\array_merge_recursive', $fragments);
}
return call_user_func($synthesizer, $fragments);
}
}