|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the CLI-SYNTAX package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
|
9
|
|
|
* <https://github.com/adhocore> |
|
10
|
|
|
* |
|
11
|
|
|
* Licensed under MIT license. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Ahc\CliSyntax\Console; |
|
15
|
|
|
|
|
16
|
|
|
use Ahc\Cli\Input\Command; |
|
17
|
|
|
use Ahc\Cli\IO\Interactor; |
|
18
|
|
|
use Ahc\CliSyntax\Exporter; |
|
19
|
|
|
use Ahc\CliSyntax\Highlighter; |
|
20
|
|
|
|
|
21
|
|
|
class ClishCommand extends Command |
|
22
|
|
|
{ |
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
parent::__construct('clish', 'PHP CLI syntax highlight and/or export.'); |
|
26
|
|
|
|
|
27
|
|
|
$this |
|
28
|
|
|
->option('-o --output', 'Output filepath where PNG image is exported', null, '') |
|
29
|
|
|
->option('-e --echo', 'Forces echo to STDOUT when --output is passed', null, '') |
|
30
|
|
|
->option('-l --with-line-no', 'Highlight with line number') |
|
31
|
|
|
->option('-f --file', \implode("\n", [ |
|
32
|
|
|
'Input PHP file to highlight and/or export', |
|
33
|
|
|
'(will read from piped input if file not given)', |
|
34
|
|
|
]), null, '') |
|
35
|
|
|
->option('-F --font', 'Font to use for export to png', null, '') |
|
36
|
|
|
->usage( |
|
37
|
|
|
'<bold> $0</end> <comment>--file file.php</end> ## print<eol/>' |
|
38
|
|
|
. '<comment> cat file.php |</end> <bold>$0</end> ## from piped stream<eol/>' |
|
39
|
|
|
. '<bold> $0</end> <comment>< file.php</end> ## from redirected stdin<eol/>' |
|
40
|
|
|
. '<bold> $0</end> <comment>--file file.php --output file.png</end> ## export<eol/>' |
|
41
|
|
|
. '<bold> $0</end> <comment>--file file.php --output file.png --echo</end> ## print + export<eol/>' |
|
42
|
|
|
. '<bold> $0</end> <comment>--file file.php --with-line-no</end> ## print with lineno<eol/>' |
|
43
|
|
|
. '<bold> $0</end> <comment>-f file.php -o file.png -F dejavu</end> ## export in dejavu font<eol/>' |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function interact(Interactor $io) |
|
48
|
|
|
{ |
|
49
|
|
|
if ($this->file) { |
|
|
|
|
|
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$code = $io->readPiped(function ($reader) use ($io) { |
|
54
|
|
|
$io->warn('Type in or paste PHP Code below, Press Ctrl+D when done.', true); |
|
55
|
|
|
$io->warn('(Opening tag `<?php` will be prepended as required):', true); |
|
56
|
|
|
|
|
57
|
|
|
return $reader->readAll(); |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
if ('' !== $code && \substr($code, 0, 5) != '<?php') { |
|
|
|
|
|
|
61
|
|
|
$eol = \substr_count(\rtrim($code), "\n") ? "\n\n" : ''; |
|
|
|
|
|
|
62
|
|
|
$code = '<?php ' . $eol . $code; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->set('code', $code); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function execute() |
|
69
|
|
|
{ |
|
70
|
|
|
$code = $this->file ? \file_get_contents($this->file) : $this->code; |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if ('' === \trim($code)) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->doExport($code); |
|
77
|
|
|
$this->doHighlight($code); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function doHighlight(string $code = null) |
|
81
|
|
|
{ |
|
82
|
|
|
if (!$this->output || $this->echo) { |
|
|
|
|
|
|
83
|
|
|
$this->app()->io()->raw((new Highlighter)->highlight($code, ['lineNo' => $this->lineNo])); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function doExport(string $code = null) |
|
88
|
|
|
{ |
|
89
|
|
|
if (!$this->output) { |
|
|
|
|
|
|
90
|
|
|
return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (!\is_dir(\dirname($this->output))) { |
|
94
|
|
|
\mkdir(\dirname($this->output), 0755, true); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$options = ['font' => $this->fixFont($this->font ?: 'ubuntu'), 'lineNo' => $this->lineNo]; |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
(new Exporter($code))->export($this->output, $options); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** @codeCoverageIgnore */ |
|
103
|
|
|
protected function fixFont(string $font): string |
|
104
|
|
|
{ |
|
105
|
|
|
if (\Phar::running()) { |
|
106
|
|
|
$basename = \basename($font, '.ttf'); |
|
107
|
|
|
$pharfont = __DIR__ . "/../../font/$basename.ttf"; |
|
108
|
|
|
|
|
109
|
|
|
if (!\is_file($font) && \is_file($pharfont)) { |
|
110
|
|
|
\copy($pharfont, $font = sys_get_temp_dir() . '/clish.ttf'); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $font; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|