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:
<?php
namespace Cz\InnuDb;
use Nette;
class Loader
{
protected $adapters = array(
'php' => 'Nette\Config\Adapters\PhpAdapter',
'ini' => 'Nette\Config\Adapters\IniAdapter',
'neon' => 'Nette\Config\Adapters\NeonAdapter',
'json' => 'Cz\InnuDb\Adapters\JsonAdapter',
);
public function load($file)
{
if(!is_file($file) || !is_readable($file))
{
throw new Nette\FileNotFoundException("File '$file' is missing or is not readable.");
}
return $this->getAdapter($file)->load($file);
}
public function save($file, $data)
{
if(file_put_contents($file, $this->getAdapter($file)->dump($data)) === FALSE)
{
throw new Nette\IOException("Cannot write file '$file'.");
}
}
public function addAdapter($extension, $adapter)
{
$this->adapters[strtolower($extension)] = $adapter;
return $this;
}
private function getAdapter($file)
{
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if(!isset($this->adapters[$extension]))
{
throw new Nette\InvalidArgumentException("Unknown file extension '$file'.");
}
return is_object($this->adapters[$extension]) ? $this->adapters[$extension] : new $this->adapters[$extension];
}
}