Skip to content

Commit

Permalink
This commit will make the table more visual
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayAleshin committed Oct 25, 2023
1 parent f24772d commit eacb90c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 66 deletions.
27 changes: 24 additions & 3 deletions src/Core/Logger/Handler/GenerationErrorsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,44 @@

namespace BumbleDocGen\Core\Logger\Handler;

use BumbleDocGen\Core\Renderer\Context\RendererContext;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;

final class GenerationErrorsHandler extends AbstractProcessingHandler
{
private array $records = [];

public function __construct($level = Logger::WARNING, bool $bubble = true)
{
public function __construct(
private RendererContext $rendererContext,
$level = Logger::WARNING,
bool $bubble = true
) {
parent::__construct($level, $bubble);
}

protected function write(array $record): void
{
$initiator = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[3] ?? [];
$fileName = str_replace([
dirname(__DIR__, 4),
getcwd()
], '', $initiator['file'] ?? '');
$line = $initiator['line'] ?? '';

$initiator = "{$fileName}#{$line}";
$entityDocUrl = $this->rendererContext->getCurrentDocumentedEntityWrapper()?->getDocUrl();
if ($entityDocUrl) {
$initiator .= "\n{$entityDocUrl}";
} elseif ($this->rendererContext->getCurrentTemplateFilePatch()) {
$initiator .= "\n{$this->rendererContext->getCurrentTemplateFilePatch()}";
}

$this->records[] = [
"type" => $record['level_name'],
"msg" => $record['message']
"msg" => $record['message'],
'initiator' => $initiator,
'isRenderingError' => boolval($this->rendererContext->getCurrentTemplateFilePatch())
];
}

Expand Down
88 changes: 29 additions & 59 deletions src/Core/Renderer/RendererIteratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,22 @@ public function getTemplatesWithOutdatedCache(): \Generator
);
$pb->setStepDescription("Processing {$templateFile->getRelativeDocPath()} file");

$oldErrorsCount = count($this->generationErrorsHandler->getRecords());
$errorsBeforeGenerationCount = count($this->generationErrorsHandler->getRecords());

$file = $this->prepareDocFileForRendering($templateFile);
$relativeTemplateName = $templateFile->getRelativeTemplatePath() ?: $templateFile->getRelativeDocPath();

$file = $this->prepareDocFileForRendering($templateFile);
if (!$file) {
$oldTemplateErrors = $this->sharedCompressedDocumentFileCache->get(
$this->getErrorLogCacheKey($relativeTemplateName)
);
$oldTemplateErrors = $oldTemplateErrors ?: [];
$this->generationErrorsHandler->addRecords($oldTemplateErrors);
++$skippedCount;
$this->moveCachedDataToCurrentData($relativeTemplateName);
$this->logger->info("Use cached version `{$templateFile->getRealPath()}`");
continue;
}
yield $file;

$pastErrors = $this->generationErrorsHandler->getRecords();
$newErrors = [];
if ($oldErrorsCount != count($pastErrors)) {
$newErrors = array_slice($pastErrors, count($pastErrors) - $oldErrorsCount);
$errorsCount = count($this->generationErrorsHandler->getRecords());
if ($errorsBeforeGenerationCount === $errorsCount) {
$this->saveContextCacheForSingleDocument($relativeTemplateName);
}
$this->sharedCompressedDocumentFileCache->set(
$this->getErrorLogCacheKey($relativeTemplateName),
$newErrors
);
}

$processed = $finder->count() - $skippedCount;
Expand Down Expand Up @@ -142,19 +133,8 @@ private function prepareDocFileForRendering(TemplateFile $templateFile): ?Templa
filePath: $templateFile->getRealPath()
);
$this->rendererContext->addDependency($fileDependency);
$this->sharedCompressedDocumentFileCache->set(
$this->getOperationsLogCacheKey($relativeTemplateName),
$this->rootEntityCollectionsGroup->getOperationsLogWithoutDuplicates()
);

$this->sharedCompressedDocumentFileCache->set(
$this->getFilesDependenciesCacheKey($relativeTemplateName),
$this->rendererContext->getDependencies()
);
return $templateFile;
}
$this->moveCachedDataToCurrentData($relativeTemplateName);
$this->logger->info("Use cached version `{$templateFile->getRealPath()}`");
return null;
}

Expand All @@ -177,12 +157,10 @@ public function getDocumentedEntityWrappersWithOutdatedCache(): \Generator
$this->rendererContext->clearDependencies();
$this->rootEntityCollectionsGroup->clearOperationsLog();

$this->rendererContext->setCurrentDocumentedEntityWrapper($entityWrapper);
$errorsBeforeGenerationCount = count($this->generationErrorsHandler->getRecords());

$this->rendererContext->setCurrentDocumentedEntityWrapper($entityWrapper);
$this->markFileNameAsRendered($entityWrapper->getDocUrl());

$oldErrorsCount = count($this->generationErrorsHandler->getRecords());

