murph-skeleton/core/Cache/SymfonyCacheManager.php

81 lines
2.3 KiB
PHP
Raw Normal View History

2021-03-25 11:59:47 +01:00
<?php
namespace App\Core\Cache;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;
2021-05-20 13:46:58 +02:00
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2021-05-20 14:43:33 +02:00
use Symfony\Component\HttpClient\Exception\ClientException;
2021-05-24 18:41:57 +02:00
use Symfony\Component\Console\Output\OutputInterface;
2021-03-25 11:59:47 +01:00
/**
* class SymfonyCacheManager.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class SymfonyCacheManager
{
protected KernelInterface $kernel;
2021-05-20 13:46:58 +02:00
protected HttpClientInterface $httpClient;
protected UrlGeneratorInterface $urlGenerator;
2021-03-25 11:59:47 +01:00
2021-05-20 13:46:58 +02:00
public function __construct(KernelInterface $kernel, HttpClientInterface $httpClient, UrlGeneratorInterface $urlGenerator)
2021-03-25 11:59:47 +01:00
{
$this->kernel = $kernel;
2021-05-20 13:46:58 +02:00
$this->httpClient = $httpClient;
$this->urlGenerator = $urlGenerator;
2021-03-25 11:59:47 +01:00
}
public function cleanRouting()
{
$finder = new Finder();
$finder
->in($this->kernel->getCacheDir())
->depth('== 0')
->name('url_*.php*')
;
2021-05-20 13:46:58 +02:00
$pingUrl = $this->urlGenerator->generate('_ping', [], UrlGeneratorInterface::ABSOLUTE_URL);
2021-03-25 11:59:47 +01:00
foreach ($finder as $file) {
unlink((string) $file->getPathname());
}
2021-05-20 13:46:58 +02:00
2021-05-20 14:43:33 +02:00
try {
// Hack: used to regenerate cache of url generator
$this->httpClient->request('POST', $pingUrl);
} catch (ClientException $e) {
}
2021-03-25 11:59:47 +01:00
}
2021-05-24 18:41:57 +02:00
public function cleanAll(OutputInterface $output = null)
2021-03-25 11:59:47 +01:00
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
2021-05-24 18:41:57 +02:00
if (null === $output) {
$output = new BufferedOutput();
}
2021-03-25 11:59:47 +01:00
$input = new ArrayInput([
'command' => 'cache:clear',
'-e' => $this->kernel->getEnvironment(),
'--no-warmup' => null,
'--ansi' => null,
]);
$application->run($input, $output);
$input = new ArrayInput([
'command' => 'cache:warmup',
'-e' => $this->kernel->getEnvironment(),
2021-03-25 11:59:47 +01:00
]);
$application->run($input, $output);
}
}