Skip to content

Commit

Permalink
Setting cache file in config
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkusebauch committed Mar 16, 2024
1 parent 104f17a commit 5754847
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 39 deletions.
2 changes: 1 addition & 1 deletion config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

$services
->set(AstFileReferenceFileCache::class)
->args(['%deptrac.cache_file%', Application::VERSION]);
->args(['%cache_file%', Application::VERSION]);

$services->alias(AstFileReferenceDeferredCacheInterface::class, AstFileReferenceFileCache::class);
$services->alias(AstFileReferenceCacheInterface::class, AstFileReferenceDeferredCacheInterface::class);
Expand Down
1 change: 1 addition & 0 deletions deptrac.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

$config
->paths('src')
->cacheFile('.cache/deptrac.cache')
->analyser(
AnalyserConfig::create()
->internalTag( '@internal' )
Expand Down
1 change: 1 addition & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
deptrac:
paths:
- ./src
cache_file: .cache/deptrac.cache

analyser:
internal_tag: "@internal"
Expand Down
4 changes: 2 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ Deptrac provides parameters that can be user in your configuration.

* `%currentWorkingDirectory%` The path Deptrac runs in
* `%projectDirectory%` The path where the configuration is stored.
* `%deptrac.cache_file%` contains the filename and path for the cache file.
Note: This parameter is set by `--cache-file=` and will be overwritten.
* `%cache_file%` contains the filename and path for the cache file.
Note: This parameter is overwritten by `--cache-file=` if it is set.

You can specify your own parameters and reuse them in your configuration:

Expand Down
10 changes: 10 additions & 0 deletions src/Contract/Config/DeptracConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class DeptracConfig implements ConfigBuilderInterface
/** @var array<string> */
private array $excludeFiles = [];

private ?string $cacheFile = null;

/**
* @deprecated use analyser(AnalyserConfig::create()) instead
*/
Expand Down Expand Up @@ -105,6 +107,13 @@ public function rulesets(Ruleset ...$rulesetConfigs): self
return $this;
}

public function cacheFile(string $path): self
{
$this->cacheFile = $path;

return $this;
}

/** @return array<mixed> */
public function toArray(): array
{
Expand Down Expand Up @@ -139,6 +148,7 @@ public function toArray(): array
}

$config['ignore_uncovered_internal_classes'] = $this->ignoreUncoveredInternalClasses;
$config['cache_file'] = $this->cacheFile;

return $config;

Check warning on line 153 in src/Contract/Config/DeptracConfig.php

View workflow job for this annotation

