@@ -6,28 +6,28 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Config |
| 8 | 8 | { |
| 9 | - public $data; |
|
| 10 | - private $resources = array(); |
|
| 11 | - |
|
| 12 | - public function __construct(array $data) |
|
| 13 | - { |
|
| 14 | - $this->data = $data; |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - public function addResource(ResourceInterface $resource) |
|
| 18 | - { |
|
| 19 | - $this->resources[] = $resource; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function getResources() |
|
| 23 | - { |
|
| 24 | - return array_unique($this->resources); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public function addConfig(Config $config) |
|
| 28 | - { |
|
| 29 | - $this->data = array_replace_recursive($this->data, $config->data); |
|
| 30 | - |
|
| 31 | - $this->resources = array_merge($this->resources, $config->getResources()); |
|
| 32 | - } |
|
| 9 | + public $data; |
|
| 10 | + private $resources = array(); |
|
| 11 | + |
|
| 12 | + public function __construct(array $data) |
|
| 13 | + { |
|
| 14 | + $this->data = $data; |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + public function addResource(ResourceInterface $resource) |
|
| 18 | + { |
|
| 19 | + $this->resources[] = $resource; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function getResources() |
|
| 23 | + { |
|
| 24 | + return array_unique($this->resources); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public function addConfig(Config $config) |
|
| 28 | + { |
|
| 29 | + $this->data = array_replace_recursive($this->data, $config->data); |
|
| 30 | + |
|
| 31 | + $this->resources = array_merge($this->resources, $config->getResources()); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -11,64 +11,64 @@ |
||
| 11 | 11 | |
| 12 | 12 | class YamlConfigurationServiceProvider implements ServiceProviderInterface |
| 13 | 13 | { |
| 14 | - protected $cacheDirPath; |
|
| 15 | - protected $configFilePath; |
|
| 16 | - protected $configCacheFactory; |
|
| 17 | - |
|
| 18 | - public function __construct($configFilePath, $options = null) |
|
| 19 | - { |
|
| 20 | - if (is_array($options)) { |
|
| 21 | - if (isset($options['cache_dir'])) { |
|
| 22 | - $this->cacheDirPath = $options['cache_dir']; |
|
| 23 | - } |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - $this->configFilePath = $configFilePath; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function register(Application $app) |
|
| 30 | - { |
|
| 31 | - $app['config'] = $app->share(function(Application $app) { |
|
| 32 | - if ($this->cacheDirPath) { |
|
| 33 | - $cache = $this->getConfigCacheFactory($app['debug'])->cache($this->cacheDirPath.'/config.cache.php', |
|
| 34 | - function(ConfigCacheInterface $cache) { |
|
| 35 | - $config = $this->loadConfig(); |
|
| 36 | - |
|
| 37 | - $content = sprintf('<?php use Junker\Silex\Config; $c = new Config(%s);', var_export($config->data, true)).PHP_EOL; |
|
| 38 | - $content .= 'return $c;'; |
|
| 39 | - |
|
| 40 | - $cache->write($content, $config->getResources()); |
|
| 41 | - } |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - $config = include $cache->getPath(); |
|
| 45 | - } else { |
|
| 46 | - $config = $this->loadConfig(); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - return $config->data; |
|
| 50 | - }); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function boot(Application $app) |
|
| 54 | - { |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - private function getConfigCacheFactory($debug = false) |
|
| 58 | - { |
|
| 59 | - if ($this->configCacheFactory === null) { |
|
| 60 | - $this->configCacheFactory = new ConfigCacheFactory($debug); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - return $this->configCacheFactory; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - protected function loadConfig() |
|
| 67 | - { |
|
| 68 | - $loader = new YamlFileLoader(new FileLocator(dirname($this->configFilePath))); |
|
| 69 | - |
|
| 70 | - $config = $loader->load($this->configFilePath); |
|
| 71 | - |
|
| 72 | - return $config; |
|
| 73 | - } |
|
| 14 | + protected $cacheDirPath; |
|
| 15 | + protected $configFilePath; |
|
| 16 | + protected $configCacheFactory; |
|
| 17 | + |
|
| 18 | + public function __construct($configFilePath, $options = null) |
|
| 19 | + { |
|
| 20 | + if (is_array($options)) { |
|
| 21 | + if (isset($options['cache_dir'])) { |
|
| 22 | + $this->cacheDirPath = $options['cache_dir']; |
|
| 23 | + } |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + $this->configFilePath = $configFilePath; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function register(Application $app) |
|
| 30 | + { |
|
| 31 | + $app['config'] = $app->share(function(Application $app) { |
|
| 32 | + if ($this->cacheDirPath) { |
|
| 33 | + $cache = $this->getConfigCacheFactory($app['debug'])->cache($this->cacheDirPath.'/config.cache.php', |
|
| 34 | + function(ConfigCacheInterface $cache) { |
|
| 35 | + $config = $this->loadConfig(); |
|
| 36 | + |
|
| 37 | + $content = sprintf('<?php use Junker\Silex\Config; $c = new Config(%s);', var_export($config->data, true)).PHP_EOL; |
|
| 38 | + $content .= 'return $c;'; |
|
| 39 | + |
|
| 40 | + $cache->write($content, $config->getResources()); |
|
| 41 | + } |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + $config = include $cache->getPath(); |
|
| 45 | + } else { |
|
| 46 | + $config = $this->loadConfig(); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + return $config->data; |
|
| 50 | + }); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function boot(Application $app) |
|
| 54 | + { |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + private function getConfigCacheFactory($debug = false) |
|
| 58 | + { |
|
| 59 | + if ($this->configCacheFactory === null) { |
|
| 60 | + $this->configCacheFactory = new ConfigCacheFactory($debug); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + return $this->configCacheFactory; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + protected function loadConfig() |
|
| 67 | + { |
|
| 68 | + $loader = new YamlFileLoader(new FileLocator(dirname($this->configFilePath))); |
|
| 69 | + |
|
| 70 | + $config = $loader->load($this->configFilePath); |
|
| 71 | + |
|
| 72 | + return $config; |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -10,87 +10,87 @@ |
||
| 10 | 10 | |
| 11 | 11 | class YamlFileLoader extends FileLoader |
| 12 | 12 | { |
| 13 | - private $yamlParser; |
|
| 13 | + private $yamlParser; |
|
| 14 | 14 | |
| 15 | - public function load($file, $type = null) |
|
| 16 | - { |
|
| 17 | - $path = $this->locator->locate($file); |
|
| 15 | + public function load($file, $type = null) |
|
| 16 | + { |
|
| 17 | + $path = $this->locator->locate($file); |
|
| 18 | 18 | |
| 19 | - $data = $this->loadFile($path); |
|
| 19 | + $data = $this->loadFile($path); |
|
| 20 | 20 | |
| 21 | - // empty file |
|
| 22 | - if ($data === null) { |
|
| 23 | - $data = array(); |
|
| 24 | - } |
|
| 21 | + // empty file |
|
| 22 | + if ($data === null) { |
|
| 23 | + $data = array(); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - // not an array |
|
| 27 | - if (!is_array($data)) { |
|
| 28 | - throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file)); |
|
| 29 | - } |
|
| 26 | + // not an array |
|
| 27 | + if (!is_array($data)) { |
|
| 28 | + throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file)); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - $config = new Config($data); |
|
| 32 | - $config->addResource(new FileResource($path)); |
|
| 31 | + $config = new Config($data); |
|
| 32 | + $config->addResource(new FileResource($path)); |
|
| 33 | 33 | |
| 34 | - // imports |
|
| 35 | - $this->parseImports($config, $path); |
|
| 34 | + // imports |
|
| 35 | + $this->parseImports($config, $path); |
|
| 36 | 36 | |
| 37 | - return $config; |
|
| 38 | - } |
|
| 37 | + return $config; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - protected function loadFile($file) |
|
| 41 | - { |
|
| 42 | - if (!class_exists('Symfony\Component\Yaml\Parser')) { |
|
| 43 | - throw new \RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.'); |
|
| 44 | - } |
|
| 40 | + protected function loadFile($file) |
|
| 41 | + { |
|
| 42 | + if (!class_exists('Symfony\Component\Yaml\Parser')) { |
|
| 43 | + throw new \RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.'); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - if (!stream_is_local($file)) { |
|
| 47 | - throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $file)); |
|
| 48 | - } |
|
| 46 | + if (!stream_is_local($file)) { |
|
| 47 | + throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $file)); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - if (!file_exists($file)) { |
|
| 51 | - throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file)); |
|
| 52 | - } |
|
| 50 | + if (!file_exists($file)) { |
|
| 51 | + throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file)); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - if ($this->yamlParser === null) { |
|
| 55 | - $this->yamlParser = new YamlParser(); |
|
| 56 | - } |
|
| 54 | + if ($this->yamlParser === null) { |
|
| 55 | + $this->yamlParser = new YamlParser(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - try { |
|
| 59 | - $data = $this->yamlParser->parse(file_get_contents($file)); |
|
| 60 | - } catch (ParseException $e) { |
|
| 61 | - throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); |
|
| 62 | - } |
|
| 58 | + try { |
|
| 59 | + $data = $this->yamlParser->parse(file_get_contents($file)); |
|
| 60 | + } catch (ParseException $e) { |
|
| 61 | + throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - return $data; |
|
| 65 | - } |
|
| 64 | + return $data; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - private function parseImports(Config &$config, $file) |
|
| 68 | - { |
|
| 69 | - if (!isset($config->data['imports'])) { |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - if (!is_array($config->data['imports'])) { |
|
| 73 | - throw new \InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file)); |
|
| 74 | - } |
|
| 67 | + private function parseImports(Config &$config, $file) |
|
| 68 | + { |
|
| 69 | + if (!isset($config->data['imports'])) { |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + if (!is_array($config->data['imports'])) { |
|
| 73 | + throw new \InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file)); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - $defaultDirectory = dirname($file); |
|
| 76 | + $defaultDirectory = dirname($file); |
|
| 77 | 77 | |
| 78 | - foreach ($config->data['imports'] as $import) { |
|
| 79 | - if (!is_array($import)) { |
|
| 80 | - throw new \InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file)); |
|
| 81 | - } |
|
| 78 | + foreach ($config->data['imports'] as $import) { |
|
| 79 | + if (!is_array($import)) { |
|
| 80 | + throw new \InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file)); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - $this->setCurrentDir($defaultDirectory); |
|
| 84 | - $sub_config = $this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file); |
|
| 83 | + $this->setCurrentDir($defaultDirectory); |
|
| 84 | + $sub_config = $this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file); |
|
| 85 | 85 | |
| 86 | - $config->addConfig($sub_config); |
|
| 86 | + $config->addConfig($sub_config); |
|
| 87 | 87 | |
| 88 | - unset($config->data['imports']); |
|
| 89 | - } |
|
| 90 | - } |
|
| 88 | + unset($config->data['imports']); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - public function supports($resource, $type = null) |
|
| 93 | - { |
|
| 94 | - return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type); |
|
| 95 | - } |
|
| 92 | + public function supports($resource, $type = null) |
|
| 93 | + { |
|
| 94 | + return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type); |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -6,38 +6,38 @@ |
||
| 6 | 6 | |
| 7 | 7 | class ConfigTest extends \PHPUnit_Framework_TestCase |
| 8 | 8 | { |
| 9 | - const CONFIG_FILE = __DIR__ . '/../res/config.yml'; |
|
| 10 | - const CACHE_PATH = '/tmp/cache_config_123634f3d'; |
|
| 9 | + const CONFIG_FILE = __DIR__ . '/../res/config.yml'; |
|
| 10 | + const CACHE_PATH = '/tmp/cache_config_123634f3d'; |
|
| 11 | 11 | |
| 12 | - public function testConfig() |
|
| 13 | - { |
|
| 14 | - $app = new Application(); |
|
| 12 | + public function testConfig() |
|
| 13 | + { |
|
| 14 | + $app = new Application(); |
|
| 15 | 15 | |
| 16 | - $app->register(new YamlConfigurationServiceProvider(self::CONFIG_FILE)); |
|
| 16 | + $app->register(new YamlConfigurationServiceProvider(self::CONFIG_FILE)); |
|
| 17 | 17 | |
| 18 | - $this->assertEquals($app['config']['db']['pass'], '123123'); |
|
| 19 | - $this->assertEquals($app['config']['facebook']['debug'], true); |
|
| 20 | - $this->assertEquals(count($app['config']['security']['rules']['IS_AUTHENTICATED_ANONYMOUSLY']), 2); |
|
| 21 | - } |
|
| 18 | + $this->assertEquals($app['config']['db']['pass'], '123123'); |
|
| 19 | + $this->assertEquals($app['config']['facebook']['debug'], true); |
|
| 20 | + $this->assertEquals(count($app['config']['security']['rules']['IS_AUTHENTICATED_ANONYMOUSLY']), 2); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - public function testCache() |
|
| 24 | - { |
|
| 25 | - $app = new Application(); |
|
| 23 | + public function testCache() |
|
| 24 | + { |
|
| 25 | + $app = new Application(); |
|
| 26 | 26 | |
| 27 | - system("rm -rf " . escapeshellarg(self::CACHE_PATH)); |
|
| 27 | + system("rm -rf " . escapeshellarg(self::CACHE_PATH)); |
|
| 28 | 28 | |
| 29 | - $app->register(new YamlConfigurationServiceProvider(self::CONFIG_FILE, ['cache_dir' => self::CACHE_PATH])); |
|
| 29 | + $app->register(new YamlConfigurationServiceProvider(self::CONFIG_FILE, ['cache_dir' => self::CACHE_PATH])); |
|
| 30 | 30 | |
| 31 | - $this->assertEquals($app['config']['db']['pass'], '123123'); |
|
| 31 | + $this->assertEquals($app['config']['db']['pass'], '123123'); |
|
| 32 | 32 | |
| 33 | - $this->assertFileExists(self::CACHE_PATH . '/config.cache.php'); |
|
| 34 | - $this->assertFileExists(self::CACHE_PATH . '/config.cache.php.meta'); |
|
| 33 | + $this->assertFileExists(self::CACHE_PATH . '/config.cache.php'); |
|
| 34 | + $this->assertFileExists(self::CACHE_PATH . '/config.cache.php.meta'); |
|
| 35 | 35 | |
| 36 | - $app = new Application(); |
|
| 36 | + $app = new Application(); |
|
| 37 | 37 | |
| 38 | - $app->register(new YamlConfigurationServiceProvider(self::CONFIG_FILE, ['cache_dir' => self::CACHE_PATH])); |
|
| 38 | + $app->register(new YamlConfigurationServiceProvider(self::CONFIG_FILE, ['cache_dir' => self::CACHE_PATH])); |
|
| 39 | 39 | |
| 40 | - $this->assertEquals($app['config']['db']['pass'], '123123'); |
|
| 41 | - } |
|
| 40 | + $this->assertEquals($app['config']['db']['pass'], '123123'); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | } |