Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-adding caching of Metas & fixing how files are scanned and ref dependencies #91

Merged
merged 22 commits into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1d268be
WIP - re-adding caching of Metas
weaverryan Jan 11, 2019
9977ce2
Cleaning up Environment - giving it proper required args
weaverryan Jan 16, 2019
31b577d
Removing unused method
weaverryan Jan 16, 2019
655bd01
Refactoring how the Scanner system works
weaverryan Jan 18, 2019
d482a22
Fixing a bug where :ref: would be added as dependencies
weaverryan Jan 18, 2019
0033d35
These test cases were actually wrong - these are all :ref:
weaverryan Jan 18, 2019
b49928b
Forgotten from earlier - making Parser more explicit
weaverryan Jan 18, 2019
ecc21aa
Simplifying dependencies further - they do not need to be recursive
weaverryan Jan 28, 2019
066445a
Fixing a bug with multiple :ref: not being resolved
weaverryan Jan 28, 2019
9bb7c9b
Fixing bug where :ref: would be made into a URL
weaverryan Jan 28, 2019
e100e39
Finishing test for cached metadata
weaverryan Jan 28, 2019
2c1b625
Making cached metas disable-able
weaverryan Jan 28, 2019
784398e
Making all the Builders in tests not use caching
weaverryan Jan 28, 2019
0f78692
Fixing bug where unresolved name was used to remove dependency
weaverryan Feb 1, 2019
4419383
updating test - all files ARE now parsed, even if orphaned
weaverryan Feb 1, 2019
b374797
reverting cleanups that were BC breaks
weaverryan Feb 1, 2019
28cd560
allowing for the current MetaEntry to not be set
weaverryan Feb 1, 2019
fb5425b
updating test for new constructor arg of ResolvedReference
weaverryan Feb 1, 2019
0093f34
fixing tests
weaverryan Feb 1, 2019
27979a9
fixing cs
weaverryan Feb 1, 2019
2c3d13a
removing array_unique
weaverryan Feb 2, 2019
a128be2
Splitting cached meta saving/loader into separate class
weaverryan Feb 2, 2019
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
72 changes: 48 additions & 24 deletions lib/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
use Doctrine\RST\Event\PreBuildScanEvent;
use Doctrine\RST\Meta\Metas;
use Symfony\Component\Filesystem\Filesystem;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function is_dir;
use function serialize;
use function unserialize;

class Builder
{
Expand All @@ -37,12 +42,6 @@ class Builder
/** @var Documents */
private $documents;

/** @var ParseQueue */
private $parseQueue;

/** @var Scanner */
private $scanner;

/** @var Copier */
private $copier;

Expand All @@ -66,10 +65,6 @@ public function __construct(?Kernel $kernel = null)
$this->metas
);

$this->parseQueue = new Builder\ParseQueue($this->documents);

$this->scanner = new Builder\Scanner($this->parseQueue, $this->metas);

$this->copier = new Builder\Copier($this->filesystem);

$this->kernel->initBuilder($this);
Expand All @@ -95,11 +90,6 @@ public function getDocuments() : Documents
return $this->documents;
}

public function getParseQueue() : Builder\ParseQueue
{
return $this->parseQueue;
}

public function getErrorManager() : ErrorManager
{
return $this->errorManager;
Expand All @@ -126,11 +116,17 @@ public function build(
$this->filesystem->mkdir($targetDirectory, 0755);
}

$this->scan($directory, $targetDirectory);
if ($this->configuration->getUseCachedMetas()) {
$this->loadCachedMetas($targetDirectory);
}

$parseQueue = $this->scan($directory, $targetDirectory);

$this->parse($directory, $targetDirectory);
$this->parse($directory, $targetDirectory, $parseQueue);

$this->render($directory, $targetDirectory);

$this->saveMetas($targetDirectory);
}

public function copy(string $source, ?string $destination = null) : self
Expand All @@ -147,19 +143,23 @@ public function mkdir(string $directory) : self
return $this;
}

private function scan(string $directory, string $targetDirectory) : void
private function scan(string $directory, string $targetDirectory) : ParseQueue
{
$this->configuration->dispatchEvent(
PreBuildScanEvent::PRE_BUILD_SCAN,
new PreBuildScanEvent($this, $directory, $targetDirectory)
);

$this->scanner->scan($directory, $this->getIndexName());
$scanner = new Scanner(
$this->configuration->getSourceFileExtension(),
$directory,
$this->metas
);

$this->scanner->scanMetas($directory);
return $scanner->scan();
}

private function parse(string $directory, string $targetDirectory) : void
private function parse(string $directory, string $targetDirectory, ParseQueue $parseQueue) : void
{
$this->configuration->dispatchEvent(
PreBuildParseEvent::PRE_BUILD_PARSE,
Expand All @@ -169,16 +169,14 @@ private function parse(string $directory, string $targetDirectory) : void
$parseQueueProcessor = new ParseQueueProcessor(
$this->kernel,
$this->errorManager,
$this->parseQueue,
$this->metas,
$this->documents,
$this->scanner,
$directory,
$targetDirectory,
$this->configuration->getFileExtension()
);

$parseQueueProcessor->process();
$parseQueueProcessor->process($parseQueue);
}

private function render(string $directory, string $targetDirectory) : void
Expand All @@ -198,4 +196,30 @@ private function render(string $directory, string $targetDirectory) : void
new PostBuildRenderEvent($this, $directory, $targetDirectory)
);
}

