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:
<?php
namespace CzProject\Configuration;
class ConfigLoader
{
protected $adapters = array();
public function addAdapter($extension, IAdapter $adapter)
{
$this->adapters[strtolower($extension)] = $adapter;
return $this;
}
public function loadConfig($file)
{
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($extension === '') {
throw new ConfigLoaderException("Missing extension for file '$file'.");
}
if (!isset($this->adapters[$extension])) {
throw new ConfigLoaderException("Missing adapter for extension '$extension', file '$file'.");
}
$adapter = $this->adapters[$extension];
if ($adapter instanceof IAdapterLoader) {
return (array) $adapter->loadConfig($file);
}
if ($adapter instanceof IAdapterParser) {
$content = file_get_contents($file);
if ($content === FALSE) {
throw new ConfigLoaderException("Reading of file '$file' failed.");
}
return (array) $adapter->parseConfig($content);
}
throw new ConfigLoaderException("Adapter must implement IAdapterLoader or IAdapterParser.");
}
}