Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 45 additions & 22 deletions lib/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Render became public and should get its own tests.

{
$this->ensureDirectoryExists($targetDirectory);

$this->configuration->dispatchEvent(
PreBuildRenderEvent::PRE_BUILD_RENDER,
new PreBuildRenderEvent($this, $directory, $targetDirectory)
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/Builder/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down