spatie /
code-outliner
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Spatie\CodeOutline\Commands; |
||||
| 4 | |||||
| 5 | use Spatie\Browsershot\Browsershot; |
||||
| 6 | use Spatie\CodeOutline\Elements\Page; |
||||
| 7 | use Spatie\CodeOutline\Parser\DirectoryParser; |
||||
| 8 | use Spatie\CodeOutline\Parser\FileParser; |
||||
| 9 | use Spatie\CodeOutline\Parser\Parser; |
||||
| 10 | use Spatie\CodeOutline\Renderer\Renderer; |
||||
| 11 | use Symfony\Component\Console\Command\Command; |
||||
| 12 | use Symfony\Component\Console\Helper\ProgressBar; |
||||
| 13 | use Symfony\Component\Console\Input\InputArgument; |
||||
| 14 | use Symfony\Component\Console\Input\InputInterface; |
||||
| 15 | use Symfony\Component\Console\Input\InputOption; |
||||
| 16 | use Symfony\Component\Console\Output\OutputInterface; |
||||
| 17 | |||||
| 18 | class OutlineFileCommand extends Command |
||||
| 19 | { |
||||
| 20 | public function __construct() |
||||
| 21 | { |
||||
| 22 | parent::__construct('outline'); |
||||
| 23 | |||||
| 24 | $this->addArgument('path', InputArgument::REQUIRED); |
||||
| 25 | |||||
| 26 | $this->addOption('output', null, InputOption::VALUE_OPTIONAL, 'Where to save the output file (it must be a PNG).'); |
||||
| 27 | |||||
| 28 | $this->addOption('extensions', null, InputOption::VALUE_OPTIONAL, 'The extensions of which files to scan for. Eg. `php,html`'); |
||||
| 29 | } |
||||
| 30 | |||||
| 31 | protected function execute(InputInterface $input, OutputInterface $output) |
||||
| 32 | { |
||||
| 33 | $page = $this->parseFiles($input, $output); |
||||
| 34 | |||||
| 35 | $rendered = $this->renderParsed($page, $input, $output); |
||||
| 36 | |||||
| 37 | $outputFilePath = $this->saveImage($rendered, $input, $output); |
||||
| 38 | |||||
| 39 | $output->writeln("Saved to {$outputFilePath}"); |
||||
| 40 | } |
||||
| 41 | |||||
| 42 | protected function parseFiles(InputInterface $input, OutputInterface $output): Page |
||||
| 43 | { |
||||
| 44 | $path = $input->getArgument('path'); |
||||
| 45 | |||||
| 46 | $output->writeln('Parsing files...'); |
||||
| 47 | |||||
| 48 | $progressBar = new ProgressBar($output); |
||||
| 49 | |||||
| 50 | $parser = $this->getParser($path, $input->getOption('extensions'), $progressBar); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 51 | |||||
| 52 | $progressBar->finish(); |
||||
| 53 | |||||
| 54 | return $parser->getParsed(); |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | protected function renderParsed(Page $page, InputInterface $input, OutputInterface $output) |
||||
|
0 ignored issues
–
show
The parameter
$input is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 58 | { |
||||
| 59 | $output->writeln("\nRendering..."); |
||||
| 60 | |||||
| 61 | $renderer = new Renderer($page); |
||||
| 62 | |||||
| 63 | return $renderer->getRendered(); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | protected function saveImage(string $rendered, InputInterface $input, OutputInterface $output): string |
||||
| 67 | { |
||||
| 68 | $output->writeln('Saving as image...'); |
||||
| 69 | |||||
| 70 | $outputFilePath = $this->getOutputFilePath($input->getOption('output')); |
||||
| 71 | |||||
| 72 | Browsershot::html($rendered)->select('body')->save($outputFilePath); |
||||
| 73 | |||||
| 74 | return $outputFilePath; |
||||
| 75 | } |
||||
| 76 | |||||
| 77 | protected function getOutputFilePath(?string $path): string |
||||
| 78 | { |
||||
| 79 | return $path ?? './outline-code.png'; |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | protected function getParser(string $path, ?string $extensions, ProgressBar $progressBar): Parser |
||||
| 83 | { |
||||
| 84 | if (is_dir($path)) { |
||||
| 85 | return (new DirectoryParser($path)) |
||||
| 86 | ->setExtensionsFromString($extensions ?? 'php') |
||||
| 87 | ->onStartParsing(function (int $count) use ($progressBar) { |
||||
| 88 | $progressBar->start($count); |
||||
| 89 | }) |
||||
| 90 | ->onFileParsed(function () use ($progressBar) { |
||||
| 91 | $progressBar->advance(); |
||||
| 92 | }); |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | return new FileParser($path); |
||||
| 96 | } |
||||
| 97 | } |
||||
| 98 |