1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DumpTokenListCommand.php |
4
|
|
|
* |
5
|
|
|
* MIT LICENSE |
6
|
|
|
* |
7
|
|
|
* LICENSE: This source file is subject to the MIT license. |
8
|
|
|
* A copy of the licenses text was distributed alongside this |
9
|
|
|
* file (usually the repository or package root). The text can also |
10
|
|
|
* be obtained on one of the following sources: |
11
|
|
|
* * http://opensource.org/licenses/MIT |
12
|
|
|
* * https://github.com/suralc/pvra/blob/master/LICENSE |
13
|
|
|
* |
14
|
|
|
* @author suralc <[email protected]> |
15
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Pvra\Console\Commands\Debug; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
use PhpParser\Parser\Tokens; |
22
|
|
|
use Pvra\Lexer\ExtendedEmulativeLexer; |
23
|
|
|
use Symfony\Component\Console\Command\Command; |
24
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
26
|
|
|
use Symfony\Component\Console\Input\InputOption; |
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class DumpTokenListCommand |
31
|
|
|
* |
32
|
|
|
* @package Pvra\Console\Commands\Debug |
33
|
|
|
*/ |
34
|
|
|
class DumpTokenListCommand extends Command |
35
|
|
|
{ |
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this->setName('debug:dumpTokens') |
39
|
|
|
->setDescription('Dump list of tokens in given file'); |
40
|
|
|
|
41
|
|
|
$this->addArgument('target', InputArgument::REQUIRED, 'The target file'); |
42
|
|
|
$this->addOption('tokenizer', 't', InputOption::VALUE_REQUIRED, 'The used tokenizer. Either php or lib', 'php'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $in, OutputInterface $out) |
46
|
|
|
{ |
47
|
|
|
$file = $in->getArgument('target'); |
48
|
|
|
if (!is_file($file) || !is_readable($file)) { |
49
|
|
|
$out->writeln(sprintf('<error>"%s" is not a valid file!</error>', $file)); |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
$tokenizer = $in->getOption('tokenizer'); |
53
|
|
|
if ($tokenizer === 'php') { |
54
|
|
|
$tokens = token_get_all(file_get_contents($file)); |
55
|
|
|
|
56
|
|
|
foreach ($tokens as $token) { |
57
|
|
|
if (is_array($token)) { |
58
|
|
|
$out->writeln(sprintf('%s(%s) "%s"', token_name($token[0]), $token[0], $token[1])); |
59
|
|
|
} else { |
60
|
|
|
$out->writeln('"' . $token . '"'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} elseif ($tokenizer === 'lib') { |
64
|
|
|
$lexer = ExtendedEmulativeLexer::createDefaultInstance(); |
65
|
|
|
$lexer->startLexing(file_get_contents($file)); |
66
|
|
|
$offset = 0; |
67
|
|
|
while ($id = $lexer->getNextToken()) { |
68
|
|
|
if (is_string($name = $this->libTokenToTokenName($id))) { |
69
|
|
|
$out->writeln($name . '(' . $lexer->getTokens()[ $offset ][0] . ') ' . $lexer->getTokens()[ $offset ][1]); |
70
|
|
|
} else { |
71
|
|
|
$out->writeln('Discarded token with id ' . $id); |
72
|
|
|
} |
73
|
|
|
$offset++; |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
$out->writeln(sprintf('<error>"%s" is not a valid value for the tokenizer option</error>', $tokenizer)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $value |
82
|
|
|
* @return string|null |
83
|
|
|
*/ |
84
|
|
|
private function libTokenToTokenName($value) |
85
|
|
|
{ |
86
|
|
|
static $resolved; |
87
|
|
|
if ($resolved === null) { |
88
|
|
|
$refl = new \ReflectionClass(Tokens::class); |
89
|
|
|
$consts = $refl->getConstants(); |
90
|
|
|
$resolved = array_flip($consts); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return isset($resolved[ $value ]) ? $resolved[ $value ] : null; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|