Skip to content

Commit

Permalink
Возможность заменить кэшер на любой другой.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 21, 2021
1 parent c70a2da commit 9ff6420
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
40 changes: 40 additions & 0 deletions DependencyInjection/CompilerPass/ReplaceCacherCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Prokl\InstagramParserRapidApiBundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class ReplaceCacherCompilerPass
*
* @since 21.07.2021
*/
class ReplaceCacherCompilerPass implements CompilerPassInterface
{
/**
* @inheritdoc
*/
public function process(ContainerBuilder $container): void
{
$cacherService = $container->getParameter('instagram_parser_rapid_api.cacher');

if (!$cacherService) {
return;
}

$destinationDefinitionParser = $container->getDefinition('instagram_parser_rapid_api.rapid_api');
$destinationDefinitionUserService = $container->getDefinition('instagram_parser_rapid_api.rapid_api_get_user_id');

if (!$container->hasDefinition($cacherService)) {
throw new \RuntimeException(
sprintf('Cacher service %s from parameter cacher_service not exist.', $cacherService)
);
}

$cacherDefinition = $container->getDefinition($cacherService);

$destinationDefinitionParser->replaceArgument(0, $cacherDefinition);
$destinationDefinitionUserService->replaceArgument(0, $cacherDefinition);
}
}
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('fixture_user_path')->defaultValue(
'/local/config/Fixture/user.txt'
)->end()
->scalarNode('cacher_service')->defaultValue('')->end()
->scalarNode('cache_path')->defaultValue('cache/instagram-parser')->end()
->scalarNode('cache_ttl')->defaultValue(86400)->end()
->scalarNode('cache_user_data_ttl')->defaultValue(31536000)->end()
Expand Down
8 changes: 7 additions & 1 deletion DependencyInjection/InstagramParserRapidApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function load(array $configs, ContainerBuilder $container) : void
} else {
$loader->load('psr-cache.yaml');
}

$container->setParameter('instagram_parser_rapid_api.cacher', $config['cacher_service']);
$cacherService = $config['cacher_service'];
if ($cacherService) {
$container->setParameter('instagram_parser_rapid_api.cacher', $config['cacher_service']);
}
}

/**
Expand All @@ -63,4 +69,4 @@ public function getAlias()
{
return 'instagram_parser_rapid_api';
}
}
}
17 changes: 14 additions & 3 deletions InstagramParserRapidApiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Prokl\InstagramParserRapidApiBundle;

use Prokl\InstagramParserRapidApiBundle\DependencyInjection\InstagramParserRapidApiExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Prokl\InstagramParserRapidApiBundle\DependencyInjection\CompilerPass\ReplaceCacherCompilerPass;

/**
* Class InstagramParserRapidApiBundle
Expand All @@ -13,9 +15,9 @@
*/
class InstagramParserRapidApiBundle extends Bundle
{
/**
* @inheritDoc
*/
/**
* @inheritDoc
*/
public function getContainerExtension()
{
if ($this->extension === null) {
Expand All @@ -24,4 +26,13 @@ public function getContainerExtension()

return $this->extension;
}
/**
* @inheritDoc
*/
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new ReplaceCacherCompilerPass());
}
}
1 change: 1 addition & 0 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ instagram_parser_rapid_api:
cache_path: 'cache/s1/instagram-parser'
cache_ttl: 86400
cache_user_data_ttl: 31536000
cacher_service: 'app.cache.adapter.redis' # Любой кэшер, реализующий Symfony\Contracts\Cache\CacheInterface

##############
# Мок запроса
Expand Down

0 comments on commit 9ff6420

Please sign in to comment.