View source
<?php
namespace Drupal\purge_ui\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
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 Drupal\purge\Plugin\Purge\Queue\QueueServiceInterface;
use Drupal\purge\Plugin\Purge\Queuer\QueuersServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PurgeBlockForm extends FormBase {
protected $config;
protected $messenger;
protected $processor;
protected $queuer;
protected $purgePurgers;
protected $purgeInvalidationFactory;
protected $purgeQueue;
public final function __construct(array $config, MessengerInterface $messenger, ProcessorsServiceInterface $purge_processors, PurgersServiceInterface $purge_purgers, InvalidationsServiceInterface $purge_invalidation_factory, QueueServiceInterface $purge_queue, QueuersServiceInterface $purge_queuers) {
if (is_null($config)) {
throw new \LogicException('\\Drupal\\purge_ui\\Form\\PurgeBlockForm should be directly instantiated with block configuration passed in.');
}
$this->config = $config;
$this->messenger = $messenger;
$this->processor = $purge_processors
->get('purge_ui_block_processor');
$this->queuer = $purge_queuers
->get('purge_ui_block_queuer');
$this->purgePurgers = $purge_purgers;
$this->purgeInvalidationFactory = $purge_invalidation_factory;
$this->purgeQueue = $purge_queue;
}
public static function create(ContainerInterface $container, array $config = NULL) {
return new static($config, $container
->get('messenger'), $container
->get('purge.processors'), $container
->get('purge.purgers'), $container
->get('purge.invalidation.factory'), $container
->get('purge.queue'), $container
->get('purge.queuers'));
}
protected function gatherInvalidationsData() {
$request = $this
->getRequest();
$expressions = [];
switch ($this->config['type']) {
case 'url':
$expressions[] = $request
->getUriForPath($request
->getRequestUri());
$expressions[] = $request
->getUri();
$expressions[] = str_replace('?' . $request
->getQueryString(), '', $expressions[1]);
break;
case 'path':
$expressions[] = ltrim($request
->getRequestUri(), '/');
$expressions[] = explode('?', $expressions[0])[0];
break;
case 'everything':
$expressions[] = NULL;
break;
}
return array_unique($expressions);
}
public function getFormId() {
return 'purge_ui.purge_' . $this->config['purge_block_id'];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form_state
->addBuildInfo('expressions', $this
->gatherInvalidationsData());
$form = [];
if ($this->config['execution'] === 'direct' && !$this->processor) {
$this->messenger
->addError($this
->t('Please contact your site administrator to enable the block processor plugin.'));
return [
'messages' => [
'#type' => 'status_messages',
],
];
}
if ($this->config['execution'] === 'queue' && !$this->queuer) {
$this->messenger
->addError($this
->t('Please contact your site administrator to enable the block queuer plugin.'));
return [
'messages' => [
'#type' => 'status_messages',
],
];
}
if (!empty($this->config['description'])) {
$form['description'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->config['description'],
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->config['submit_label'],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (empty($form_state
->getBuildInfo()['expressions'])) {
$form_state
->setErrorByName('submit', $this
->t('Invalid form submission.'));
}
if ($this->config['execution'] === 'direct' && !$this->processor) {
$form_state
->setErrorByName('submit', $this
->t('Please contact your site administrator to enable the block processor plugin.'));
}
if ($this->config['execution'] === 'queue' && !$this->queuer) {
$form_state
->setErrorByName('submit', $this
->t('Please contact your site administrator to enable the block queuer plugin.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$invalidations = [];
foreach ($form_state
->getBuildInfo()['expressions'] as $expression) {
try {
$invalidations[] = $this->purgeInvalidationFactory
->get($this->config['type'], $expression);
} catch (TypeUnsupportedException $e) {
$this->messenger
->addError($this
->t("No purger supports the type '@type', please install one!", [
'@type' => $this->config['type'],
]));
return;
}
}
if ($this->config['execution'] === 'queue') {
$this->purgeQueue
->add($this->queuer, $invalidations);
$this->messenger
->addStatus($this
->t('Please wait for the queue to be processed!'));
}
else {
try {
$this->purgePurgers
->invalidate($this->processor, $invalidations);
foreach ($invalidations as $invalidation) {
$object = $invalidation
->getType();
if (!is_null($invalidation
->getExpression())) {
$object = $this
->t('@object with expression "@expr"', [
'@object' => $invalidation
->getType(),
'@expr' => (string) $invalidation
->getExpression(),
]);
}
if ($invalidation
->getState() === InvStatesInterface::SUCCEEDED) {
$this->messenger
->addMessage($this
->t('Succesfully cleared @object.', [
'@object' => $object,
]));
}
elseif ($invalidation
->getState() === InvStatesInterface::PROCESSING) {
$this->messenger
->addWarning($this
->t('Requested to clear multistep object of type @object!', [
'@object' => $object,
]));
}
else {
$this->messenger
->addError($this
->t('Failed to clear @object!', [
'@object' => $object,
]));
}
}
} catch (DiagnosticsException $e) {
$this->messenger
->addError($e
->getMessage());
} catch (CapacityException $e) {
$this->messenger
->addError($e
->getMessage());
} catch (LockException $e) {
$this->messenger
->addError($e
->getMessage());
}
}
}
}