1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * Highlighter |
||
7 | * |
||
8 | * Copyright (C) 2016, Some right reserved. |
||
9 | * |
||
10 | * @author Kacper "Kadet" Donat <[email protected]> |
||
11 | * |
||
12 | * Contact with author: |
||
13 | * Xmpp: [email protected] |
||
14 | * E-mail: [email protected] |
||
15 | * |
||
16 | * From Kadet with love. |
||
17 | */ |
||
18 | |||
19 | namespace Kadet\Highlighter\bin\Commands; |
||
20 | |||
21 | use Kadet\Highlighter\bin\VerboseOutput; |
||
22 | use Kadet\Highlighter\KeyLighter; |
||
23 | use Kadet\Highlighter\Language\Language; |
||
24 | use Symfony\Component\Console\Command\Command; |
||
25 | use Symfony\Component\Console\Exception\InvalidArgumentException; |
||
26 | use Symfony\Component\Console\Formatter\OutputFormatter; |
||
27 | use Symfony\Component\Console\Input\InputArgument; |
||
28 | use Symfony\Component\Console\Input\InputInterface; |
||
29 | use Symfony\Component\Console\Input\InputOption; |
||
30 | use Symfony\Component\Console\Output\Output; |
||
31 | use Symfony\Component\Console\Output\OutputInterface; |
||
32 | |||
33 | class HighlightCommand extends Command |
||
34 | { |
||
35 | protected $_debug = ['time', 'detailed-time', 'count', 'tree-before', 'tree-after', 'density']; |
||
36 | |||
37 | protected function configure() |
||
38 | { |
||
39 | $this->setName('highlight') |
||
40 | ->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'File(s) to highlight') |
||
41 | ->addOption('normalize', null, InputOption::VALUE_OPTIONAL, 'Normalize input to LF') |
||
42 | ->addOption('language', 'l', InputOption::VALUE_OPTIONAL, 'Source Language to highlight, see <comment>list-languages</comment> command') |
||
43 | ->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format, see <comment>list-languages</comment> command', 'cli') |
||
44 | ->addOption( |
||
45 | 'debug', |
||
46 | 'd', |
||
47 | InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
||
48 | 'Debug features: ' . implode(', ', array_map(function ($f) { |
||
49 | return "<info>{$f}</info>"; |
||
50 | }, $this->_debug)) |
||
51 | ) |
||
52 | ->setDescription('<comment>[DEFAULT]</comment> Highlights given file') |
||
53 | ; |
||
54 | } |
||
55 | |||
56 | protected function execute(InputInterface $input, OutputInterface $output) |
||
57 | { |
||
58 | if (!empty($input->getOption('debug')) && $output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) { |
||
59 | $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
||
60 | } |
||
61 | |||
62 | $output->writeln($this->getApplication()->getLongVersion() . "\n", Output::VERBOSITY_VERBOSE); |
||
63 | $formatter = KeyLighter::get()->getFormatter($input->getOption('format')) ?: KeyLighter::get()->getDefaultFormatter(); |
||
64 | |||
65 | foreach ($input->getArgument('path') as $filename) { |
||
66 | $this->process($input, $output, $filename, $formatter); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | protected function content($path, $normalize = false) |
||
71 | { |
||
72 | if (!($file = @fopen($path, 'r'))) { |
||
73 | return false; |
||
74 | } |
||
75 | |||
76 | $content = ''; |
||
77 | while (!feof($file)) { |
||
78 | $content .= fgets($file); |
||
79 | } |
||
80 | fclose($file); |
||
81 | |||
82 | return $normalize ? str_replace("\r\n", "\n", $content) : $content; // normalize input |
||
83 | } |
||
84 | |||
85 | public function mergeApplicationDefinition($arguments = true) |
||
86 | { |
||
87 | // Some ugly hack, because symfony/console is not very extensible :( |
||
88 | parent::mergeApplicationDefinition($this->getApplication()->explicit); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param InputInterface $input |
||
93 | * @param OutputInterface $output |
||
94 | * @param $filename |
||
95 | * @param $formatter |
||
96 | */ |
||
97 | protected function process(InputInterface $input, OutputInterface $output, $filename, $formatter) |
||
98 | { |
||
99 | $language = $input->getOption('language') |
||
100 | ? KeyLighter::get()->getLanguage($input->getOption('language')) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
101 | : Language::byFilename($filename); |
||
102 | |||
103 | if (!($source = $this->content($filename, $input->getOption('normalize')))) { |
||
104 | throw new InvalidArgumentException(sprintf( |
||
105 | 'Specified file %s doesn\'t exist, check if given path is correct.', |
||
106 | $filename |
||
107 | )); |
||
108 | } |
||
109 | |||
110 | if ($output->isVerbose()) { |
||
111 | $output->writeln(sprintf( |
||
112 | "Used file: <path>%s</path>, Language: <language>%s</language>, Formatter: <formatter>%s</formatter>", |
||
113 | $filename, |
||
114 | $language->getFQN(), |
||
115 | get_class($formatter) |
||
116 | )); |
||
117 | |||
118 | $verbose = new VerboseOutput($output, $input, $language, $formatter, $source); |
||
119 | $formatted = $verbose->process(); |
||
120 | } else { |
||
121 | $formatted = KeyLighter::get()->highlight($source, $language, $formatter); |
||
122 | } |
||
123 | |||
124 | if (!$input->getOption('no-output')) { |
||
125 | $output->writeln(OutputFormatter::escape($formatted)); |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 |