@@ -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 | } |
@@ -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; |
@@ -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 | } |
@@ -28,10 +28,10 @@ |
||
28 | 28 | |
29 | 29 | public function register(Application $app) |
30 | 30 | { |
31 | - $app['config'] = $app->share(function (Application $app) { |
|
31 | + $app['config'] = $app->share(function(Application $app) { |
|
32 | 32 | if ($this->cacheDirPath) { |
33 | 33 | $cache = $this->getConfigCacheFactory($app['debug'])->cache($this->cacheDirPath.'/config.cache.php', |
34 | - function (ConfigCacheInterface $cache) { |
|
34 | + function(ConfigCacheInterface $cache) { |
|
35 | 35 | $config = $this->loadConfig(); |
36 | 36 | |
37 | 37 | $content = sprintf('<?php use Junker\Silex\Config; $c = new Config(%s);', var_export($config->data, true)).PHP_EOL; |