deblan.tv/vendor/trinity/src/Trinity/Bundle/ContentManagerBundle/Command/CacheClearVarnishCommand.php
2015-03-02 21:57:49 +01:00

61 lines
1.9 KiB
PHP

<?php
namespace Trinity\Bundle\ContentManagerBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Trinity\Bundle\ContentManagerBundle\Model\NavQuery;
class CacheClearVarnishCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('cache:clear-varnish')
->setDescription('Clear varnish cache')
->addOption('host', null, InputOption::VALUE_REQUIRED, 'Clear the specified http-host')
->setHelp(<<<EOF
The <info>%command.name%</info> command clears the varnish
Option http-host with host like "local.www.sstnfc.com" will clean only this host
EOF
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('host')) {
$command = 'varnishadm -S /etc/varnish/secret -T :6082 \'ban req.http.host == "'.$input->getOption('host').'"\'';
$process = new Process($command);
$output->writeln(sprintf('<comment>Command</comment> %s', $command));
$process->run();
return true;
}
foreach (NavQuery::create()->find() as $nav) {
if (!$nav->isRegexDomain()) {
$command = 'varnishadm -S /etc/varnish/secret -T :6082 \'ban req.http.host == "'.$nav->getDomain().'"\'';
} else {
$command = 'varnishadm -S /etc/varnish/secret -T :6082 \'ban req.http.host ~ "'.$nav->getVarnishDomainRegex().'"\'';
}
$process = new Process($command);
$output->writeln(sprintf('<comment>Command</comment> %s', $command));
$process->run();
}
return true;
}
}