private function loadCachedMetas(string $targetDirectory) : void
weaverryan marked this conversation as resolved.
Show resolved Hide resolved
{
$metaCachePath = $this->getMetaCachePath($targetDirectory);
if (! file_exists($metaCachePath)) {
return;
}

$contents = file_get_contents($metaCachePath);

if ($contents === false) {
return;
weaverryan marked this conversation as resolved.
Show resolved Hide resolved
}

$this->metas->setMetaEntries(unserialize($contents));
}

private function saveMetas(string $targetDirectory) : void
{
file_put_contents($this->getMetaCachePath($targetDirectory), serialize($this->metas->getAll()));
}

private function getMetaCachePath(string $targetDirectory) : string
{
return $targetDirectory . '/metas.php';
}
}
63 changes: 32 additions & 31 deletions lib/Builder/ParseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,52 @@

namespace Doctrine\RST\Builder;

use function array_shift;
use InvalidArgumentException;
use function array_filter;
use function array_key_exists;
use function array_keys;
use function sprintf;

class ParseQueue
final class ParseQueue
{
/** @var Documents */
private $documents;

/** @var string[] */
private $parseQueue = [];

/** @var int[] */
private $states = [];

public function __construct(Documents $documents)
/**
* An array where each key is the filename and the value is a
* boolean indicating if the file needs to be parsed or not.
*
* @var bool[]
*/
private $fileStatuses = [];

public function addFile(string $filename, bool $parseNeeded) : void
{
$this->documents = $documents;
}
if (isset($this->fileStatuses[$filename])) {
throw new InvalidArgumentException(sprintf('File "%s" is already in the parse queue', $filename));
}

public function getState(string $file) : ?int
{
return $this->states[$file] ?? null;
$this->fileStatuses[$filename] = $parseNeeded;
}

public function setState(string $file, int $state) : void
public function isFileKnownToParseQueue(string $filename) : bool
{
$this->states[$file] = $state;
return array_key_exists($filename, $this->fileStatuses);
}

public function getFileToParse() : ?string
public function doesFileRequireParsing(string $filename) : bool
{
if ($this->parseQueue !== []) {
return array_shift($this->parseQueue);
if (! $this->isFileKnownToParseQueue($filename)) {
throw new InvalidArgumentException(sprintf('File "%s" is not known to the parse queue', $filename));
}

return null;
return $this->fileStatuses[$filename];
}

public function addToParseQueue(string $file) : void
/**
* @return string[]
*/
public function getAllFilesThatRequireParsing() : array
{
$this->states[$file] = State::PARSE;

if ($this->documents->hasDocument($file)) {
return;
}

$this->parseQueue[$file] = $file;
return array_keys(array_filter($this->fileStatuses, static function (bool $parseNeeded) {
return $parseNeeded;
}));
}
}
36 changes: 3 additions & 33 deletions lib/Builder/ParseQueueProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Doctrine\RST\Meta\Metas;
use Doctrine\RST\Nodes\DocumentNode;
use Doctrine\RST\Parser;
use function array_filter;
use function file_exists;
use function filectime;

class ParseQueueProcessor
Expand All @@ -21,18 +19,12 @@ class ParseQueueProcessor
/** @var ErrorManager */
private $errorManager;

/** @var ParseQueue */
private $parseQueue;

/** @var Metas */
private $metas;

/** @var Documents */
private $documents;

/** @var Scanner */
private $scanner;

/** @var string */
private $directory;

Expand All @@ -45,28 +37,24 @@ class ParseQueueProcessor
public function __construct(
Kernel $kernel,
ErrorManager $errorManager,
ParseQueue $parseQueue,
Metas $metas,
Documents $documents,
Scanner $scanner,
string $directory,
string $targetDirectory,
string $fileExtension
) {
$this->kernel = $kernel;
$this->errorManager = $errorManager;
$this->parseQueue = $parseQueue;
$this->metas = $metas;
$this->documents = $documents;
$this->scanner = $scanner;
$this->directory = $directory;
$this->targetDirectory = $targetDirectory;
$this->fileExtension = $fileExtension;
}

public function process() : void
public function process(ParseQueue $parseQueue) : void
{
while ($file = $this->parseQueue->getFileToParse()) {
foreach ($parseQueue->getAllFilesThatRequireParsing() as $file) {
$this->processFile($file);
}
}
Expand All @@ -85,20 +73,14 @@ private function processFile(string $file) : void

$this->kernel->postParse($document);

$dependencies = $environment->getDependencies();

foreach ($this->buildDependenciesToScan($dependencies) as $dependency) {
$this->scanner->scan($this->directory, $dependency);
}

$this->metas->set(
$file,
$this->buildDocumentUrl($document),
(string) $document->getTitle(),
$document->getTitles(),
$document->getTocs(),
(int) filectime($fileAbsolutePath),
$dependencies,
$environment->getDependencies(),
$environment->getLinks()
);
}
Expand All @@ -117,18 +99,6 @@ private function createFileParser(string $file) : Parser
return $parser;
}

/**
* @param string[] $dependencies
*
* @return string[]
*/
private function buildDependenciesToScan(array $dependencies) : array
{
return array_filter($dependencies, function (string $dependency) : bool {
return file_exists($this->buildFileAbsolutePath($dependency));
});
}

private function buildFileAbsolutePath(string $file) : string
{
return $this->directory . '/' . $file . '.rst';
Expand Down
Loading