stfalcon-studio / DoctrineRedisCacheBundle

Compare afab044 ... +4 ... 405f151

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.


@@ -12,6 +12,9 @@
Loading
12 12
13 13
namespace StfalconStudio\DoctrineRedisCacheBundle;
14 14
15 +
use StfalconStudio\DoctrineRedisCacheBundle\DependencyInjection\Compiler\DetectLastMigrationPass;
16 +
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
17 +
use Symfony\Component\DependencyInjection\ContainerBuilder;
15 18
use Symfony\Component\HttpKernel\Bundle\Bundle;
16 19
17 20
/**
@@ -21,4 +24,13 @@
Loading
21 24
 */
22 25
class StfalconStudioDoctrineRedisCacheBundle extends Bundle
23 26
{
27 +
    /**
28 +
     * {@inheritdoc}
29 +
     */
30 +
    public function build(ContainerBuilder $container): void
31 +
    {
32 +
        parent::build($container);
33 +
34 +
        $container->addCompilerPass(new DetectLastMigrationPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 33);
35 +
    }
24 36
}

@@ -0,0 +1,80 @@
Loading
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\DependencyInjection\Compiler;
14 +
15 +
use Doctrine\Migrations\Configuration\Configuration;
16 +
use Doctrine\Migrations\Finder\MigrationFinder;
17 +
use StfalconStudio\DoctrineRedisCacheBundle\Exception\InvalidArgumentException;
18 +
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19 +
use Symfony\Component\DependencyInjection\ContainerBuilder;
20 +
21 +
/**
22 +
 * DetectLastMigrationPass.
23 +
 *
24 +
 * @author Artem Henvald <genvaldartem@gmail.com>
25 +
 */
26 +
final class DetectLastMigrationPass implements CompilerPassInterface
27 +
{
28 +
    /**
29 +
     * {@inheritdoc}
30 +
     */
31 +
    public function process(ContainerBuilder $container): void
32 +
    {
33 +
        $migrationFinder = $container->get(MigrationFinder::class);
34 +
        if (!$migrationFinder instanceof MigrationFinder) {
35 +
            throw new InvalidArgumentException(\sprintf('Service "%s" is missed in container', MigrationFinder::class));
36 +
        }
37 +
38 +
        $configuration = $container->get('doctrine.migrations.configuration');
39 +
        if (!$configuration instanceof Configuration) {
40 +
            throw new InvalidArgumentException(\sprintf('Service "%s" is missed in container', Configuration::class));
41 +
        }
42 +
43 +
        $migrations = [];
44 +
        foreach ($configuration->getMigrationDirectories() as $migrationDirectory) {
45 +
            foreach ($migrationFinder->findMigrations($migrationDirectory) as $migration) {
46 +
                $migrations[] = $migration;
47 +
            }
48 +
        }
49 +
        foreach ($configuration->getMigrationClasses() as $migration) {
50 +
            $migrations[] = $migration;
51 +
        }
52 +
53 +
        $processedMigrations = [];
54 +
        foreach ($migrations as $migration) {
55 +
            \preg_match('#Version.*#', $migration, $matches);
56 +
            if (!empty($matches[0])) {
57 +
                $processedMigrations[] = $matches[0];
58 +
            }
59 +
        }
60 +
61 +
        \sort($processedMigrations); // Sort by name
62 +
        $latest = \end($processedMigrations);
63 +
64 +
        $cachePools = [];
65 +
        if ($container->hasParameter('doctrine_redis_cache.cache_pools')) {
66 +
            $cachePools = (array) $container->getParameter('doctrine_redis_cache.cache_pools');
67 +
        }
68 +
69 +
        if (false !== $latest && !empty($cachePools)) {
70 +
            $latestMigration = (string) $latest;
71 +
72 +
            foreach ($cachePools as $cachePool) {
73 +
                $definition = $container->getDefinition($cachePool);
74 +
                $tags = $definition->getTags();
75 +
                $tags['cache.pool'][0]['namespace'] = $latestMigration;
76 +
                $definition->setTags($tags);
77 +
            }
78 +
        }
79 +
    }
80 +
}

@@ -12,53 +12,28 @@
Loading
12 12
13 13
namespace StfalconStudio\DoctrineRedisCacheBundle\DependencyInjection;
14 14
15 -
use Doctrine\Migrations\Finder\RecursiveRegexFinder;
16 15
use Symfony\Component\Config\FileLocator;
17 16
use Symfony\Component\DependencyInjection\ContainerBuilder;
18 17
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19 18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20 19
21 20
/**
22 -
 * This is the class that loads and manages StfalconStudioDoctrineRedisCacheBundle configuration.
21 +
 * StfalconStudioDoctrineRedisCacheExtension.
23 22
 *
24 23
 * @author Artem Henvald <genvaldartem@gmail.com>
25 24
 */
26 25
class StfalconStudioDoctrineRedisCacheExtension extends Extension
27 26
{
28 -
    /** @var RecursiveRegexFinder */
29 -
    private $migrationFinder;
30 -
31 -
    /**
32 -
     * Constructor.
33 -
     */
34 -
    public function __construct()
35 -
    {
36 -
        $this->migrationFinder = new RecursiveRegexFinder();
37 -
    }
38 -
39 27
    /**
40 28
     * {@inheritdoc}
41 29
     */
42 30
    public function load(array $configs, ContainerBuilder $container): void
43 31
    {
32 +
        $configuration = new Configuration();
33 +
        $config = $this->processConfiguration($configuration, $configs);
34 +
        $container->setParameter('doctrine_redis_cache.cache_pools', $config['cache_pools']);
35 +
44 36
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
45 37
        $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';
63 38
    }
64 39
}

@@ -0,0 +1,48 @@
Loading
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\DependencyInjection;
14 +
15 +
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16 +
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17 +
use Symfony\Component\Config\Definition\ConfigurationInterface;
18 +
19 +
/**
20 +
 * Configuration.
21 +
 *
22 +
 * @author Artem Henvald <genvaldartem@gmail.com>
23 +
 */
24 +
class Configuration implements ConfigurationInterface
25 +
{
26 +
    /**
27 +
     * {@inheritdoc}
28 +
     */
29 +
    public function getConfigTreeBuilder(): TreeBuilder
30 +
    {
31 +
        $treeBuilder = new TreeBuilder('stfalcon_studio_doctrine_redis_cache');
32 +
33 +
        /** @var ArrayNodeDefinition $root */
34 +
        $root = $treeBuilder->getRootNode();
35 +
36 +
        $root
37 +
            ->children()
38 +
                ->arrayNode('cache_pools')
39 +
                    ->info('A list of cache pools for which should be set the custom namespace.')
40 +
                    ->prototype('scalar')->end()
41 +
                    ->defaultValue([])
42 +
                ->end()
43 +
            ->end()
44 +
        ;
45 +
46 +
        return $treeBuilder;
47 +
    }
48 +
}

Learn more Showing 3 files with coverage changes found.

New file DependencyInjection/Configuration.php
New
Loading file...
New file DependencyInjection/Compiler/DetectLastMigrationPass.php
New
Loading file...
New file StfalconStudioDoctrineRedisCacheBundle.php
New
Loading file...
Files Complexity Coverage
DependencyInjection ø 100.00%
StfalconStudioDoctrineRedisCacheBundle.php ø 100.00%
Project Totals (4 files) 15 100.00%
Loading