Skip to content

Commit d3ece80

Browse files
committed
CacheWarmers
1 parent b32939b commit d3ece80

File tree

11 files changed

+506
-1
lines changed

11 files changed

+506
-1
lines changed

DependencyInjection/BitrixOrdinaryToolsExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function load(array $configs, ContainerBuilder $container) : void
3737
$loader->load('seo.yaml');
3838
$loader->load('image_resizer.yaml');
3939
$loader->load('loggers.yaml');
40+
$loader->load('warmers.yaml');
4041

4142
$loader->load('notifier.yaml');
4243
if (class_exists(NotifierInterface::class)) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\DependencyInjection\CompilerPass;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\Routing\Router;
8+
9+
/**
10+
* Class WarmersConfiguratorCompilerPass
11+
* @package Prokl\BitrixOrdinaryToolsBundle\DependencyInjection\CompilerPass
12+
*
13+
* @since 06.08.2021
14+
*/
15+
final class WarmersConfiguratorCompilerPass implements CompilerPassInterface
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function process(ContainerBuilder $container)
21+
{
22+
// Нет роутера - нет команды по прогреву кэша роутера.
23+
if (!class_exists(Router::class)
24+
||
25+
!$container->hasDefinition('router')
26+
) {
27+
$container->removeDefinition('bitrix_ordinary_tools.router_cache_warmer');
28+
}
29+
30+
// Нет http клиента - нет команды по прогреву кэша битриксовых страниц.
31+
if (!$container->hasDefinition('http_client')
32+
&&
33+
!$container->hasAlias('http_client')
34+
) {
35+
$container->removeDefinition('bitrix_ordinary_tools.bitrix_page_cache_warmer');
36+
37+
return;
38+
}
39+
40+
// Нет в контейнере параметра warming_pages - прогрев только главной страницы.
41+
if (!$container->hasParameter('warming_pages')) {
42+
$definition = $container->getDefinition('bitrix_ordinary_tools.bitrix_page_cache_warmer');
43+
$definition->replaceArgument(2, ['/']);
44+
}
45+
}
46+
}

