AbstractConfigFactory.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Traversable;
  11. use Zend\ServiceManager;
  12. /**
  13. * Class AbstractConfigFactory
  14. */
  15. class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $configs = array();
  21. /**
  22. * @var string[]
  23. */
  24. protected $defaultPatterns = array(
  25. '#config[\._-](.*)$#i',
  26. '#^(.*)[\\\\\._-]config$#i'
  27. );
  28. /**
  29. * @var string[]
  30. */
  31. protected $patterns;
  32. /**
  33. * Determine if we can create a service with name
  34. *
  35. * @param ServiceManager\ServiceLocatorInterface $serviceLocator
  36. * @param string $name
  37. * @param string $requestedName
  38. * @return bool
  39. */
  40. public function canCreateServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName)
  41. {
  42. if (isset($this->configs[$requestedName])) {
  43. return true;
  44. }
  45. if (!$serviceLocator->has('Config')) {
  46. return false;
  47. }
  48. $key = $this->match($requestedName);
  49. if (null === $key) {
  50. return false;
  51. }
  52. $config = $serviceLocator->get('Config');
  53. return isset($config[$key]);
  54. }
  55. /**
  56. * Create service with name
  57. *
  58. * @param ServiceManager\ServiceLocatorInterface $serviceLocator
  59. * @param string $name
  60. * @param string $requestedName
  61. * @return string|mixed|array
  62. */
  63. public function createServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName)
  64. {
  65. if (isset($this->configs[$requestedName])) {
  66. return $this->configs[$requestedName];
  67. }
  68. $key = $this->match($requestedName);
  69. if (isset($this->configs[$key])) {
  70. $this->configs[$requestedName] = $this->configs[$key];
  71. return $this->configs[$key];
  72. }
  73. $config = $serviceLocator->get('Config');
  74. $this->configs[$requestedName] = $this->configs[$key] = $config[$key];
  75. return $config;
  76. }
  77. /**
  78. * @param string $pattern
  79. * @return self
  80. * @throws Exception\InvalidArgumentException
  81. */
  82. public function addPattern($pattern)
  83. {
  84. if (!is_string($pattern)) {
  85. throw new Exception\InvalidArgumentException('pattern must be string');
  86. }
  87. $patterns = $this->getPatterns();
  88. array_unshift($patterns, $pattern);
  89. $this->setPatterns($patterns);
  90. return $this;
  91. }
  92. /**
  93. * @param array|Traversable $patterns
  94. * @return self
  95. * @throws Exception\InvalidArgumentException
  96. */
  97. public function addPatterns($patterns)
  98. {
  99. if ($patterns instanceof Traversable) {
  100. $patterns = iterator_to_array($patterns);
  101. }
  102. if (!is_array($patterns)) {
  103. throw new Exception\InvalidArgumentException("patterns must be array or Traversable");
  104. }
  105. foreach ($patterns as $pattern) {
  106. $this->addPattern($pattern);
  107. }
  108. return $this;
  109. }
  110. /**
  111. * @param array|Traversable $patterns
  112. * @return self
  113. * @throws \InvalidArgumentException
  114. */
  115. public function setPatterns($patterns)
  116. {
  117. if ($patterns instanceof Traversable) {
  118. $patterns = iterator_to_array($patterns);
  119. }
  120. if (!is_array($patterns)) {
  121. throw new \InvalidArgumentException("patterns must be array or Traversable");
  122. }
  123. $this->patterns = $patterns;
  124. return $this;
  125. }
  126. /**
  127. * @return array
  128. */
  129. public function getPatterns()
  130. {
  131. if (null === $this->patterns) {
  132. $this->setPatterns($this->defaultPatterns);
  133. }
  134. return $this->patterns;
  135. }
  136. /**
  137. * @param string $requestedName
  138. * @return null|string
  139. */
  140. protected function match($requestedName)
  141. {
  142. foreach ($this->getPatterns() as $pattern) {
  143. if (preg_match($pattern, $requestedName, $matches)) {
  144. return $matches[1];
  145. }
  146. }
  147. return null;
  148. }
  149. }