|
| 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') |
| 29 | + ->option('-e --echo', 'Forces echo to STDOUT when --output is passed') |
| 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 | + ])) |
| 34 | + ->option('-F --font', 'Font to use for export to png') |
| 35 | + ->usage( |
| 36 | + '<bold> $0</end> <comment>--file file.php</end> ## print<eol/>' |
| 37 | + . '<bold> cat file.php | $0</end> ## from piped stream<eol/>' |
| 38 | + . '<bold> $0</end> <comment>< file.php ## 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 | + $code = \trim($code); |
| 70 | + |
| 71 | + if ('' === $code) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $this->doExport($code); |
| 76 | + $this->doHighlight($code); |
| 77 | + } |
| 78 | + |
| 79 | + protected function doHighlight(string $code = null) |
| 80 | + { |
| 81 | + if (!$this->output || $this->echo) { |
| 82 | + echo new Highlighter($code); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + protected function doExport(string $code = null) |
| 87 | + { |
| 88 | + if (!$this->output) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (!\is_dir(\dirname($this->output))) { |
| 93 | + \mkdir(\dirname($this->output), 0755, true); |
| 94 | + } |
| 95 | + |
| 96 | + (new Exporter($code))->export($this->output, \array_filter(['font' => $this->font])); |
| 97 | + } |
| 98 | +} |
0 commit comments