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