Skip to content

Commit ed25aaf

Browse files
authored
Add cache prefix seed
1 parent c49b490 commit ed25aaf

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

DependencyInjection/StfalconStudioDoctrineRedisCacheExtension.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace StfalconStudio\DoctrineRedisCacheBundle\DependencyInjection;
1414

15+
use Doctrine\Migrations\Finder\RecursiveRegexFinder;
1516
use Symfony\Component\Config\FileLocator;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
@@ -24,12 +25,40 @@
2425
*/
2526
class StfalconStudioDoctrineRedisCacheExtension extends Extension
2627
{
28+
/** @var RecursiveRegexFinder */
29+
private $migrationFinder;
30+
31+
/**
32+
* Constructor.
33+
*/
34+
public function __construct()
35+
{
36+
$this->migrationFinder = new RecursiveRegexFinder();
37+
}
38+
2739
/**
2840
* {@inheritdoc}
2941
*/
3042
public function load(array $configs, ContainerBuilder $container): void
3143
{
3244
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3345
$loader->load('services.yaml');
46+
47+
$container->setParameter('cache_prefix_seed', $this->getLastMigrationVersion($container->getParameter('doctrine_migrations.dir_name')));
48+
}
49+
50+
/**
51+
* @param string $dir
52+
*
53+
* @return string
54+
*/
55+
public function getLastMigrationVersion(string $dir): string
56+
{
57+
$migrations = $this->migrationFinder->findMigrations($dir);
58+
59+
$versions = \array_keys($migrations);
60+
$latest = \end($versions);
61+
62+
return false !== $latest ? (string) $latest : '0';
3463
}
3564
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ After that you can stop or rerun old script. And after rerun they will use a new
2727

2828
## Installation
2929

30-
```composer req stfalcon-studio/doctrine-redis-cache-bundle='~1.2'```
30+
```composer req stfalcon-studio/doctrine-redis-cache-bundle='~1.3'```
3131

3232
#### Check the `config/bundles.php` file
3333

Tests/DependencyInjection/StfalconStudioDoctrineRedisCacheExtensionTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ protected function tearDown(): void
4646

4747
public function testLoadExtension(): void
4848
{
49-
$this->container->setParameter('doctrine_migrations.dir_name', []); // Just add a dummy required parameter
49+
$this->container->setParameter('doctrine_migrations.dir_name', __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Migrations');
5050
$this->container->loadFromExtension($this->extension->getAlias());
5151
$this->container->compile();
5252

53+
self::assertSame('20200101000001', $this->container->getParameter('cache_prefix_seed'));
54+
5355
self::assertArrayHasKey(MigrationVersionService::class, $this->container->getRemovedIds());
5456
self::assertArrayHasKey(MigrationFinder::class, $this->container->getRemovedIds());
5557

@@ -61,4 +63,13 @@ public function testLoadExtension(): void
6163
$this->container->get(MigrationVersionService::class);
6264
$this->container->get(MigrationFinder::class);
6365
}
66+
67+
public function testLoadExtensionWithNotMigration(): void
68+
{
69+
$this->container->setParameter('doctrine_migrations.dir_name', __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Migrations'.\DIRECTORY_SEPARATOR.'empty');
70+
$this->container->loadFromExtension($this->extension->getAlias());
71+
$this->container->compile();
72+
73+
self::assertSame('0', $this->container->getParameter('cache_prefix_seed'));
74+
}
6475
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/*
3+
* This file is part of the StfalconStudioDoctrineRedisCacheBundle.
4+
*
5+
* (c) Stfalcon LLC <stfalcon.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace StfalconStudio\DoctrineRedisCacheBundle\Tests\Migrations;
14+
15+
use Doctrine\DBAL\Schema\Schema;
16+
use Doctrine\Migrations\AbstractMigration;
17+
18+
/**
19+
* Blank migration.
20+
*/
21+
final class Version20200101000001 extends AbstractMigration
22+
{
23+
/**
24+
* @param Schema $schema
25+
*/
26+
public function up(Schema $schema) : void
27+
{
28+
}
29+
30+
/**
31+
* @param Schema $schema
32+
*/
33+
public function down(Schema $schema) : void
34+
{
35+
}
36+
}

Tests/Migrations/empty/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)