PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Config/Factory.php

https://bitbucket.org/gencer/zf2
PHP | 299 lines | 248 code | 12 blank | 39 comment | 7 complexity | fa3b99ba69b09a0780a376525406be1f MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Config;
  10. use Zend\Stdlib\ArrayUtils;
  11. class Factory
  12. {
  13. /**
  14. * Plugin manager for loading readers
  15. *
  16. * @var null|ReaderPluginManager
  17. */
  18. public static $readers = null;
  19. /**
  20. * Plugin manager for loading writers
  21. *
  22. * @var null|WriterPluginManager
  23. */
  24. public static $writers = null;
  25. /**
  26. * Registered config file extensions.
  27. * key is extension, value is reader instance or plugin name
  28. *
  29. * @var array
  30. */
  31. protected static $extensions = array(
  32. 'ini' => 'ini',
  33. 'json' => 'json',
  34. 'xml' => 'xml',
  35. 'yaml' => 'yaml',
  36. );
  37. /**
  38. * Register config file extensions for writing
  39. * key is extension, value is writer instance or plugin name
  40. *
  41. * @var array
  42. */
  43. protected static $writerExtensions = array(
  44. 'php' => 'php',
  45. 'ini' => 'ini',
  46. 'json' => 'json',
  47. 'xml' => 'xml',
  48. 'yaml' => 'yaml',
  49. );
  50. /**
  51. * Read a config from a file.
  52. *
  53. * @param string $filename
  54. * @param bool $returnConfigObject
  55. * @param bool $useIncludePath
  56. * @return array|Config
  57. * @throws Exception\InvalidArgumentException
  58. * @throws Exception\RuntimeException
  59. */
  60. public static function fromFile($filename, $returnConfigObject = false, $useIncludePath = false)
  61. {
  62. $filepath = $filename;
  63. if (!file_exists($filename)) {
  64. if (!$useIncludePath) {
  65. throw new Exception\RuntimeException(sprintf(
  66. 'Filename "%s" cannot be found relative to the working directory',
  67. $filename
  68. ));
  69. }
  70. $fromIncludePath = stream_resolve_include_path($filename);
  71. if (!$fromIncludePath) {
  72. throw new Exception\RuntimeException(sprintf(
  73. 'Filename "%s" cannot be found relative to the working directory or the include_path ("%s")',
  74. $filename,
  75. get_include_path()
  76. ));
  77. }
  78. $filepath = $fromIncludePath;
  79. }
  80. $pathinfo = pathinfo($filepath);
  81. if (!isset($pathinfo['extension'])) {
  82. throw new Exception\RuntimeException(sprintf(
  83. 'Filename "%s" is missing an extension and cannot be auto-detected',
  84. $filename
  85. ));
  86. }
  87. $extension = strtolower($pathinfo['extension']);
  88. if ($extension === 'php') {
  89. if (!is_file($filepath) || !is_readable($filepath)) {
  90. throw new Exception\RuntimeException(sprintf(
  91. "File '%s' doesn't exist or not readable",
  92. $filename
  93. ));
  94. }
  95. $config = include $filepath;
  96. } elseif (isset(static::$extensions[$extension])) {
  97. $reader = static::$extensions[$extension];
  98. if (!$reader instanceof Reader\ReaderInterface) {
  99. $reader = static::getReaderPluginManager()->get($reader);
  100. static::$extensions[$extension] = $reader;
  101. }
  102. /** @var Reader\ReaderInterface $reader */
  103. $config = $reader->fromFile($filepath);
  104. } else {
  105. throw new Exception\RuntimeException(sprintf(
  106. 'Unsupported config file extension: .%s',
  107. $pathinfo['extension']
  108. ));
  109. }
  110. return ($returnConfigObject) ? new Config($config) : $config;
  111. }
  112. /**
  113. * Read configuration from multiple files and merge them.
  114. *
  115. * @param array $files
  116. * @param bool $returnConfigObject
  117. * @param bool $useIncludePath
  118. * @return array|Config
  119. */
  120. public static function fromFiles(array $files, $returnConfigObject = false, $useIncludePath = false)
  121. {
  122. $config = array();
  123. foreach ($files as $file) {
  124. $config = ArrayUtils::merge($config, static::fromFile($file, false, $useIncludePath));
  125. }
  126. return ($returnConfigObject) ? new Config($config) : $config;
  127. }
  128. /**
  129. * Writes a config to a file
  130. *
  131. * @param string $filename
  132. * @param array|Config $config
  133. * @return bool TRUE on success | FALSE on failure
  134. * @throws Exception\RuntimeException
  135. * @throws Exception\InvalidArgumentException
  136. */
  137. public static function toFile($filename, $config)
  138. {
  139. if (
  140. (is_object($config) && !($config instanceOf Config)) ||
  141. (!is_object($config) && !is_array($config))
  142. ) {
  143. throw new Exception\InvalidArgumentException(
  144. __METHOD__." \$config should be an array or instance of Zend\\Config\\Config"
  145. );
  146. }
  147. $extension = substr(strrchr($filename, '.'), 1);
  148. $directory = dirname($filename);
  149. if (!is_dir($directory)) {
  150. throw new Exception\RuntimeException(
  151. "Directory '{$directory}' does not exists!"
  152. );
  153. }
  154. if (!is_writable($directory)) {
  155. throw new Exception\RuntimeException(
  156. "Cannot write in directory '{$directory}'"
  157. );
  158. }
  159. if (!isset(static::$writerExtensions[$extension])) {
  160. throw new Exception\RuntimeException(
  161. "Unsupported config file extension: '.{$extension}' for writing."
  162. );
  163. }
  164. $writer = static::$writerExtensions[$extension];
  165. if (($writer instanceOf Writer\AbstractWriter) === false) {
  166. $writer = self::getWriterPluginManager()->get($writer);
  167. static::$writerExtensions[$extension] = $writer;
  168. }
  169. if (is_object($config)) {
  170. $config = $config->toArray();
  171. }
  172. $content = $writer->processConfig($config);
  173. return (bool) (file_put_contents($filename, $content) !== false);
  174. }
  175. /**
  176. * Set reader plugin manager
  177. *
  178. * @param ReaderPluginManager $readers
  179. * @return void
  180. */
  181. public static function setReaderPluginManager(ReaderPluginManager $readers)
  182. {
  183. static::$readers = $readers;
  184. }
  185. /**
  186. * Get the reader plugin manager
  187. *
  188. * @return ReaderPluginManager
  189. */
  190. public static function getReaderPluginManager()
  191. {
  192. if (static::$readers === null) {
  193. static::$readers = new ReaderPluginManager();
  194. }
  195. return static::$readers;
  196. }
  197. /**
  198. * Set writer plugin manager
  199. *
  200. * @param WriterPluginManager $writers
  201. * @return void
  202. */
  203. public static function setWriterPluginManager(WriterPluginManager $writers)
  204. {
  205. static::$writers = $writers;
  206. }
  207. /**
  208. * Get the writer plugin manager
  209. *
  210. * @return WriterPluginManager
  211. */
  212. public static function getWriterPluginManager()
  213. {
  214. if (static::$writers === null) {
  215. static::$writers = new WriterPluginManager();
  216. }
  217. return static::$writers;
  218. }
  219. /**
  220. * Set config reader for file extension
  221. *
  222. * @param string $extension
  223. * @param string|Reader\ReaderInterface $reader
  224. * @throws Exception\InvalidArgumentException
  225. * @return void
  226. */
  227. public static function registerReader($extension, $reader)
  228. {
  229. $extension = strtolower($extension);
  230. if (!is_string($reader) && !$reader instanceof Reader\ReaderInterface) {
  231. throw new Exception\InvalidArgumentException(sprintf(
  232. 'Reader should be plugin name, class name or ' .
  233. 'instance of %s\Reader\ReaderInterface; received "%s"',
  234. __NAMESPACE__,
  235. (is_object($reader) ? get_class($reader) : gettype($reader))
  236. ));
  237. }
  238. static::$extensions[$extension] = $reader;
  239. }
  240. /**
  241. * Set config writer for file extension
  242. *
  243. * @param string $extension
  244. * @param string|Writer\AbstractWriter $writer
  245. * @throws Exception\InvalidArgumentException
  246. * @return void
  247. */
  248. public static function registerWriter($extension, $writer)
  249. {
  250. $extension = strtolower($extension);
  251. if (!is_string($writer) && !$writer instanceof Writer\AbstractWriter) {
  252. throw new Exception\InvalidArgumentException(sprintf(
  253. 'Writer should be plugin name, class name or ' .
  254. 'instance of %s\Writer\AbstractWriter; received "%s"',
  255. __NAMESPACE__,
  256. (is_object($writer) ? get_class($writer) : gettype($writer))
  257. ));
  258. }
  259. static::$writerExtensions[$extension] = $writer;
  260. }
  261. }