Skip to content

Commit

Permalink
Merge pull request #174 from gacela-project/feature/custom-caching-dir
Browse files Browse the repository at this point in the history
Allow defining custom cache directory name
  • Loading branch information
JesusValeraDev authored Jun 24, 2022
2 parents d889eb0 + f598fbe commit e75f257
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

- Group gacela cache files inside a `cache/` directory.
- Allow enabling/disabling cache files from the project config files.
- Added `setCacheDirectory()` to `GacelaConfig`.
- Added `vendor/bin/gacela` script.
- Add `.editorconfig` & `.gitattributes` files.
- Ignore `composer.lock`.
Expand Down
11 changes: 11 additions & 0 deletions src/Framework/Bootstrap/GacelaConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ final class GacelaConfig

private bool $cacheEnabled = true;

private string $cacheDirectory = 'cache';

/**
* @param array<string,class-string|object|callable> $externalServices
*/
Expand Down Expand Up @@ -120,13 +122,21 @@ public function setCacheEnabled(bool $flag): self
return $this;
}

public function setCacheDirectory(string $dir): self
{
$this->cacheDirectory = $dir;

return $this;
}

/**
* @return array{
* external-services:array<string,class-string|object|callable>,
* config-builder:ConfigBuilder,
* suffix-types-builder:SuffixTypesBuilder,
* mapping-interfaces-builder:MappingInterfacesBuilder,
* cache-enabled:bool,
* cache-directory:string,
* }
*
* @internal
Expand All @@ -139,6 +149,7 @@ public function build(): array
'suffix-types-builder' => $this->suffixTypesBuilder,
'mapping-interfaces-builder' => $this->mappingInterfacesBuilder,
'cache-enabled' => $this->cacheEnabled,
'cache-directory' => $this->cacheDirectory,
];
}
}
17 changes: 16 additions & 1 deletion src/Framework/Bootstrap/SetupGacela.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class SetupGacela extends AbstractSetupGacela

private bool $cacheEnabled = true;

private string $cacheDirectory = 'cache';

public function __construct()
{
$this->configFn = static function (): void {
Expand Down Expand Up @@ -61,7 +63,8 @@ public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self
->setSuffixTypesBuilder($build['suffix-types-builder'])
->setMappingInterfacesBuilder($build['mapping-interfaces-builder'])
->setExternalServices($build['external-services'])
->setCacheEnabled($build['cache-enabled']);
->setCacheEnabled($build['cache-enabled'])
->setCacheDirectory($build['cache-directory']);
}

public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self
Expand Down Expand Up @@ -190,4 +193,16 @@ public function isCacheEnabled(): bool
{
return $this->cacheEnabled;
}

public function setCacheDirectory(string $dir): self
{
$this->cacheDirectory = $dir;

return $this;
}

public function getCacheDirectory(): string
{
return $this->cacheDirectory;
}
}
2 changes: 2 additions & 0 deletions src/Framework/Bootstrap/SetupGacelaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public function buildSuffixTypes(SuffixTypesBuilder $suffixTypesBuilder): Suffix
*/
public function externalServices(): array;

public function getCacheDirectory(): string;

public function isCacheEnabled(): bool;
}
9 changes: 7 additions & 2 deletions src/Framework/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getAppRootDir(): string

public function getCacheDir(): string
{
return $this->getAppRootDir() . '/cache/';
return $this->getAppRootDir() . '/' . $this->getSetupGacela()->getCacheDirectory();
}

public function setSetup(SetupGacelaInterface $setup): self
Expand All @@ -117,13 +117,18 @@ public function getFactory(): ConfigFactory
if ($this->configFactory === null) {
$this->configFactory = new ConfigFactory(
$this->getAppRootDir(),
$this->setup ?? new SetupGacela()
$this->getSetupGacela()
);
}

return $this->configFactory;
}

private function getSetupGacela(): SetupGacelaInterface
{
return $this->setup ?? new SetupGacela();
}

private function hasValue(string $key): bool
{
return array_key_exists($key, $this->config);
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CodeGenerator/MakeFileCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GacelaTest\Feature\CodeGenerator;

use GacelaTest\Feature\CodeGenerator\Util\DirectoryUtil;
use GacelaTest\Feature\Util\DirectoryUtil;
use PHPUnit\Framework\TestCase;

final class MakeFileCommandTest extends TestCase
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CodeGenerator/MakeModuleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GacelaTest\Feature\CodeGenerator;

use GacelaTest\Feature\CodeGenerator\Util\DirectoryUtil;
use GacelaTest\Feature\Util\DirectoryUtil;
use PHPUnit\Framework\TestCase;

final class MakeModuleCommandTest extends TestCase
Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/Framework/CustomCacheDirectory/FeatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace GacelaTest\Feature\Framework\CustomCacheDirectory;

use Gacela\Framework\Bootstrap\GacelaConfig;
use Gacela\Framework\ClassResolver\ClassNameCache;
use Gacela\Framework\ClassResolver\DocBlockService\CustomServicesCache;
use Gacela\Framework\Gacela;
use GacelaTest\Feature\Util\DirectoryUtil;
use PHPUnit\Framework\TestCase;

final class FeatureTest extends TestCase
{
public function setUp(): void
{
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void {
$config->addAppConfig('config/*.php');
$config->setCacheDirectory('custom-caching-dir');
});
}

public function tearDown(): void
{
DirectoryUtil::removeDir(__DIR__ . '/custom-caching-dir');
}

public function test_custom_caching_dir(): void
{
$facade = new Module\Facade();
self::assertSame('name', $facade->getName());

self::assertFileExists(__DIR__ . '/custom-caching-dir/' . ClassNameCache::CACHE_FILENAME);
self::assertFileExists(__DIR__ . '/custom-caching-dir/' . CustomServicesCache::CACHE_FILENAME);
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Framework/CustomCacheDirectory/Module/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace GacelaTest\Feature\Framework\CustomCacheDirectory\Module;

use Gacela\Framework\AbstractFacade;
use Gacela\Framework\DocBlockResolverAwareTrait;
use GacelaTest\Feature\Framework\CustomCacheDirectory\Module\Persistence\FakeRepository;

/**
* @method FakeRepository getRepository()
*/
final class Facade extends AbstractFacade
{
use DocBlockResolverAwareTrait;

public function getName(): string
{
return $this->getRepository()->findName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace GacelaTest\Feature\Framework\CustomCacheDirectory\Module\Persistence;

final class FakeRepository
{
public function findName(): string
{
return 'name';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace GacelaTest\Feature\CodeGenerator\Util;
namespace GacelaTest\Feature\Util;

use FilesystemIterator;
use RecursiveDirectoryIterator;
Expand Down

0 comments on commit e75f257

Please sign in to comment.