PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/monica/monica/vendor/zendframework/zendframework/library/Zend/Config/Factory.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 277 lines | 229 code | 10 blank | 38 comment | 4 complexity | 732c0c9850de3e7e987c353e5cead97f MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2013 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. * @return array|Config
  56. * @throws Exception\InvalidArgumentException
  57. * @throws Exception\RuntimeException
  58. */
  59. public static function fromFile($filename, $returnConfigObject = false)
  60. {
  61. $pathinfo = pathinfo($filename);
  62. if (!isset($pathinfo['extension'])) {
  63. throw new Exception\RuntimeException(sprintf(
  64. 'Filename "%s" is missing an extension and cannot be auto-detected',
  65. $filename
  66. ));
  67. }
  68. $extension = strtolower($pathinfo['extension']);
  69. if ($extension === 'php') {
  70. if (!is_file($filename) || !is_readable($filename)) {
  71. throw new Exception\RuntimeException(sprintf(
  72. "File '%s' doesn't exist or not readable",
  73. $filename
  74. ));
  75. }
  76. $config = include $filename;
  77. } elseif (isset(static::$extensions[$extension])) {
  78. $reader = static::$extensions[$extension];
  79. if (!$reader instanceof Reader\ReaderInterface) {
  80. $reader = static::getReaderPluginManager()->get($reader);
  81. static::$extensions[$extension] = $reader;
  82. }
  83. /** @var Reader\ReaderInterface $reader */
  84. $config = $reader->fromFile($filename);
  85. } else {
  86. throw new Exception\RuntimeException(sprintf(
  87. 'Unsupported config file extension: .%s',
  88. $pathinfo['extension']
  89. ));
  90. }
  91. return ($returnConfigObject) ? new Config($config) : $config;
  92. }
  93. /**
  94. * Read configuration from multiple files and merge them.
  95. *
  96. * @param array $files
  97. * @param bool $returnConfigObject
  98. * @return array|Config
  99. */
  100. public static function fromFiles(array $files, $returnConfigObject = false)
  101. {
  102. $config = array();
  103. foreach ($files as $file) {
  104. $config = ArrayUtils::merge($config, static::fromFile($file));
  105. }
  106. return ($returnConfigObject) ? new Config($config) : $config;
  107. }
  108. /**
  109. * Writes a config to a file
  110. *
  111. * @param string $filename
  112. * @param array|Config $config
  113. * @return boolean TRUE on success | FALSE on failure
  114. * @throws Exception\RuntimeException
  115. * @throws Exception\InvalidArgumentException
  116. */
  117. public static function toFile($filename, $config)
  118. {
  119. if (
  120. (is_object($config) && !($config instanceOf Config)) ||
  121. (!is_object($config) && !is_array($config))
  122. ) {
  123. throw new Exception\InvalidArgumentException(
  124. __METHOD__." \$config should be an array or instance of Zend\\Config\\Config"
  125. );
  126. }
  127. $extension = substr(strrchr($filename, '.'), 1);
  128. $directory = dirname($filename);
  129. if (!is_dir($directory)) {
  130. throw new Exception\RuntimeException(
  131. "Directory '{$directory}' does not exists!"
  132. );
  133. }
  134. if (!is_writable($directory)) {
  135. throw new Exception\RuntimeException(
  136. "Cannot write in directory '{$directory}'"
  137. );
  138. }
  139. if(!isset(self::$writerExtensions[$extension])) {
  140. throw new Exception\RuntimeException(
  141. "Unsupported config file extension: '.{$extension}' for writing."
  142. );
  143. }
  144. $writer = self::$writerExtensions[$extension];
  145. if (($writer instanceOf Writer\AbstractWriter) === false) {
  146. $writer = self::getWriterPluginManager()->get($writer);
  147. self::$writerExtensions[$extension] = $writer;
  148. }
  149. if (is_object($config)) {
  150. $config = $config->toArray();
  151. }
  152. $content = $writer->processConfig($config);
  153. return (bool) (file_put_contents($filename, $content) !== false);
  154. }
  155. /**
  156. * Set reader plugin manager
  157. *
  158. * @param ReaderPluginManager $readers
  159. * @return void
  160. */
  161. public static function setReaderPluginManager(ReaderPluginManager $readers)
  162. {
  163. static::$readers = $readers;
  164. }
  165. /**
  166. * Get the reader plugin manager
  167. *
  168. * @return ReaderPluginManager
  169. */
  170. public static function getReaderPluginManager()
  171. {
  172. if (static::$readers === null) {
  173. static::$readers = new ReaderPluginManager();
  174. }
  175. return static::$readers;
  176. }
  177. /**
  178. * Set writer plugin manager
  179. *
  180. * @param WriterPluginManager $writers
  181. * @return void
  182. */
  183. public static function setWriterPluginManager(WriterPluginManager $writers)
  184. {
  185. self::$writers = $writers;
  186. }
  187. /**
  188. * Get the writer plugin manager
  189. *
  190. * @return WriterPluginManager
  191. */
  192. public static function getWriterPluginManager()
  193. {
  194. if (static::$writers === null) {
  195. static::$writers = new WriterPluginManager();
  196. }
  197. return static::$writers;
  198. }
  199. /**
  200. * Set config reader for file extension
  201. *
  202. * @param string $extension
  203. * @param string|Reader\ReaderInterface $reader
  204. * @throws Exception\InvalidArgumentException
  205. * @return void
  206. */
  207. public static function registerReader($extension, $reader)
  208. {
  209. $extension = strtolower($extension);
  210. if (!is_string($reader) && !$reader instanceof Reader\ReaderInterface) {
  211. throw new Exception\InvalidArgumentException(sprintf(
  212. 'Reader should be plugin name, class name or ' .
  213. 'instance of %s\Reader\ReaderInterface; received "%s"',
  214. __NAMESPACE__,
  215. (is_object($reader) ? get_class($reader) : gettype($reader))
  216. ));
  217. }
  218. static::$extensions[$extension] = $reader;
  219. }
  220. /**
  221. * Set config writer for file extension
  222. *
  223. * @param string $extension
  224. * @param string|Writer\AbstractWriter $writer
  225. * @throw Exception\InvalidArgumentException
  226. * @return void
  227. */
  228. public static function registerWriter($extension, $writer)
  229. {
  230. $extension = strtolower($extension);
  231. if (!is_string($writer) && !$writer instanceof Writer\AbstractWriter) {
  232. throw new Exception\InvalidArgumentException(sprintf(
  233. 'Writer should be plugin name, class name or ' .
  234. 'instance of %s\Writer\AbstractWriter; received "%s"',
  235. __NAMESPACE__,
  236. (is_object($writer) ? get_class($writer) : gettype($writer))
  237. ));
  238. }
  239. self::$writerExtensions[$extension] = $writer;
  240. }
  241. }