GitHub Actions / infection (ubuntu-22.04, 8.1)

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } $config['ignore_uncovered_internal_classes'] = $this->ignoreUncoveredInternalClasses; $config['cache_file'] = $this->cacheFile; - return $config; + return count($config) > 1 ? array_slice($config, 0, 1, true) : $config; } public function getExtensionAlias() : string {

Check warning on line 153 in src/Contract/Config/DeptracConfig.php

View workflow job for this annotation

GitHub Actions / infection (ubuntu-22.04, 8.3)

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } $config['ignore_uncovered_internal_classes'] = $this->ignoreUncoveredInternalClasses; $config['cache_file'] = $this->cacheFile; - return $config; + return count($config) > 1 ? array_slice($config, 0, 1, true) : $config; } public function getExtensionAlias() : string {

Check warning on line 153 in src/Contract/Config/DeptracConfig.php

View workflow job for this annotation

GitHub Actions / infection (ubuntu-22.04, 8.2)

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ } $config['ignore_uncovered_internal_classes'] = $this->ignoreUncoveredInternalClasses; $config['cache_file'] = $this->cacheFile; - return $config; + return count($config) > 1 ? array_slice($config, 0, 1, true) : $config; } public function getExtensionAlias() : string {
}
Expand Down
19 changes: 6 additions & 13 deletions src/Supportive/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function getDefaultInputDefinition(): InputDefinition
null,
InputOption::VALUE_REQUIRED,
'Location where cache file will be stored',
getcwd().DIRECTORY_SEPARATOR.'.deptrac.cache'
null
),
new InputOption(
'--config-file',
Expand Down Expand Up @@ -101,25 +101,18 @@ public function doRun(InputInterface $input, OutputInterface $output): int
? (string) $configFile
: $currentWorkingDirectory.DIRECTORY_SEPARATOR.'deptrac.yaml';

/** @var string|numeric|null $cacheFile */
$cacheFile = $input->getParameterOption('--cache-file', $currentWorkingDirectory.DIRECTORY_SEPARATOR.'.deptrac.cache');
$cache = $input->hasParameterOption('--cache-file')
? (string) $cacheFile
: $currentWorkingDirectory.DIRECTORY_SEPARATOR.'.deptrac.cache';
/** @var ?string $cache */
$cache = $input->getParameterOption('--cache-file', null);

$factory = new ServiceContainerBuilder($currentWorkingDirectory);
if ($input->hasParameterOption('--clear-cache', true)) {
$factory = $factory->clearCache($cache);
}
if (!in_array($input->getArgument('command'), ['init', 'list', 'help', 'completion'], true)) {
$factory = $factory->withConfig($config);
}
if (false === $input->hasParameterOption('--no-cache', true)) {
$factory = $factory->withCache($cache);
}

$noCache = $input->hasParameterOption('--no-cache', true);

try {
$container = $factory->build();
$container = $factory->build($noCache ? false : $cache, $input->hasParameterOption('--clear-cache', true));
$commandLoader = $container->get('console.command_loader');
if (!$commandLoader instanceof CommandLoaderInterface) {
throw new RuntimeException('CommandLoader not initialized. Commands can not be registered.');
Expand Down
11 changes: 11 additions & 0 deletions src/Supportive/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$this->appendFormatters($rootNode);
$this->appendEmitterTypes($rootNode);
$this->appendIgnoreUncoveredInternalClasses($rootNode);
$this->appendCacheFile($rootNode);

return $builder;
}
Expand Down Expand Up @@ -236,4 +237,14 @@ private function appendIgnoreUncoveredInternalClasses(ArrayNodeDefinition $node)
->end()
->end();
}

private function appendCacheFile(ArrayNodeDefinition $node): void
{
$node
->children()
->scalarNode('cache_file')
->defaultValue('.deptrac.cache')
->end()
->end();
}
}
4 changes: 4 additions & 0 deletions src/Supportive/DependencyInjection/DeptracExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('formatters', $configs['formatters'] ?? []);
$container->setParameter('analyser', $configs['analyser']);
$container->setParameter('ignore_uncovered_internal_classes', $configs['ignore_uncovered_internal_classes']);
$container->setParameter('cache_file', $configs['cache_file']);
}

public function prepend(ContainerBuilder $container): void
Expand Down Expand Up @@ -61,5 +62,8 @@ public function prepend(ContainerBuilder $container): void
if (!$container->hasParameter('ignore_uncovered_internal_classes')) {
$container->setParameter('ignore_uncovered_internal_classes', true);
}
if (!$container->hasParameter('cache_file')) {
$container->setParameter('cache_file', '.deptrac.cache');
}
}
}
40 changes: 18 additions & 22 deletions src/Supportive/DependencyInjection/ServiceContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,54 +44,37 @@ public function withConfig(?string $configFile): self
return $builder;
}

public function withCache(?string $cacheFile): self
private function withCache(string $cacheFile): void
{
if (null === $cacheFile) {
return $this;
}

$builder = clone $this;

if (Path::isRelative($cacheFile)) {
/** @throws void */
$cacheFile = Path::makeAbsolute($cacheFile, $this->workingDirectory);
}

$builder->cacheFile = new SplFileInfo($cacheFile);

return $builder;
$this->cacheFile = new SplFileInfo($cacheFile);
}

public function clearCache(?string $cacheFile): self
private function clearCache(string $cacheFile): void
{
if (null === $cacheFile) {
return $this;
}

$builder = clone $this;

if (Path::isRelative($cacheFile)) {
/** @throws void */
$cacheFile = Path::makeAbsolute($cacheFile, $this->workingDirectory);
}

unlink($cacheFile);

return $builder;
}

/**
* @throws CacheFileException
* @throws CannotLoadConfiguration
*/
public function build(): ContainerBuilder
public function build(string|false|null $cacheOverride, bool $clearCache): ContainerBuilder
{
$container = new ContainerBuilder();

$container->setParameter('currentWorkingDirectory', $this->workingDirectory);

self::registerCompilerPasses($container);
self::loadServices($container, $this->cacheFile);

$container->registerExtension(new DeptracExtension());

Expand All @@ -100,6 +83,19 @@ public function build(): ContainerBuilder
self::loadConfiguration($container, $this->configFile);
}

/** @var ?string $cacheFileFromConfig */
$cacheFileFromConfig = $container->getExtensionConfig('deptrac')[0]['cache_file'] ?? null; // if there is any
$cache = $cacheOverride ?? $cacheFileFromConfig; // override if there is a no-cache or path to file
$cache = $cache ?? '.deptrac.cache'; // override if there is no file specified and needs one

if (false !== $cache) {
if ($clearCache) {
$this->clearCache($cache);
}
$this->withCache($cache);
}

self::loadServices($container, $this->cacheFile);
$container->compile(true);

return $container;
Expand Down Expand Up @@ -146,7 +142,7 @@ private static function loadServices(ContainerBuilder $container, ?SplFileInfo $
}
}

$container->setParameter('deptrac.cache_file', $cacheFile->getPathname());
$container->setParameter('cache_file', $cacheFile->getPathname());
try {
$loader->load('cache.php');
} catch (Exception $exception) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Supportive/DependencyInjection/DeptracExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function testDefaults(): void
self::assertSame(self::FORMATTER_DEFAULTS, $this->container->getParameter('formatters'));
self::assertSame(self::ANALYSER_DEFAULTS, $this->container->getParameter('analyser'));
self::assertSame(true, $this->container->getParameter('ignore_uncovered_internal_classes'));
self::assertSame('.deptrac.cache', $this->container->getParameter('cache_file'));
}

public function testDefaultsWithEmptyRoot(): void
Expand All @@ -83,6 +84,7 @@ public function testDefaultsWithEmptyRoot(): void
self::assertSame(self::FORMATTER_DEFAULTS, $this->container->getParameter('formatters'));
self::assertSame(self::ANALYSER_DEFAULTS, $this->container->getParameter('analyser'));
self::assertSame(true, $this->container->getParameter('ignore_uncovered_internal_classes'));
self::assertSame('.deptrac.cache', $this->container->getParameter('cache_file'));
}

public function testPathsWithMultipleElements(): void
Expand Down Expand Up @@ -426,6 +428,19 @@ public function testIgnoreUncoveredInternalClasses(): void
self::assertSame(false, $this->container->getParameter('ignore_uncovered_internal_classes'));
}

public function testCacheFile(): void
{
$configs = [
'deptrac' => [
'cache_file' => 'test.path',
],
];

$this->extension->load($configs, $this->container);

self::assertSame('test.path', $this->container->getParameter('cache_file'));
}

public function testGraphvizFormatterWithEmptyNodes(): void
{
$configs = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testBuildsContainerWithDefaultParameters(): void
{
$builder = new ServiceContainerBuilder(__DIR__);

$container = $builder->build();
$container = $builder->build(null, false);

self::assertTrue($container->getParameter('ignore_uncovered_internal_classes'));
self::assertSame(
Expand All @@ -40,5 +40,6 @@ public function testBuildsContainerWithDefaultParameters(): void
[],
$container->getParameter('skip_violations')
);
self::assertSame(__DIR__.'/.deptrac.cache', $container->getParameter('cache_file'));
}
}

0 comments on commit 5754847

Please sign in to comment.