Token.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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\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. * @return array
  160. */
  161. protected function buildMap()
  162. {
  163. if (null === $this->map) {
  164. if (!$this->suffix && !$this->prefix) {
  165. $this->map = $this->tokens;
  166. } else {
  167. $this->map = array();
  168. foreach ($this->tokens as $token => $value) {
  169. $this->map[$this->prefix . $token . $this->suffix] = $value;
  170. }
  171. }
  172. foreach (array_keys($this->map) as $key) {
  173. if (empty($key)) {
  174. unset($this->map[$key]);
  175. }
  176. }
  177. }
  178. return $this->map;
  179. }
  180. /**
  181. * Process
  182. *
  183. * @param Config $config
  184. * @return Config
  185. * @throws Exception\InvalidArgumentException
  186. */
  187. public function process(Config $config)
  188. {
  189. return $this->doProcess($config, $this->buildMap());
  190. }
  191. /**
  192. * Process a single value
  193. *
  194. * @param $value
  195. * @return mixed
  196. */
  197. public function processValue($value)
  198. {
  199. return $this->doProcess($value, $this->buildMap());
  200. }
  201. /**
  202. * Applies replacement map to the given value by modifying the value itself
  203. *
  204. * @param mixed $value
  205. * @param array $replacements
  206. *
  207. * @return mixed
  208. *
  209. * @throws Exception\InvalidArgumentException if the provided value is a read-only {@see Config}
  210. */
  211. private function doProcess($value, array $replacements)
  212. {
  213. if ($value instanceof Config) {
  214. if ($value->isReadOnly()) {
  215. throw new Exception\InvalidArgumentException('Cannot process config because it is read-only');
  216. }
  217. foreach ($value as $key => $val) {
  218. $value->$key = $this->doProcess($val, $replacements);
  219. }
  220. return $value;
  221. }
  222. if ($value instanceof Traversable || is_array($value)) {
  223. foreach ($value as & $val) {
  224. $val = $this->doProcess($val, $replacements);
  225. }
  226. return $value;
  227. }
  228. if (!is_string($value) && (is_bool($value) || is_numeric($value))) {
  229. $stringVal = (string) $value;
  230. $changedVal = strtr($value, $this->map);
  231. // replace the value only if a string replacement occurred
  232. if ($changedVal !== $stringVal) {
  233. return $changedVal;
  234. }
  235. return $value;
  236. }
  237. return strtr((string) $value, $this->map);
  238. }
  239. }