|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ConfigToken; |
|
4
|
|
|
|
|
5
|
|
|
use ConfigToken\TokenResolver\Exception\UnknownTokenSourceException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Provides a method to inject the resolved token values collection back into the original string. |
|
9
|
|
|
*/ |
|
10
|
|
|
class TokenInjector |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Inject the resolved token values in the given string. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $string The string where to inject the resolved token values. |
|
16
|
|
|
* @param TokenCollection $tokens The tokens. |
|
17
|
|
|
* @param boolean $checkHash If true, the hash of the string must match the source hash of the tokens. |
|
18
|
|
|
* @throws UnknownTokenSourceException |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
7 |
|
public static function injectString($string, TokenCollection $tokens, $checkHash = False) { |
|
22
|
7 |
|
if ($checkHash) { |
|
23
|
1 |
|
$hash = md5($string); |
|
24
|
1 |
|
if ($tokens->hasSourceHash() && ($tokens->getSourceHash() !== $hash)) { |
|
25
|
|
|
throw new UnknownTokenSourceException('Unable to inject tokens.'); |
|
26
|
|
|
} |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
7 |
|
$offsets = array(); |
|
30
|
7 |
|
$allTokens = $tokens->getArray(); |
|
31
|
7 |
|
foreach ($allTokens as $tokenString => $token) { |
|
32
|
7 |
|
$tokenOffsets = $token->getOffsets(); |
|
33
|
7 |
|
foreach ($tokenOffsets as $offset) { |
|
34
|
7 |
|
$offsets[$offset] = $tokenString; |
|
35
|
7 |
|
} |
|
36
|
7 |
|
} |
|
37
|
7 |
|
ksort($offsets); |
|
38
|
|
|
|
|
39
|
7 |
|
$blocks = array(); |
|
40
|
7 |
|
$lastOffset = 0; |
|
41
|
7 |
|
$offsetDelta = 0; |
|
42
|
|
|
/** @var Token[] $injected */ |
|
43
|
7 |
|
$injected = array(); |
|
44
|
7 |
|
foreach ($offsets as $offset => $tokenString) { |
|
45
|
|
|
/** @var Token $token */ |
|
46
|
7 |
|
$token = $allTokens[$tokenString]; |
|
47
|
7 |
|
if (!$token->getIsInjected()) { |
|
48
|
7 |
|
$token->adjustOffset($offset, $offsetDelta); |
|
49
|
7 |
|
if ($token->getIsResolved()) { |
|
50
|
7 |
|
$tokenValue = $token->getTokenValue(); |
|
51
|
7 |
|
$blocks[] = substr($string, $lastOffset, $offset - $lastOffset); |
|
52
|
7 |
|
$blocks[] = $tokenValue; |
|
53
|
7 |
|
$lastOffset = $offset + strlen($tokenString); |
|
54
|
7 |
|
$offsetDelta += strlen($tokenValue) - strlen($tokenString); |
|
55
|
7 |
|
$injected[$tokenString] = $token; |
|
56
|
7 |
|
} |
|
57
|
7 |
|
} |
|
58
|
7 |
|
} |
|
59
|
|
|
|
|
60
|
7 |
|
if ($lastOffset > 0) { |
|
61
|
7 |
|
$blocks[] = substr($string, $lastOffset); |
|
62
|
7 |
|
} |
|
63
|
|
|
|
|
64
|
7 |
|
foreach ($injected as $token) { |
|
65
|
7 |
|
$token->setIsInjected(true); |
|
66
|
7 |
|
} |
|
67
|
|
|
|
|
68
|
7 |
|
if (empty($blocks)) { |
|
69
|
2 |
|
$newString = $string; |
|
70
|
2 |
|
} else { |
|
71
|
7 |
|
$newString = implode('', $blocks); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
7 |
|
unset($string); |
|
75
|
7 |
|
unset($blocks); |
|
76
|
|
|
|
|
77
|
7 |
|
if ($checkHash) { |
|
78
|
1 |
|
$tokens->setSourceHash(md5($newString)); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
7 |
|
return $newString; |
|
82
|
|
|
} |
|
83
|
|
|
} |