From ed25aaf13a3ff128cf28357cf04c10f1e781e7cd Mon Sep 17 00:00:00 2001 From: Artem Henvald Date: Tue, 14 Apr 2020 14:41:25 +0300 Subject: [PATCH] Add cache prefix seed --- ...alconStudioDoctrineRedisCacheExtension.php | 29 +++++++++++++++ README.md | 2 +- ...nStudioDoctrineRedisCacheExtensionTest.php | 13 ++++++- Tests/Migrations/Version20200101000001.php | 36 +++++++++++++++++++ Tests/Migrations/empty/.gitkeep | 0 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 Tests/Migrations/Version20200101000001.php create mode 100644 Tests/Migrations/empty/.gitkeep diff --git a/DependencyInjection/StfalconStudioDoctrineRedisCacheExtension.php b/DependencyInjection/StfalconStudioDoctrineRedisCacheExtension.php index 1addb90..dce1dcb 100644 --- a/DependencyInjection/StfalconStudioDoctrineRedisCacheExtension.php +++ b/DependencyInjection/StfalconStudioDoctrineRedisCacheExtension.php @@ -12,6 +12,7 @@ namespace StfalconStudio\DoctrineRedisCacheBundle\DependencyInjection; +use Doctrine\Migrations\Finder\RecursiveRegexFinder; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; @@ -24,6 +25,17 @@ */ class StfalconStudioDoctrineRedisCacheExtension extends Extension { + /** @var RecursiveRegexFinder */ + private $migrationFinder; + + /** + * Constructor. + */ + public function __construct() + { + $this->migrationFinder = new RecursiveRegexFinder(); + } + /** * {@inheritdoc} */ @@ -31,5 +43,22 @@ public function load(array $configs, ContainerBuilder $container): void { $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yaml'); + + $container->setParameter('cache_prefix_seed', $this->getLastMigrationVersion($container->getParameter('doctrine_migrations.dir_name'))); + } + + /** + * @param string $dir + * + * @return string + */ + public function getLastMigrationVersion(string $dir): string + { + $migrations = $this->migrationFinder->findMigrations($dir); + + $versions = \array_keys($migrations); + $latest = \end($versions); + + return false !== $latest ? (string) $latest : '0'; } } diff --git a/README.md b/README.md index 1a3f206..af628de 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ After that you can stop or rerun old script. And after rerun they will use a new ## Installation -```composer req stfalcon-studio/doctrine-redis-cache-bundle='~1.2'``` +```composer req stfalcon-studio/doctrine-redis-cache-bundle='~1.3'``` #### Check the `config/bundles.php` file diff --git a/Tests/DependencyInjection/StfalconStudioDoctrineRedisCacheExtensionTest.php b/Tests/DependencyInjection/StfalconStudioDoctrineRedisCacheExtensionTest.php index 78101ff..0b75ca6 100644 --- a/Tests/DependencyInjection/StfalconStudioDoctrineRedisCacheExtensionTest.php +++ b/Tests/DependencyInjection/StfalconStudioDoctrineRedisCacheExtensionTest.php @@ -46,10 +46,12 @@ protected function tearDown(): void public function testLoadExtension(): void { - $this->container->setParameter('doctrine_migrations.dir_name', []); // Just add a dummy required parameter + $this->container->setParameter('doctrine_migrations.dir_name', __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Migrations'); $this->container->loadFromExtension($this->extension->getAlias()); $this->container->compile(); + self::assertSame('20200101000001', $this->container->getParameter('cache_prefix_seed')); + self::assertArrayHasKey(MigrationVersionService::class, $this->container->getRemovedIds()); self::assertArrayHasKey(MigrationFinder::class, $this->container->getRemovedIds()); @@ -61,4 +63,13 @@ public function testLoadExtension(): void $this->container->get(MigrationVersionService::class); $this->container->get(MigrationFinder::class); } + + public function testLoadExtensionWithNotMigration(): void + { + $this->container->setParameter('doctrine_migrations.dir_name', __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Migrations'.\DIRECTORY_SEPARATOR.'empty'); + $this->container->loadFromExtension($this->extension->getAlias()); + $this->container->compile(); + + self::assertSame('0', $this->container->getParameter('cache_prefix_seed')); + } } diff --git a/Tests/Migrations/Version20200101000001.php b/Tests/Migrations/Version20200101000001.php new file mode 100644 index 0000000..08a1c3a --- /dev/null +++ b/Tests/Migrations/Version20200101000001.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace StfalconStudio\DoctrineRedisCacheBundle\Tests\Migrations; + +use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; + +/** + * Blank migration. + */ +final class Version20200101000001 extends AbstractMigration +{ + /** + * @param Schema $schema + */ + public function up(Schema $schema) : void + { + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) : void + { + } +} diff --git a/Tests/Migrations/empty/.gitkeep b/Tests/Migrations/empty/.gitkeep new file mode 100644 index 0000000..e69de29