1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Kadet\Highlighter\bin\Commands\Test; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Kadet\Highlighter\KeyLighter; |
8
|
|
|
use Kadet\Highlighter\Language\Language; |
9
|
|
|
use Kadet\Highlighter\Tests\Helpers\TestFormatter; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
class RegenerateCommand extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var KeyLighter |
20
|
|
|
*/ |
21
|
|
|
private $_keylighter; |
22
|
|
|
private $_formatter; |
23
|
|
|
|
24
|
|
|
private $_input; |
25
|
|
|
private $_output; |
26
|
|
|
|
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this->setName('test:regenerate') |
30
|
|
|
->addArgument('files', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'tests to regenerate', ['*']) |
31
|
|
|
->addOption('new', null, InputOption::VALUE_NONE, 'generate only new files') |
32
|
|
|
->setDescription('Regenerates test files') |
33
|
|
|
; |
34
|
|
|
|
35
|
|
|
$this->_keylighter = KeyLighter::get(); |
36
|
|
|
$this->_formatter = new TestFormatter(); |
37
|
|
|
|
38
|
|
|
$this->_input = realpath(__DIR__.'/../../../Tests/Samples'); |
39
|
|
|
$this->_output = realpath(__DIR__.'/../../../Tests/Expected/Test'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
|
|
$iterator = new \RecursiveIteratorIterator( |
45
|
|
|
new \RecursiveDirectoryIterator( |
46
|
|
|
$this->_input, |
47
|
|
|
\RecursiveDirectoryIterator::SKIP_DOTS | \RecursiveDirectoryIterator::UNIX_PATHS |
48
|
|
|
), \RecursiveIteratorIterator::LEAVES_ONLY |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
/** @var \SplFileInfo $file */ |
52
|
|
|
foreach ($iterator as $file) { |
53
|
|
|
$pathname = substr($file->getPathname(), strlen($this->_input) + 1); |
54
|
|
|
if(!$this->regenerate($input, $pathname)) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$output->writeln("Generating $pathname...", OutputInterface::VERBOSITY_QUIET); |
59
|
|
|
|
60
|
|
|
$language = Language::byFilename($pathname); |
61
|
|
|
$result = $this->_keylighter->highlight(file_get_contents($file->getPathname()), $language, $this->_formatter); |
62
|
|
|
|
63
|
|
|
if(!file_exists($this->_output.'/'.dirname($pathname))) { |
64
|
|
|
mkdir($this->_output.'/'.dirname($pathname), true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
file_put_contents("{$this->_output}/$pathname.tkn", $result); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function regenerate(InputInterface $input, $filename) { |
72
|
|
|
$filename = str_replace(DIRECTORY_SEPARATOR, '/', $filename); |
73
|
|
|
$patterns = $input->getArgument('files'); |
74
|
|
|
|
75
|
|
|
foreach($patterns as $pattern) { |
76
|
|
|
if($input->getOption('new') && file_exists("{$this->_output}/$filename.tkn")) { |
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if(fnmatch($pattern, $filename)) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |