AbstractConfigFactory.php 5.0 KB

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