View source
<?php
namespace Drupal\tome_static_cron\Plugin\QueueWorker;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\State\StateInterface;
use Drupal\tome_static\RequestPreparer;
use Drupal\tome_static\StaticGeneratorInterface;
use Drupal\tome_static\TomeStaticHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class TomeStaticQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
const STATE_KEY_INVOKE_PATHS = 'tome_static_cron.invoke_paths';
const STATE_KEY_OLD_PATHS = 'tome_static_cron.old_paths';
protected $static;
protected $requestPreparer;
protected $currentRequest;
protected $state;
protected $logger;
public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticGeneratorInterface $static, RequestPreparer $request_preparer, RequestStack $request_stack, StateInterface $state, LoggerChannelFactoryInterface $logger_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->static = $static;
$this->requestPreparer = $request_preparer;
$this->currentRequest = $request_stack
->getCurrentRequest();
$this->state = $state;
$this->logger = $logger_factory
->get('tome_static_cron');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('tome_static.generator'), $container
->get('tome_static.request_preparer'), $container
->get('request_stack'), $container
->get('state'), $container
->get('logger.factory'));
}
public function processItem($data) {
$base_url = $data['base_url'];
$state_invoke_paths = $this->state
->get(self::STATE_KEY_INVOKE_PATHS, []);
if (isset($data['action']) && $data['action'] === 'process_invoke_paths') {
$state_invoke_paths = $this->static
->exportPaths($state_invoke_paths);
_tome_static_cron_queue_paths($state_invoke_paths, $base_url);
$this->state
->set(TomeStaticQueueWorker::STATE_KEY_INVOKE_PATHS, []);
return;
}
$path = $data['path'];
if ($this->currentRequest) {
$this->static
->prepareStaticDirectory();
$original_params = TomeStaticHelper::setBaseUrl($this->currentRequest, $base_url);
}
$this->requestPreparer
->prepareForRequest();
try {
$invoke_paths = $this->static
->requestPath($path);
} catch (\Exception $e) {
$this->logger
->error('Error when requesting path "' . $path . '"": ' . $e
->getMessage() . ' ' . $e
->getTraceAsString(), []);
$invoke_paths = [];
}
$this->state
->set(self::STATE_KEY_INVOKE_PATHS, array_merge($state_invoke_paths, $invoke_paths));
if ($this->currentRequest) {
TomeStaticHelper::restoreBaseUrl($this->currentRequest, $original_params);
}
}
}