diff --git a/lib/Builder.php b/lib/Builder.php index 1be06378..a88c4d2b 100644 --- a/lib/Builder.php +++ b/lib/Builder.php @@ -138,27 +138,8 @@ public function build( string $directory, string $targetDirectory = 'output' ): void { - // Creating output directory if doesn't exists - if (! is_dir($targetDirectory)) { - $this->filesystem->mkdir($targetDirectory, 0755); - } - - $indexFilename = sprintf('%s.%s', $this->indexName, $this->configuration->getSourceFileExtension()); - if (! file_exists($directory . '/' . $indexFilename)) { - throw new InvalidArgumentException(sprintf('Could not find index file "%s" in "%s"', $indexFilename, $directory)); - } - - if ($this->configuration->getUseCachedMetas()) { - $this->cachedMetasLoader->loadCachedMetaEntries($targetDirectory, $this->metas); - } - - $parseQueue = $this->scan($directory, $targetDirectory); - - $this->parse($directory, $targetDirectory, $parseQueue); - + $this->parse($directory, $targetDirectory); $this->render($directory, $targetDirectory); - - $this->cachedMetasLoader->cacheMetaEntries($targetDirectory, $this->metas); } public function copy(string $source, ?string $destination = null): self @@ -200,7 +181,25 @@ private function scan(string $directory, string $targetDirectory): ParseQueue return $scanner->scan(); } - private function parse(string $directory, string $targetDirectory, ParseQueue $parseQueue): void + public function parse(string $directory, string $targetDirectory): void + { + $this->ensureDirectoryExists($targetDirectory); + + $indexFilename = sprintf('%s.%s', $this->indexName, $this->configuration->getSourceFileExtension()); + if (! file_exists($directory . '/' . $indexFilename)) { + throw new InvalidArgumentException(sprintf('Could not find index file "%s" in "%s"', $indexFilename, $directory)); + } + + if ($this->configuration->getUseCachedMetas()) { + $this->cachedMetasLoader->loadCachedMetaEntries($targetDirectory, $this->metas); + } + + $parseQueue = $this->scan($directory, $targetDirectory); + + $this->doParse($directory, $targetDirectory, $parseQueue); + } + + private function doParse(string $directory, string $targetDirectory, ParseQueue $parseQueue): void { $this->configuration->dispatchEvent( PreBuildParseEvent::PRE_BUILD_PARSE, @@ -220,8 +219,10 @@ private function parse(string $directory, string $targetDirectory, ParseQueue $p $parseQueueProcessor->process($parseQueue); } - private function render(string $directory, string $targetDirectory): void + public function render(string $directory, string $targetDirectory): void { + $this->ensureDirectoryExists($targetDirectory); + $this->configuration->dispatchEvent( PreBuildRenderEvent::PRE_BUILD_RENDER, new PreBuildRenderEvent($this, $directory, $targetDirectory) @@ -236,6 +237,28 @@ private function render(string $directory, string $targetDirectory): void PostBuildRenderEvent::POST_BUILD_RENDER, new PostBuildRenderEvent($this, $directory, $targetDirectory) ); + + $this->storeCache($targetDirectory); + } + + private function ensureDirectoryExists(string $targetDirectory): void + { + if (is_dir($targetDirectory)) { + return; + } + + $this->filesystem->mkdir($targetDirectory, 0755); + } + + /** + * Persist the cache to disk. + * + * The meta's are cached after we finished rendering as the renderer resolves the filenames of the dependencies in + * these meta's. + */ + private function storeCache(string $targetDirectory): void + { + $this->cachedMetasLoader->cacheMetaEntries($targetDirectory, $this->metas); } private function getScannerFinder(): Finder diff --git a/tests/Builder/BuilderTest.php b/tests/Builder/BuilderTest.php index abcfb7f8..1938ae4f 100644 --- a/tests/Builder/BuilderTest.php +++ b/tests/Builder/BuilderTest.php @@ -7,6 +7,7 @@ use Doctrine\RST\Builder; use Doctrine\RST\Meta\MetaEntry; use Doctrine\Tests\RST\BaseBuilderTest; +use InvalidArgumentException; use function array_unique; use function array_values; @@ -339,6 +340,14 @@ public function testReferenceToTitleWith2CharactersLong(): void ); } + public function testItThrowsWhenIndexFileCannotBeFound(): void + { + $builder = new Builder(); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Could not find index file "index.rst" in "/tmp"'); + $builder->parse('/tmp', '/tmp/test'); + } + protected function getFixturesDirectory(): string { return 'Builder';