$filesDependenciesKey = "{$entityWrapper->getEntityName()}_{$entityWrapper->getParentDocFilePath()}";
if (
!$this->configuration->useSharedCache() ||
Expand All @@ -201,33 +179,16 @@ public function getDocumentedEntityWrappersWithOutdatedCache(): \Generator
$this->moveCachedDataToCurrentData($entityWrapper->getParentDocFilePath(), $entityWrapper->getEntityName());
$this->logger->info("Use cached version `{$this->configuration->getOutputDir()}{$entityWrapper->getDocUrl()}`");
++$skippedCount;
$oldTemplateErrors = $this->sharedCompressedDocumentFileCache->get(
$this->getErrorLogCacheKey($entityWrapper->getEntityName())
);
$oldTemplateErrors = $oldTemplateErrors ?: [];
$this->generationErrorsHandler->addRecords($oldTemplateErrors);
continue;
}

$this->sharedCompressedDocumentFileCache->set(
$this->getOperationsLogCacheKey($entityWrapper->getEntityName()),
$this->rootEntityCollectionsGroup->getOperationsLogWithoutDuplicates()
);

$pastErrors = $this->generationErrorsHandler->getRecords();
$newErrors = [];
if ($oldErrorsCount != count($pastErrors)) {
$newErrors = array_slice($pastErrors, count($pastErrors) - $oldErrorsCount);
$errorsCount = count($this->generationErrorsHandler->getRecords());
if ($errorsBeforeGenerationCount === $errorsCount) {
$this->saveContextCacheForSingleDocument(
$entityWrapper->getEntityName(),
$entityWrapper->getParentDocFilePath()
);
}
$this->sharedCompressedDocumentFileCache->set(
$this->getErrorLogCacheKey($entityWrapper->getEntityName()),
$newErrors
);

$this->sharedCompressedDocumentFileCache->set(
$this->getFilesDependenciesCacheKey($filesDependenciesKey),
$this->rendererContext->getDependencies()
);
$this->rendererContext->clearDependencies();
$this->rootEntityCollectionsGroup->clearOperationsLog();
}
Expand Down Expand Up @@ -409,6 +370,20 @@ private function moveCachedDataToCurrentData(string $templateFileName, ?string $
}
}

private function saveContextCacheForSingleDocument(string $docKey, ?string $namespace = null): void
{
$this->sharedCompressedDocumentFileCache->set(
$this->getOperationsLogCacheKey($docKey),
$this->rootEntityCollectionsGroup->getOperationsLogWithoutDuplicates()
);

$filesDependenciesKey = $namespace ? "{$docKey}_{$namespace}" : $docKey;
$this->sharedCompressedDocumentFileCache->set(
$this->getFilesDependenciesCacheKey($filesDependenciesKey),
$this->rendererContext->getDependencies()
);
}

private function getOperationsLogCacheKey(string $key): string
{
return "operations_log_{$key}";
Expand All @@ -418,9 +393,4 @@ private function getFilesDependenciesCacheKey(string $key): string
{
return "files_dependencies_{$key}";
}

private function getErrorLogCacheKey(string $key): string
{
return "past_errors_{$key}";
}
}
28 changes: 24 additions & 4 deletions src/DocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
use DI\NotFoundException;
use Monolog\Logger;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Style\OutputStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -332,12 +334,30 @@ public function generate(): void
} else {
$this->io->writeln("<comment>Generation completed with errors</>");

$header = ['<fg=white>Error</>', '<fg=white>Text</>'];
$this->io->getFormatter()->setStyle('warning', new OutputFormatterStyle('yellow'));
$badErrorStyle = new OutputFormatterStyle('red', '#ff0', ['bold']);
$this->io->getFormatter()->setStyle('critical', $badErrorStyle);
$this->io->getFormatter()->setStyle('alert', $badErrorStyle);
$this->io->getFormatter()->setStyle('emergency', $badErrorStyle);
$table = $this->io->createTable();

$rows = [];
foreach ($warningMessages as $warningMessage) {
$rows[] = [$warningMessage['type'], $warningMessage['msg']];
$warningMessagesCount = count($warningMessages);
foreach ($warningMessages as $i => $warningMessage) {
$tag = strtolower($warningMessage['type']);
$rows[] = ["<{$tag}>{$warningMessage['type']}</>", "<{$tag}>{$warningMessage['msg']}</>"];
if ($warningMessage['isRenderingError']) {
$rows[] = ['', '<options=conceal,underscore>This error occurs during the document rendering process</>'];
}
$rows[] = ['', $warningMessage['initiator']];
if ($warningMessagesCount - $i !== 1) {
$rows[] = new TableSeparator();
}
}
$this->io->table($header, $rows);
$table->setStyle('box');
$table->addRows($rows);
$table->render();
$this->io->newLine();
}

$this->io->writeln("<info>Performance</>");
Expand Down

0 comments on commit eacb90c

Please sign in to comment.