InvalidateCommand.php in Purge 8.3
File
modules/purge_drush/src/Commands/InvalidateCommand.php
View source
<?php
namespace Drupal\purge_drush\Commands;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\purge\Plugin\Purge\Invalidation\Exception\InvalidExpressionException;
use Drupal\purge\Plugin\Purge\Invalidation\Exception\MissingExpressionException;
use Drupal\purge\Plugin\Purge\Invalidation\Exception\TypeUnsupportedException;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationsServiceInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvStatesInterface;
use Drupal\purge\Plugin\Purge\Processor\ProcessorsServiceInterface;
use Drupal\purge\Plugin\Purge\Purger\Exception\CapacityException;
use Drupal\purge\Plugin\Purge\Purger\Exception\DiagnosticsException;
use Drupal\purge\Plugin\Purge\Purger\Exception\LockException;
use Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;
class InvalidateCommand extends DrushCommands {
protected $purgeProcessors;
protected $purgePurgers;
protected $purgeInvalidationFactory;
public function __construct(InvalidationsServiceInterface $purge_invalidation_factory, ProcessorsServiceInterface $purge_processors, PurgersServiceInterface $purge_purgers) {
parent::__construct();
$this->purgeInvalidationFactory = $purge_invalidation_factory;
$this->purgeProcessors = $purge_processors;
$this->purgePurgers = $purge_purgers;
}
public function invalidate($type, $expression = NULL, array $options = [
'format' => 'string',
]) {
if (!($processor = $this->purgeProcessors
->get('drush_purge_invalidate'))) {
throw new \Exception(dt("Please add the required processor:\ndrush p:processor-add drush_purge_invalidate"));
}
try {
$invalidations = [
$this->purgeInvalidationFactory
->get($type, $expression),
];
} catch (PluginNotFoundException $e) {
throw new \Exception(dt("Type '@type' does not exist, see 'drush p:types' for available types.", [
'@type' => $type,
]));
} catch (InvalidExpressionException $e) {
throw new \Exception($e
->getMessage());
} catch (TypeUnsupportedException $e) {
throw new \Exception(dt("There is no purger supporting '@type', please install one!", [
'@type' => $type,
]));
} catch (MissingExpressionException $e) {
throw new \Exception($e
->getMessage());
}
if ($options['format'] === 'string' && $type === 'everything') {
$this
->io()
->caution(dt("Invalidating everything will mass-clear potentially thousands" . " of pages, which could temporarily make your site really slow as" . " external caches will have to warm up again.\n"));
if (!$this
->io()
->confirm(dt("Are you really sure?"))) {
throw new UserAbortException();
}
}
try {
$this->purgePurgers
->invalidate($processor, $invalidations);
} catch (DiagnosticsException $e) {
throw new \Exception($e
->getMessage());
} catch (CapacityException $e) {
throw new \Exception($e
->getMessage());
} catch (LockException $e) {
throw new \Exception($e
->getMessage());
}
if ($invalidations[0]
->getState() === InvStatesInterface::SUCCEEDED) {
if ($options['format'] === 'string') {
$this
->io()
->success(dt('Item invalidated successfully!'));
}
}
else {
throw new \Exception(dt('Invalidation failed, return state is: @state.', [
'@state' => $invalidations[0]
->getStateString(),
]));
}
}
public function rebuildExternal(array $options = [
'format' => 'string',
]) {
return $this
->invalidate('everything', NULL, $options);
}
}