2021-03-29 13:58:05 +02: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:47:50 +02:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2021-05-20 14:43:58 +02:00
|
|
|
use Symfony\Component\HttpClient\Exception\ClientException;
|
2021-05-24 18:45:23 +02:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2021-05-30 17:57:56 +02:00
|
|
|
use Symfony\Component\HttpClient\Exception\TransportException;
|
2021-03-29 13:58:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* class SymfonyCacheManager.
|
|
|
|
*
|
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class SymfonyCacheManager
|
|
|
|
{
|
|
|
|
protected KernelInterface $kernel;
|
2021-05-20 13:47:50 +02:00
|
|
|
protected HttpClientInterface $httpClient;
|
|
|
|
protected UrlGeneratorInterface $urlGenerator;
|
2021-03-29 13:58:05 +02:00
|
|
|
|
2021-05-20 13:47:50 +02:00
|
|
|
public function __construct(KernelInterface $kernel, HttpClientInterface $httpClient, UrlGeneratorInterface $urlGenerator)
|
2021-03-29 13:58:05 +02:00
|
|
|
{
|
|
|
|
$this->kernel = $kernel;
|
2021-05-20 13:47:50 +02:00
|
|
|
$this->httpClient = $httpClient;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cleanRouting()
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder
|
|
|
|
->in($this->kernel->getCacheDir())
|
|
|
|
->depth('== 0')
|
|
|
|
->name('url_*.php*')
|
|
|
|
;
|
|
|
|
|
2021-05-20 13:47:50 +02:00
|
|
|
$pingUrl = $this->urlGenerator->generate('_ping', [], UrlGeneratorInterface::ABSOLUTE_URL);
|
|
|
|
|
2021-03-29 13:58:05 +02:00
|
|
|
foreach ($finder as $file) {
|
|
|
|
unlink((string) $file->getPathname());
|
|
|
|
}
|
2021-05-20 13:47:50 +02:00
|
|
|
|
2021-05-20 14:43:58 +02:00
|
|
|
try {
|
|
|
|
// Hack: used to regenerate cache of url generator
|
|
|
|
$this->httpClient->request('POST', $pingUrl);
|
|
|
|
} catch (ClientException $e) {
|
2021-05-30 17:57:56 +02:00
|
|
|
} catch (TransportException $e) {
|
2021-05-20 14:43:58 +02:00
|
|
|
}
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:45:23 +02:00
|
|
|
public function cleanAll(OutputInterface $output = null)
|
2021-03-29 13:58:05 +02:00
|
|
|
{
|
|
|
|
$application = new Application($this->kernel);
|
|
|
|
$application->setAutoExit(false);
|
2021-05-24 18:45:23 +02:00
|
|
|
|
|
|
|
if (null === $output) {
|
|
|
|
$output = new BufferedOutput();
|
|
|
|
}
|
2021-03-29 13:58:05 +02:00
|
|
|
|
|
|
|
$input = new ArrayInput([
|
|
|
|
'command' => 'cache:clear',
|
2021-04-04 20:09:29 +02:00
|
|
|
'-e' => $this->kernel->getEnvironment(),
|
|
|
|
'--no-warmup' => null,
|
2021-05-24 20:26:14 +02:00
|
|
|
'--ansi' => null,
|
2021-04-04 20:09:29 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$application->run($input, $output);
|
|
|
|
|
|
|
|
$input = new ArrayInput([
|
|
|
|
'command' => 'cache:warmup',
|
|
|
|
'-e' => $this->kernel->getEnvironment(),
|
2021-03-29 13:58:05 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$application->run($input, $output);
|
|
|
|
}
|
|
|
|
}
|