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('-f --file', \implode("\n", [ |
31
|
|
|
'Input PHP file to highlight and/or export', |
32
|
|
|
'(will read from piped input if file not given)', |
33
|
|
|
]), null, '') |
34
|
|
|
->option('-F --font', 'Font to use for export to png', null, '') |
35
|
|
|
->usage( |
36
|
|
|
'<bold> $0</end> <comment>--file file.php</end> ## print<eol/>' |
37
|
|
|
. '<comment> cat file.php |</end> <bold>$0</end> ## from piped stream<eol/>' |
38
|
|
|
. '<bold> $0</end> <comment>< file.php</end> ## from redirected stdin<eol/>' |
39
|
|
|
. '<bold> $0</end> <comment>--file file.php --output file.png</end> ## export<eol/>' |
40
|
|
|
. '<bold> $0</end> <comment>--file file.php --output file.png --echo</end> ## print + export<eol/>' |
41
|
|
|
. '<bold> $0</end> <comment>-f file.php -o file.png -F dejavu</end> ## export in dejavu font<eol/>' |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function interact(Interactor $io) |
46
|
|
|
{ |
47
|
|
|
if ($this->file) { |
|
|
|
|
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$code = $io->readPiped(function ($reader) use ($io) { |
52
|
|
|
$io->warn('Type in or paste PHP Code below, Press Ctrl+D when done.', true); |
53
|
|
|
$io->warn('(Opening tag `<?php` will be prepended as required):', true); |
54
|
|
|
|
55
|
|
|
return $reader->readAll(); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
if ('' !== $code && \substr($code, 0, 5) != '<?php') { |
|
|
|
|
59
|
|
|
$eol = \substr_count(\rtrim($code), "\n") ? "\n\n" : ''; |
|
|
|
|
60
|
|
|
$code = '<?php ' . $eol . $code; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->set('code', $code); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function execute() |
67
|
|
|
{ |
68
|
|
|
$code = $this->file ? \file_get_contents($this->file) : $this->code; |
|
|
|
|
69
|
|
|
|
70
|
|
|
if ('' === \trim($code)) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->doExport($code); |
75
|
|
|
$this->doHighlight($code); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function doHighlight(string $code = null) |
79
|
|
|
{ |
80
|
|
|
if (!$this->output || $this->echo) { |
|
|
|
|
81
|
|
|
$this->app()->io()->raw((string) new Highlighter($code)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function doExport(string $code = null) |
86
|
|
|
{ |
87
|
|
|
if (!$this->output) { |
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!\is_dir(\dirname($this->output))) { |
92
|
|
|
\mkdir(\dirname($this->output), 0755, true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$options = ['font' => $this->fixFont($this->font ?: 'ubuntu')]; |
|
|
|
|
96
|
|
|
|
97
|
|
|
(new Exporter($code))->export($this->output, $options); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @codeCoverageIgnore */ |
101
|
|
|
protected function fixFont(string $font): string |
102
|
|
|
{ |
103
|
|
|
if (\Phar::running()) { |
104
|
|
|
$basename = \basename($font, '.ttf'); |
105
|
|
|
$pharfont = __DIR__ . "/../../font/$basename.ttf"; |
106
|
|
|
|
107
|
|
|
if (!\is_file($font) && \is_file($pharfont)) { |
108
|
|
|
\copy($pharfont, $font = sys_get_temp_dir() . '/clish.ttf'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $font; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|