Resources/config/warmers.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
################
2+
# Cache warmers
3+
################
4+
5+
services:
6+
# конфигурация по умолчанию в *этом* файле
7+
_defaults:
8+
autowire: true
9+
autoconfigure: true
10+
public: true
11+
12+
# Прогрев кэша роутера
13+
bitrix_ordinary_tools.router_cache_warmer:
14+
autoconfigure: false
15+
arguments:
16+
- '@router'
17+
class: Prokl\BitrixOrdinaryToolsBundle\Services\CacheWarmer\RouterCacheWarm
18+
tags: [kernel.cache_warmer]
19+
20+
# Прогрев кэша сайта на Битриксе
21+
bitrix_ordinary_tools.bitrix_page_cache_warmer:
22+
autoconfigure: false
23+
arguments:
24+
- '@http_client'
25+
- '%kernel.site.host%'
26+
- ['%warming_pages%']
27+
class: Prokl\BitrixOrdinaryToolsBundle\Services\CacheWarmer\BitrixCacheWarmer
28+
tags: [kernel.cache_warmer]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\CacheWarmer;
4+
5+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
6+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
7+
use Symfony\Contracts\HttpClient\HttpClientInterface;
8+
9+
/**
10+
* Class BitrixCacheWarmer
11+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\CacheWarmer
12+
*
13+
* @since 06.08.2021
14+
*/
15+
class BitrixCacheWarmer implements CacheWarmerInterface
16+
{
17+
/**
18+
* @var HttpClientInterface $httpClient Клиент.
19+
*/
20+
private $httpClient;
21+
22+
/**
23+
* @var string $httpHost Адрес сайта со схемой.
24+
*/
25+
private $httpHost;
26+
27+
/**
28+
* @var array $urls URL для прогрева.
29+
*/
30+
private $urls;
31+
32+
/**
33+
* BitrixCacheWarmer constructor.
34+
*
35+
* @param HttpClientInterface $httpClient Клиент.
36+
* @param string $httpHost Адрес сайта со схемой.
37+
* @param array $urls URL-ы для прогрева.
38+
*/
39+
public function __construct(
40+
HttpClientInterface $httpClient,
41+
string $httpHost,
42+
array $urls = []
43+
) {
44+
$this->httpClient = $httpClient;
45+
$this->httpHost = $httpHost;
46+
$this->urls = $urls;
47+
}
48+
49+
/**
50+
* @inheritDoc
51+
*/
52+
public function isOptional()
53+
{
54+
return true;
55+
}
56+
57+
/**
58+
* @inheritDoc
59+
*/
60+
public function warmUp($cacheDir)
61+
{
62+
foreach ($this->urls as $url) {
63+
$this->loadUri($url);
64+
}
65+
66+
return [];
67+
}
68+
69+
/**
70+
* @param string $uri URL к загрузке.
71+
*
72+
* @return void
73+
*/
74+
private function loadUri(string $uri) : void
75+
{
76+
try {
77+
$this->httpClient->request('GET', $this->httpHost . $uri);
78+
} catch (TransportExceptionInterface $e) {
79+
// Silence gold
80+
}
81+
}
82+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\CacheWarmer;
4+
5+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
6+
use Symfony\Component\Routing\Router;
7+
8+
/**
9+
* Class RouterCacheWarm
10+
* @package Local\Services\Bitrix
11+
*
12+
* @since 06.08.2021
13+
*/
14+
class RouterCacheWarm implements CacheWarmerInterface
15+
{
16+
/**
17+
* @var Router $router Роутер.
18+
*/
19+
private $router;
20+
21+
/**
22+
* RouterCacheWarm constructor.
23+
*
24+
* @param Router $router Роутер.
25+
*/
26+
public function __construct(Router $router)
27+
{
28+
$this->router = $router;
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function isOptional()
35+
{
36+
return true;
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function warmUp($cacheDir)
43+
{
44+
$this->router->getMatcher();
45+
$this->router->getGenerator();
46+
47+
return [];
48+
}
49+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Tests\Cases\CompilerPass;
4+
5+
use Prokl\BitrixOrdinaryToolsBundle\DependencyInjection\CompilerPass\WarmersConfiguratorCompilerPass;
6+
use Prokl\TestingTools\Base\BaseTestCase;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Definition;
9+
10+
/**
11+
* Class WarmersConfiguratorCompilerPassTest
12+
* @package Prokl\BitrixOrdinaryToolsBundle\Tests\Cases\CompilerPass
13+
*
14+
* @since 06.08.2021
15+
*/
16+
class WarmersConfiguratorCompilerPassTest extends BaseTestCase
17+
{
18+
/**
19+
* @var WarmersConfiguratorCompilerPass $obTestObject
20+
*/
21+
protected $obTestObject;
22+
23+
/**
24+
* @var object $fakeService
25+
*/
26+
private $fakeService;
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
35+
$this->fakeService = new class {
36+
public function handle()
37+
{
38+
}
39+
};
40+
41+
$this->container = new ContainerBuilder();
42+
43+
$this->obTestObject = new WarmersConfiguratorCompilerPass();
44+
}
45+
46+
/**
47+
* Нет http клиента.
48+
*
49+
* @return void
50+
*/
51+
public function testNoHttpClient() : void
52+
{
53+
$this->container->setDefinition(
54+
'bitrix_ordinary_tools.bitrix_page_cache_warmer',
55+
new Definition(get_class($this->fakeService))
56+
);
57+
58+
$this->obTestObject->process($this->container);
59+
60+
$this->assertFalse($this->container->hasDefinition('bitrix_ordinary_tools.bitrix_page_cache_warmer'));
61+
}
62+
63+
/**
64+
* Не задан параметр warming_pages.
65+
*
66+
* @return void
67+
*/
68+
public function testNoPageParameters() : void
69+
{
70+
$this->container->setDefinition(
71+
'http_client',
72+
new Definition(get_class($this->fakeService))
73+
);
74+
75+
$definition = new Definition(get_class($this->fakeService));
76+
$definition->addArgument($this->container->getDefinition('http_client'));
77+
$definition->addArgument('localhost');
78+
$definition->addArgument(['/xxx/']);
79+
80+
$this->container->setDefinition(
81+
'bitrix_ordinary_tools.bitrix_page_cache_warmer',
82+
$definition
83+
);
84+
85+
$this->obTestObject->process($this->container);
86+
87+
$newDefinition = $this->container->getDefinition('bitrix_ordinary_tools.bitrix_page_cache_warmer');
88+
$newParam = $newDefinition->getArguments();
89+
90+
$this->assertSame(['/'], $newParam[2]);
91+
}
92+
93+
/**
94+
* Нет router.
95+
*
96+
* @return void
97+
*/
98+
public function testNoRouter() : void
99+
{
100+
$this->container->setDefinition(
101+
'bitrix_ordinary_tools.router_cache_warmer',
102+
new Definition(get_class($this->fakeService))
103+
);
104+
105+
$this->obTestObject->process($this->container);
106+
107+
$this->assertFalse($this->container->hasDefinition('bitrix_ordinary_tools.router_cache_warmer'));
108+
}
109+
110+
/**
111+
* Нет router.
112+
*
113+
* @return void
114+
*/
115+
public function testHasRouter() : void
116+
{
117+
$this->container->setDefinition(
118+
'bitrix_ordinary_tools.router_cache_warmer',
119+
new Definition(get_class($this->fakeService))
120+
);
121+
122+
$this->container->setDefinition(
123+
'router',
124+
new Definition(get_class($this->fakeService))
125+
);
126+
127+
$this->obTestObject->process($this->container);
128+
129+
$this->assertTrue($this->container->hasDefinition('bitrix_ordinary_tools.router_cache_warmer'));
130+
}
131+
}

0 commit comments

Comments
 (0)