|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of phpDocumentor. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @link https://phpdoc.org |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpDocumentor\Guides\Cli\DevServer; |
| 15 | + |
| 16 | +use League\Tactician\CommandBus; |
| 17 | +use phpDocumentor\DevServer\Server; |
| 18 | +use phpDocumentor\DevServer\Watcher\FileModifiedEvent; |
| 19 | +use phpDocumentor\FileSystem\FileSystem; |
| 20 | +use phpDocumentor\FileSystem\FlySystemAdapter; |
| 21 | +use phpDocumentor\Guides\Compiler\CompilerContext; |
| 22 | +use phpDocumentor\Guides\Handlers\CompileDocumentsCommand; |
| 23 | +use phpDocumentor\Guides\Handlers\ParseFileCommand; |
| 24 | +use phpDocumentor\Guides\Handlers\RenderDocumentCommand; |
| 25 | +use phpDocumentor\Guides\Nodes\DocumentNode; |
| 26 | +use phpDocumentor\Guides\Nodes\ProjectNode; |
| 27 | +use phpDocumentor\Guides\RenderContext; |
| 28 | +use phpDocumentor\Guides\Renderer\DocumentListIterator; |
| 29 | +use phpDocumentor\Guides\Settings\ProjectSettings; |
| 30 | +use Symfony\Component\Console\Output\OutputInterface; |
| 31 | + |
| 32 | +use function assert; |
| 33 | +use function sprintf; |
| 34 | +use function substr; |
| 35 | + |
| 36 | +final class RerenderListener |
| 37 | +{ |
| 38 | + /** @param array<string, DocumentNode> $documents */ |
| 39 | + public function __construct( |
| 40 | + private readonly OutputInterface $output, |
| 41 | + private readonly CommandBus $commandBus, |
| 42 | + private readonly FileSystem $sourceFileSystem, |
| 43 | + private readonly ProjectSettings $settings, |
| 44 | + private readonly ProjectNode $projectNode, |
| 45 | + private array $documents, |
| 46 | + private readonly Server $server, |
| 47 | + ) { |
| 48 | + } |
| 49 | + |
| 50 | + public function __invoke(FileModifiedEvent $event): void |
| 51 | + { |
| 52 | + $this->output->writeln( |
| 53 | + sprintf( |
| 54 | + 'File modified: %s, rerendering...', |
| 55 | + $event->path, |
| 56 | + ), |
| 57 | + ); |
| 58 | + $file = substr($event->path, 0, -4); |
| 59 | + |
| 60 | + $document = $this->commandBus->handle( |
| 61 | + new ParseFileCommand( |
| 62 | + $this->sourceFileSystem, |
| 63 | + '', |
| 64 | + $file, |
| 65 | + $this->settings->getInputFormat(), |
| 66 | + 1, |
| 67 | + $this->projectNode, |
| 68 | + true, |
| 69 | + ), |
| 70 | + ); |
| 71 | + assert($document instanceof DocumentNode); |
| 72 | + |
| 73 | + $documents = $this->documents; |
| 74 | + $documents[$file] = $document; |
| 75 | + |
| 76 | + /** @var array<string, DocumentNode> $documents */ |
| 77 | + $documents = $this->commandBus->handle(new CompileDocumentsCommand($documents, new CompilerContext($this->projectNode))); |
| 78 | + $this->documents = $documents; |
| 79 | + $destinationFileSystem = FlySystemAdapter::createForPath($this->settings->getOutput()); |
| 80 | + |
| 81 | + $documentIterator = DocumentListIterator::create( |
| 82 | + $this->projectNode->getRootDocumentEntry(), |
| 83 | + $this->documents, |
| 84 | + ); |
| 85 | + |
| 86 | + $renderContext = RenderContext::forProject( |
| 87 | + $this->projectNode, |
| 88 | + $this->documents, |
| 89 | + $this->sourceFileSystem, |
| 90 | + $destinationFileSystem, |
| 91 | + '/', |
| 92 | + 'html', |
| 93 | + )->withIterator($documentIterator); |
| 94 | + |
| 95 | + $this->commandBus->handle( |
| 96 | + new RenderDocumentCommand( |
| 97 | + $this->documents[$file], |
| 98 | + $renderContext->withDocument($this->documents[$file]), |
| 99 | + ), |
| 100 | + ); |
| 101 | + |
| 102 | + $this->output->writeln('Rerendering completed.'); |
| 103 | + $this->server->notifyClients(); |
| 104 | + } |
| 105 | +} |
0 commit comments