| @@ 20-100 (lines=81) @@ | ||
| 17 | ||
| 18 | use Kenjis\MonkeyPatch\Patcher\ConstantPatcher\NodeVisitor; |
|
| 19 | ||
| 20 | class ConstantPatcher extends AbstractPatcher |
|
| 21 | { |
|
| 22 | /** |
|
| 23 | * @var special constant names which we don't patch |
|
| 24 | */ |
|
| 25 | private static $blacklist = [ |
|
| 26 | 'true', |
|
| 27 | 'false', |
|
| 28 | 'null', |
|
| 29 | ]; |
|
| 30 | ||
| 31 | public static $replacement; |
|
| 32 | ||
| 33 | public function __construct() |
|
| 34 | { |
|
| 35 | $this->node_visitor = new NodeVisitor(); |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @param string $name constant name |
|
| 40 | * @return boolean |
|
| 41 | */ |
|
| 42 | public static function isBlacklisted($name) |
|
| 43 | { |
|
| 44 | if (in_array(strtolower($name), self::$blacklist)) |
|
| 45 | { |
|
| 46 | return true; |
|
| 47 | } |
|
| 48 | ||
| 49 | return false; |
|
| 50 | } |
|
| 51 | ||
| 52 | protected static function generateNewSource($source) |
|
| 53 | { |
|
| 54 | $tokens = token_get_all($source); |
|
| 55 | $new_source = ''; |
|
| 56 | $i = -1; |
|
| 57 | ||
| 58 | ksort(self::$replacement); |
|
| 59 | reset(self::$replacement); |
|
| 60 | $replacement['key'] = key(self::$replacement); |
|
| 61 | $replacement['value'] = current(self::$replacement); |
|
| 62 | next(self::$replacement); |
|
| 63 | if ($replacement['key'] === null) |
|
| 64 | { |
|
| 65 | $replacement = false; |
|
| 66 | } |
|
| 67 | ||
| 68 | foreach ($tokens as $token) |
|
| 69 | { |
|
| 70 | $i++; |
|
| 71 | ||
| 72 | if (is_string($token)) |
|
| 73 | { |
|
| 74 | $new_source .= $token; |
|
| 75 | } |
|
| 76 | elseif ($i == $replacement['key']) |
|
| 77 | { |
|
| 78 | $new_source .= $replacement['value']; |
|
| 79 | $replacement['key'] = key(self::$replacement); |
|
| 80 | $replacement['value'] = current(self::$replacement); |
|
| 81 | next(self::$replacement); |
|
| 82 | if ($replacement['key'] === null) |
|
| 83 | { |
|
| 84 | $replacement = false; |
|
| 85 | } |
|
| 86 | } |
|
| 87 | else |
|
| 88 | { |
|
| 89 | $new_source .= $token[1]; |
|
| 90 | } |
|
| 91 | } |
|
| 92 | ||
| 93 | if ($replacement !== false) |
|
| 94 | { |
|
| 95 | throw new LogicException('Replacement data still remain'); |
|
| 96 | } |
|
| 97 | ||
| 98 | return $new_source; |
|
| 99 | } |
|
| 100 | } |
|
| 101 | ||
| @@ 20-100 (lines=81) @@ | ||
| 17 | ||
| 18 | use Kenjis\MonkeyPatch\Patcher\ConstantPatcher\NodeVisitor; |
|
| 19 | ||
| 20 | class ConstantPatcher extends AbstractPatcher |
|
| 21 | { |
|
| 22 | /** |
|
| 23 | * @var special constant names which we don't patch |
|
| 24 | */ |
|
| 25 | private static $blacklist = [ |
|
| 26 | 'true', |
|
| 27 | 'false', |
|
| 28 | 'null', |
|
| 29 | ]; |
|
| 30 | ||
| 31 | public static $replacement; |
|
| 32 | ||
| 33 | public function __construct() |
|
| 34 | { |
|
| 35 | $this->node_visitor = new NodeVisitor(); |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @param string $name constant name |
|
| 40 | * @return boolean |
|
| 41 | */ |
|
| 42 | public static function isBlacklisted($name) |
|
| 43 | { |
|
| 44 | if (in_array(strtolower($name), self::$blacklist)) |
|
| 45 | { |
|
| 46 | return true; |
|
| 47 | } |
|
| 48 | ||
| 49 | return false; |
|
| 50 | } |
|
| 51 | ||
| 52 | protected static function generateNewSource($source) |
|
| 53 | { |
|
| 54 | $tokens = token_get_all($source); |
|
| 55 | $new_source = ''; |
|
| 56 | $i = -1; |
|
| 57 | ||
| 58 | ksort(self::$replacement); |
|
| 59 | reset(self::$replacement); |
|
| 60 | $replacement['key'] = key(self::$replacement); |
|
| 61 | $replacement['value'] = current(self::$replacement); |
|
| 62 | next(self::$replacement); |
|
| 63 | if ($replacement['key'] === null) |
|
| 64 | { |
|
| 65 | $replacement = false; |
|
| 66 | } |
|
| 67 | ||
| 68 | foreach ($tokens as $token) |
|
| 69 | { |
|
| 70 | $i++; |
|
| 71 | ||
| 72 | if (is_string($token)) |
|
| 73 | { |
|
| 74 | $new_source .= $token; |
|
| 75 | } |
|
| 76 | elseif ($i == $replacement['key']) |
|
| 77 | { |
|
| 78 | $new_source .= $replacement['value']; |
|
| 79 | $replacement['key'] = key(self::$replacement); |
|
| 80 | $replacement['value'] = current(self::$replacement); |
|
| 81 | next(self::$replacement); |
|
| 82 | if ($replacement['key'] === null) |
|
| 83 | { |
|
| 84 | $replacement = false; |
|
| 85 | } |
|
| 86 | } |
|
| 87 | else |
|
| 88 | { |
|
| 89 | $new_source .= $token[1]; |
|
| 90 | } |
|
| 91 | } |
|
| 92 | ||
| 93 | if ($replacement !== false) |
|
| 94 | { |
|
| 95 | throw new LogicException('Replacement data still remain'); |
|
| 96 | } |
|
| 97 | ||
| 98 | return $new_source; |
|
| 99 | } |
|
| 100 | } |
|
| 101 | ||