Token.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\Processor;
  10. use Traversable;
  11. use Zend\Config\Config;
  12. use Zend\Config\Exception;
  13. class Token implements ProcessorInterface
  14. {
  15. /**
  16. * Token prefix.
  17. *
  18. * @var string
  19. */
  20. protected $prefix = '';
  21. /**
  22. * Token suffix.
  23. *
  24. * @var string
  25. */
  26. protected $suffix = '';
  27. /**
  28. * The registry of tokens
  29. *
  30. * @var array
  31. */
  32. protected $tokens = array();
  33. /**
  34. * Replacement map
  35. *
  36. * @var array
  37. */
  38. protected $map = null;
  39. /**
  40. * Token Processor walks through a Config structure and replaces all
  41. * occurrences of tokens with supplied values.
  42. *
  43. * @param array|Config|Traversable $tokens Associative array of TOKEN => value
  44. * to replace it with
  45. * @param string $prefix
  46. * @param string $suffix
  47. * @internal param array $options
  48. * @return Token
  49. */
  50. public function __construct($tokens = array(), $prefix = '', $suffix = '')
  51. {
  52. $this->setTokens($tokens);
  53. $this->setPrefix($prefix);
  54. $this->setSuffix($suffix);
  55. }
  56. /**
  57. * @param string $prefix
  58. * @return Token
  59. */
  60. public function setPrefix($prefix)
  61. {
  62. // reset map
  63. $this->map = null;
  64. $this->prefix = $prefix;
  65. return $this;
  66. }
  67. /**
  68. * @return string
  69. */
  70. public function getPrefix()
  71. {
  72. return $this->prefix;
  73. }
  74. /**
  75. * @param string $suffix
  76. * @return Token
  77. */
  78. public function setSuffix($suffix)
  79. {
  80. // reset map
  81. $this->map = null;
  82. $this->suffix = $suffix;
  83. return $this;
  84. }
  85. /**
  86. * @return string
  87. */
  88. public function getSuffix()
  89. {
  90. return $this->suffix;
  91. }
  92. /**
  93. * Set token registry.
  94. *
  95. * @param array|Config|Traversable $tokens Associative array of TOKEN => value
  96. * to replace it with
  97. * @return Token
  98. * @throws Exception\InvalidArgumentException
  99. */
  100. public function setTokens($tokens)
  101. {
  102. if (is_array($tokens)) {
  103. $this->tokens = $tokens;
  104. } elseif ($tokens instanceof Config) {
  105. $this->tokens = $tokens->toArray();
  106. } elseif ($tokens instanceof Traversable) {
  107. $this->tokens = array();
  108. foreach ($tokens as $key => $val) {
  109. $this->tokens[$key] = $val;
  110. }
  111. } else {
  112. throw new Exception\InvalidArgumentException('Cannot use ' . gettype($tokens) . ' as token registry.');
  113. }
  114. // reset map
  115. $this->map = null;
  116. return $this;
  117. }
  118. /**
  119. * Get current token registry.
  120. *
  121. * @return array
  122. */
  123. public function getTokens()
  124. {
  125. return $this->tokens;
  126. }
  127. /**
  128. * Add new token.
  129. *
  130. * @param string $token
  131. * @param mixed $value
  132. * @return Token
  133. * @throws Exception\InvalidArgumentException
  134. */
  135. public function addToken($token, $value)
  136. {
  137. if (!is_scalar($token)) {
  138. throw new Exception\InvalidArgumentException('Cannot use ' . gettype($token) . ' as token name.');
  139. }
  140. $this->tokens[$token] = $value;
  141. // reset map
  142. $this->map = null;
  143. return $this;
  144. }
  145. /**
  146. * Add new token.
  147. *
  148. * @param string $token
  149. * @param mixed $value
  150. * @return Token
  151. */
  152. public function setToken($token, $value)
  153. {
  154. return $this->addToken($token, $value);
  155. }
  156. /**
  157. * Build replacement map
  158. */
  159. protected function buildMap()
  160. {
  161. if (!$this->suffix && !$this->prefix) {
  162. $this->map = $this->tokens;
  163. } else {
  164. $this->map = array();
  165. foreach ($this->tokens as $token => $value) {
  166. $this->map[$this->prefix . $token . $this->suffix] = $value;
  167. }
  168. }
  169. }
  170. /**
  171. * Process
  172. *
  173. * @param Config $config
  174. * @return Config
  175. * @throws Exception\InvalidArgumentException
  176. */
  177. public function process(Config $config)
  178. {
  179. if ($config->isReadOnly()) {
  180. throw new Exception\InvalidArgumentException('Cannot process config because it is read-only');
  181. }
  182. if ($this->map === null) {
  183. $this->buildMap();
  184. }
  185. /**
  186. * Walk through config and replace values
  187. */
  188. $keys = array_keys($this->map);
  189. $values = array_values($this->map);
  190. foreach ($config as $key => $val) {
  191. if ($val instanceof Config) {
  192. $this->process($val);
  193. } else {
  194. $config->$key = str_replace($keys, $values, $val);
  195. }
  196. }
  197. return $config;
  198. }
  199. /**
  200. * Process a single value
  201. *
  202. * @param $value
  203. * @return mixed
  204. */
  205. public function processValue($value)
  206. {
  207. if ($this->map === null) {
  208. $this->buildMap();
  209. }
  210. $keys = array_keys($this->map);
  211. $values = array_values($this->map);
  212. return str_replace($keys, $values, $value);
  213. }
  214. }