Skip to content

Commit

Permalink
Add cache prefix seed
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h committed Apr 14, 2020
1 parent c49b490 commit ed25aaf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
29 changes: 29 additions & 0 deletions DependencyInjection/StfalconStudioDoctrineRedisCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,12 +25,40 @@
*/
class StfalconStudioDoctrineRedisCacheExtension extends Extension
{
/** @var RecursiveRegexFinder */
private $migrationFinder;

/**
* Constructor.
*/
public function __construct()
{
$this->migrationFinder = new RecursiveRegexFinder();
}

/**
* {@inheritdoc}
*/
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';
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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'));
}
}
36 changes: 36 additions & 0 deletions Tests/Migrations/Version20200101000001.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the StfalconStudioDoctrineRedisCacheBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* 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
{
}
}
Empty file added Tests/Migrations/empty/.gitkeep
Empty file.

0 comments on commit ed25aaf

Please sign in to comment.