From 154fe050248db1545c478d967abc819e528262bd Mon Sep 17 00:00:00 2001 From: Steven worley Date: Sun, 19 Jun 2022 15:50:56 +1000 Subject: [PATCH 1/8] Add the quant tome module. --- modules/quant_tome/drush.services.yml | 6 + modules/quant_tome/quant_tome.info.yml | 9 + modules/quant_tome/quant_tome.services.yml | 12 ++ .../src/Commands/QuantTomeCommands.php | 44 +++++ modules/quant_tome/src/QuantTomeBatch.php | 177 ++++++++++++++++++ src/Plugin/QueueItem/RouteItem.php | 38 ++-- 6 files changed, 273 insertions(+), 13 deletions(-) create mode 100644 modules/quant_tome/drush.services.yml create mode 100644 modules/quant_tome/quant_tome.info.yml create mode 100644 modules/quant_tome/quant_tome.services.yml create mode 100644 modules/quant_tome/src/Commands/QuantTomeCommands.php create mode 100644 modules/quant_tome/src/QuantTomeBatch.php diff --git a/modules/quant_tome/drush.services.yml b/modules/quant_tome/drush.services.yml new file mode 100644 index 00000000..631a6c3f --- /dev/null +++ b/modules/quant_tome/drush.services.yml @@ -0,0 +1,6 @@ +services: + quant_tome.commands: + class: \Drupal\quant_tome\Commands\QuantTomeCommands + arguments: ['@quant_tome.deploy_batch'] + tags: + - { name: drush.command } diff --git a/modules/quant_tome/quant_tome.info.yml b/modules/quant_tome/quant_tome.info.yml new file mode 100644 index 00000000..c87f7aad --- /dev/null +++ b/modules/quant_tome/quant_tome.info.yml @@ -0,0 +1,9 @@ +name: 'Quant Tome' +type: module +description: Deploy tome static output to Quant +core: 8.x +core_version_requirement: ^8 || ^9 +package: Quant +dependencies: +- tome:tome_static +- quant:quant_api diff --git a/modules/quant_tome/quant_tome.services.yml b/modules/quant_tome/quant_tome.services.yml new file mode 100644 index 00000000..d7f5dc86 --- /dev/null +++ b/modules/quant_tome/quant_tome.services.yml @@ -0,0 +1,12 @@ +services: + # quant_tome.redirect_subscriber: + # class: Drupal\quant_tome\EventSubscriber\RedirectSubscriber + # arguments: ['@tome_static.generator', '@file_system'] + # tags: + # - { name: event_subscriber } + quant_tome.deploy_batch: + class: Drupal\quant_tome\QuantTomeBatch + arguments: + - '@tome_static.generator' + - '@file_system' + - '@quant_api.client' diff --git a/modules/quant_tome/src/Commands/QuantTomeCommands.php b/modules/quant_tome/src/Commands/QuantTomeCommands.php new file mode 100644 index 00000000..6373f2b3 --- /dev/null +++ b/modules/quant_tome/src/Commands/QuantTomeCommands.php @@ -0,0 +1,44 @@ +batch = $batch; + } + + /** + * Deploy a tome static build to Quant. + * + * @command quant:tome:deploy + */ + public function deploy(array $options = []) { + if (!$this->batch->checkConfig()) { + $this->io()->error('Cannot connect to the QuantAPI, please check configuration.'); + return 1; + } + if (!$this->batch->checkBuild()) { + $this->io()->error('No static build available for deploy, please run "drush tome:export.'); + return 1; + } + $batch_builder = $this->batch->getBatch(); + batch_set($batch_builder->toArray()); + $result = drush_backend_batch_process(); + } + +} diff --git a/modules/quant_tome/src/QuantTomeBatch.php b/modules/quant_tome/src/QuantTomeBatch.php new file mode 100644 index 00000000..d57531ff --- /dev/null +++ b/modules/quant_tome/src/QuantTomeBatch.php @@ -0,0 +1,177 @@ +static = $static; + $this->fileSystem = $file_system; + $this->client = $client; + } + + /** + * Check to see if quant is configured correctly. + * + * @return bool + * If we can connect to the Quant api. + */ + public function checkConfig() { + return $this->client->ping(); + } + + /** + * Determine if tome export exists. + * + * @return bool + * State of the export location. + */ + public function checkBuild() { + return file_exists($this->static->getStaticDirectory()); + } + + /** + * Generate the batch to seed tome exports. + * + * @return \Drupal\Core\Batch\BatchBuilder + * A batch builder object. + */ + public function getBatch() { + $batch_builder = new BatchBuilder(); + $files = []; + + foreach ($this->fileSystem->scanDirectory($this->static->getStaticDirectory(), '/.*/') as $file) { + $files[] = $file->uri; + } + + foreach (array_chunk($files, 10) as $chunk) { + $batch_builder->addOperation([$this, 'getHashes'], [$chunk]); + } + + $batch_builder->addOperation([$this, 'checkRequiredFiles']); + return $batch_builder; + } + + /** + * Generate hashes of the files. + * + * Generate hashes as Quant's API would for the file content. + * This will reduce the number of files that we need to seed + * in the final batch operation. + * + * @param array $files + * List of file URIs. + * @param array|\ArrayAccess &$context + * The batch context. + */ + public function getHashes(array $files, &$context) { + $file_hashes = []; + foreach ($files as $file) { + $file_hashes[$file] = md5(file_get_contents($file)); + } + + // @TODO: Quant meta look up or local? + + $context['results']['files'] = isset($context['results']['files']) ? $context['results']['files'] : []; + $context['results']['files'] = array_merge($context['results']['files'], $file_hashes); + } + + /** + * Processes the hashed records and generates the deploy batch. + * + * Takes the computed file hashes and evaluates which files + * need to be sent back to Quant. Will then create another batch + * operation to seed the data. + * + * @param array|\ArrayAccess $context + * The batch context. + */ + public function checkRequiredFiles(&$context) { + $file_hashes = $context['results']['files']; + + foreach ($file_hashes as $file_path => $hash) { + // @TODO: Quant meta check. + // unset($file_hashes[$file_path]); + } + + $batch_builder = new BatchBuilder(); + foreach ($file_hashes as $file_path => $hash) { + $item = new RouteItem([ + 'route' => $this->pathToUri($file_path), + 'uri' => $this->pathToUri($file_path), + 'file_path' => $file_path, + ]); + $batch_builder->addOperation([$this, 'deploy'], [$item]); + } + + batch_set($batch_builder->toArray()); + } + + /** + * Convert the path to a URI used by Quant. + * + * @param string $file_path + * The file path. + * + * @return string + */ + public function pathToUri($file_path) { + return str_replace($this->static->getStaticDirectory(), '', $file_path); + } + + /** + * Deploy a file to Quant. + * + * @var \Drupal\quant\Plugin\QueueItem $item + * The file item to send to Quants API. + */ + public function deploy($item, &$context) { + \Drupal::logger('quant_tome')->notice('Sending %s', [ + '%s' => $item->log(), + ]); + $item->send(); + } + +} diff --git a/src/Plugin/QueueItem/RouteItem.php b/src/Plugin/QueueItem/RouteItem.php index 820103d8..8681e1f0 100644 --- a/src/Plugin/QueueItem/RouteItem.php +++ b/src/Plugin/QueueItem/RouteItem.php @@ -19,6 +19,10 @@ class RouteItem implements QuantQueueItemInterface { */ private $route; + private $uri; + + private $file_path; + /** * {@inheritdoc} */ @@ -34,7 +38,11 @@ public function __construct(array $data = []) { $route = "/{$route}"; } - $this->route = trim($route); + $route = trim($route); + + $this->route = $route; + $this->uri = isset($data['uri']) ? $data['uri'] : strtok($route, '?'); + $this->file_path = isset($data['file_path']) ? $data['file_path'] : DRUPAL_ROOT . strtok($route, '?'); } /** @@ -43,20 +51,24 @@ public function __construct(array $data = []) { public function send() { // Wrapper for routes that resolve as files. - $ext = pathinfo(strtok($this->route, '?'), PATHINFO_EXTENSION); - - if ($ext && file_exists(DRUPAL_ROOT . strtok($this->route, '?'))) { - $file_item = new FileItem([ - 'file' => strtok($this->route, '?'), - 'url' => $this->route, - 'full_path' => DRUPAL_ROOT . $this->route, - ]); - $file_item->send(); - return; + $ext = pathinfo($this->uri, PATHINFO_EXTENSION); + $response = FALSE; + + if (file_exists($this->file_path) && !empty($ext)) { + if ($ext != 'html') { + $file_item = new FileItem([ + 'file' => $this->file_path, + 'url' => $this->uri, + ]); + $file_item->send(); + return; + } + // Synthetic response - load the content directly. + $response = [file_get_contents($this->file_path), 'text/html; charset=UTF-8']; + } else { + $response = Seed::markupFromRoute($this->route); } - $response = Seed::markupFromRoute($this->route); - if (!$response) { \Drupal::logger('quant_seed')->error("Unable to send {$this->route}"); return; From 417d89a66432bce5f5eeec7b34f296e3e62a0145 Mon Sep 17 00:00:00 2001 From: Steven worley Date: Tue, 21 Jun 2022 09:10:24 +1000 Subject: [PATCH 2/8] Add redirect and thread support. --- modules/quant_tome/quant_tome.services.yml | 11 +-- .../src/Commands/QuantTomeCommands.php | 23 ++++++- .../EventSubscriber/RedirectSubscriber.php | 68 +++++++++++++++++++ modules/quant_tome/src/QuantTomeBatch.php | 51 +++++++++++--- 4 files changed, 137 insertions(+), 16 deletions(-) create mode 100644 modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php diff --git a/modules/quant_tome/quant_tome.services.yml b/modules/quant_tome/quant_tome.services.yml index d7f5dc86..cf159ddd 100644 --- a/modules/quant_tome/quant_tome.services.yml +++ b/modules/quant_tome/quant_tome.services.yml @@ -1,12 +1,13 @@ services: - # quant_tome.redirect_subscriber: - # class: Drupal\quant_tome\EventSubscriber\RedirectSubscriber - # arguments: ['@tome_static.generator', '@file_system'] - # tags: - # - { name: event_subscriber } + quant_tome.redirect_subscriber: + class: Drupal\quant_tome\EventSubscriber\RedirectSubscriber + arguments: ['@tome_static.generator', '@file_system'] + tags: + - { name: event_subscriber } quant_tome.deploy_batch: class: Drupal\quant_tome\QuantTomeBatch arguments: - '@tome_static.generator' - '@file_system' - '@quant_api.client' + - '@queue' diff --git a/modules/quant_tome/src/Commands/QuantTomeCommands.php b/modules/quant_tome/src/Commands/QuantTomeCommands.php index 6373f2b3..c8ad3ff6 100644 --- a/modules/quant_tome/src/Commands/QuantTomeCommands.php +++ b/modules/quant_tome/src/Commands/QuantTomeCommands.php @@ -4,12 +4,18 @@ use Drush\Commands\DrushCommands; use Drupal\quant_tome\QuantTomeBatch; +use Drupal\quant\Commands\QuantDrushCommands; /** * Contains the quant:tome:deploy command. */ class QuantTomeCommands extends DrushCommands { + /** + * The batch builder. + * + * @var \Drupal\quant_tome\QuantTomeBatch + */ protected $batch; /** @@ -27,18 +33,31 @@ public function __construct(QuantTomeBatch $batch) { * * @command quant:tome:deploy */ - public function deploy(array $options = []) { + public function deploy(array $options = ['threads' => 5]) { + $this->io()->writeln('Preparing Tome output for Quant...'); + if (!$this->batch->checkConfig()) { $this->io()->error('Cannot connect to the QuantAPI, please check configuration.'); return 1; } if (!$this->batch->checkBuild()) { - $this->io()->error('No static build available for deploy, please run "drush tome:export.'); + $this->io()->error('No static build available for deploy, please run "drush tome:static"'); return 1; } + $batch_builder = $this->batch->getBatch(); batch_set($batch_builder->toArray()); + $result = drush_backend_batch_process(); + + if (!empty($result['object'][0]['errors'])) { + $this->io()->error('Deploy failed! Consult the error log for more information'); + return 1; + } + + // Process the queue after the batch has collected it. + $quant_drush = new QuantDrushCommands(); + $quant_drush->message(['threads' => $options['threads']]); } } diff --git a/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php b/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php new file mode 100644 index 00000000..6002aeaa --- /dev/null +++ b/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php @@ -0,0 +1,68 @@ +staticGenerator = $static_generator; + $this->fileSystem = $file_system; + } + + /** + * Reacts to a response event. + * + * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event + * The event. + */ + public function onResponse(FilterResponseEvent $event) { + $response = $event->getResponse(); + $request = $event->getRequest(); + if ($request->attributes->has(StaticGeneratorInterface::REQUEST_KEY) && $response instanceof RedirectResponse) { + $base_dir = $this->staticGenerator->getStaticDirectory(); + $this->fileSystem->prepareDirectory($base_dir, FileSystemInterface::CREATE_DIRECTORY); + file_put_contents("$base_dir/_redirects", $request->getPathInfo() . ' ' . $response->getTargetUrl() . "\n", FILE_APPEND); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[KernelEvents::RESPONSE][] = ['onResponse']; + return $events; + } +} diff --git a/modules/quant_tome/src/QuantTomeBatch.php b/modules/quant_tome/src/QuantTomeBatch.php index d57531ff..9490b8d8 100644 --- a/modules/quant_tome/src/QuantTomeBatch.php +++ b/modules/quant_tome/src/QuantTomeBatch.php @@ -9,6 +9,8 @@ use Drupal\Core\File\FileSystemInterface; use Drupal\quant\Plugin\QueueItem\RouteItem; use Drupal\quant_api\Client\QuantClient; +use Drupal\Core\Queue\QueueFactory; +use Drupal\quant\Plugin\QueueItem\RedirectItem; class QuantTomeBatch { @@ -36,6 +38,13 @@ class QuantTomeBatch { */ protected $client; + /** + * The queue factory. + * + * @var \Drupal\Core\Queue\QueueFactory + */ + protected $queueFactory; + /** * Constructor. * @@ -46,10 +55,11 @@ class QuantTomeBatch { * @param \Drupal\quant_api\Client\QuantClient $client * The Quant api client. */ - public function __construct(StaticGeneratorInterface $static, FileSystemInterface $file_system, QuantClient $client) { + public function __construct(StaticGeneratorInterface $static, FileSystemInterface $file_system, QuantClient $client, QueueFactory $queue_factory) { $this->static = $static; $this->fileSystem = $file_system; $this->client = $client; + $this->queueFactory = $queue_factory; } /** @@ -131,22 +141,37 @@ public function getHashes(array $files, &$context) { public function checkRequiredFiles(&$context) { $file_hashes = $context['results']['files']; - foreach ($file_hashes as $file_path => $hash) { - // @TODO: Quant meta check. - // unset($file_hashes[$file_path]); - } + $queue = $this->queueFactory->get('quant_seed_worker'); + $queue->deleteQueue(); - $batch_builder = new BatchBuilder(); foreach ($file_hashes as $file_path => $hash) { + if (strpos($file_path, 'redirect') > -1) { + if ($handle = fopen($file_path, 'r')) { + while (!feof($handle)) { + $line = fgets($handle); + $redirect = explode(' ', $line); + if (empty($redirect[0])) { + break; + } + $queue->createItem(new RedirectItem([ + 'source' => trim($redirect[0]), + 'destination' => trim($redirect[1]), + 'status_code' => 301, + ])); + } + } + fclose($handle); + continue; + } + $item = new RouteItem([ 'route' => $this->pathToUri($file_path), 'uri' => $this->pathToUri($file_path), 'file_path' => $file_path, ]); - $batch_builder->addOperation([$this, 'deploy'], [$item]); - } - batch_set($batch_builder->toArray()); + $queue->createItem($item); + } } /** @@ -174,4 +199,12 @@ public function deploy($item, &$context) { $item->send(); } + public function finish($success, &$context) { + if ($success) { + \Drupal::logger('quant_tome')->info('Complete!'); + } else { + \Drupal::logger('quant_tome')->error('Failed to deploy all files, check the logs!'); + } + } + } From 8f1895acd030f976a6d95a09c7288c0a203480c2 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Wed, 21 Sep 2022 18:42:05 -0700 Subject: [PATCH 3/8] Strip /index.html from Tome URI to match Quant seeding. --- modules/quant_tome/src/QuantTomeBatch.php | 10 +++++++--- src/Plugin/QueueItem/RouteItem.php | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/quant_tome/src/QuantTomeBatch.php b/modules/quant_tome/src/QuantTomeBatch.php index 9490b8d8..2fe58c1d 100644 --- a/modules/quant_tome/src/QuantTomeBatch.php +++ b/modules/quant_tome/src/QuantTomeBatch.php @@ -164,9 +164,10 @@ public function checkRequiredFiles(&$context) { continue; } + $uri = $this->pathToUri($file_path); $item = new RouteItem([ - 'route' => $this->pathToUri($file_path), - 'uri' => $this->pathToUri($file_path), + 'route' => $uri, + 'uri' => $uri, 'file_path' => $file_path, ]); @@ -183,7 +184,10 @@ public function checkRequiredFiles(&$context) { * @return string */ public function pathToUri($file_path) { - return str_replace($this->static->getStaticDirectory(), '', $file_path); + // Strip directory and index.html from paths to match regular Quant processing. + $uri = str_replace($this->static->getStaticDirectory(), '', $file_path); + $uri = str_replace('/index.html', '', $uri); + return $uri; } /** diff --git a/src/Plugin/QueueItem/RouteItem.php b/src/Plugin/QueueItem/RouteItem.php index 8681e1f0..0db97ae8 100644 --- a/src/Plugin/QueueItem/RouteItem.php +++ b/src/Plugin/QueueItem/RouteItem.php @@ -51,7 +51,7 @@ public function __construct(array $data = []) { public function send() { // Wrapper for routes that resolve as files. - $ext = pathinfo($this->uri, PATHINFO_EXTENSION); + $ext = pathinfo($this->file_path, PATHINFO_EXTENSION); $response = FALSE; if (file_exists($this->file_path) && !empty($ext)) { From 201309cba1a8bcd263bc76706172284799be789f Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Wed, 21 Sep 2022 19:30:31 -0700 Subject: [PATCH 4/8] Minor code style and comment updates. --- modules/quant_tome/quant_tome.info.yml | 2 +- .../src/Commands/QuantTomeCommands.php | 10 ++--- .../EventSubscriber/RedirectSubscriber.php | 1 - modules/quant_tome/src/QuantTomeBatch.php | 42 +++++++++++-------- src/Plugin/QueueItem/RouteItem.php | 22 +++++++--- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/modules/quant_tome/quant_tome.info.yml b/modules/quant_tome/quant_tome.info.yml index c87f7aad..7e8df163 100644 --- a/modules/quant_tome/quant_tome.info.yml +++ b/modules/quant_tome/quant_tome.info.yml @@ -1,6 +1,6 @@ name: 'Quant Tome' type: module -description: Deploy tome static output to Quant +description: 'Deploy Tome static output to Quant.' core: 8.x core_version_requirement: ^8 || ^9 package: Quant diff --git a/modules/quant_tome/src/Commands/QuantTomeCommands.php b/modules/quant_tome/src/Commands/QuantTomeCommands.php index c8ad3ff6..7e545592 100644 --- a/modules/quant_tome/src/Commands/QuantTomeCommands.php +++ b/modules/quant_tome/src/Commands/QuantTomeCommands.php @@ -3,8 +3,8 @@ namespace Drupal\quant_tome\Commands; use Drush\Commands\DrushCommands; -use Drupal\quant_tome\QuantTomeBatch; use Drupal\quant\Commands\QuantDrushCommands; +use Drupal\quant_tome\QuantTomeBatch; /** * Contains the quant:tome:deploy command. @@ -29,7 +29,7 @@ public function __construct(QuantTomeBatch $batch) { } /** - * Deploy a tome static build to Quant. + * Deploy a Tome static build to Quant. * * @command quant:tome:deploy */ @@ -37,11 +37,11 @@ public function deploy(array $options = ['threads' => 5]) { $this->io()->writeln('Preparing Tome output for Quant...'); if (!$this->batch->checkConfig()) { - $this->io()->error('Cannot connect to the QuantAPI, please check configuration.'); + $this->io()->error('Cannot connect to the Quant API. Please check the Quant configuration.'); return 1; } if (!$this->batch->checkBuild()) { - $this->io()->error('No static build available for deploy, please run "drush tome:static"'); + $this->io()->error('No Tome static build is available. Please run "drush tome:static".'); return 1; } @@ -51,7 +51,7 @@ public function deploy(array $options = ['threads' => 5]) { $result = drush_backend_batch_process(); if (!empty($result['object'][0]['errors'])) { - $this->io()->error('Deploy failed! Consult the error log for more information'); + $this->io()->error('Deploy failed. Please consult the error log for more information.'); return 1; } diff --git a/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php b/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php index 6002aeaa..7c5e5de0 100644 --- a/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php +++ b/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php @@ -1,6 +1,5 @@ static = $static; @@ -63,17 +63,17 @@ public function __construct(StaticGeneratorInterface $static, FileSystemInterfac } /** - * Check to see if quant is configured correctly. + * Check to see if Quant is configured correctly. * * @return bool - * If we can connect to the Quant api. + * If we can connect to the Quant API. */ public function checkConfig() { return $this->client->ping(); } /** - * Determine if tome export exists. + * Determine if Tome export exists. * * @return bool * State of the export location. @@ -83,7 +83,7 @@ public function checkBuild() { } /** - * Generate the batch to seed tome exports. + * Generate the batch to seed Tome exports. * * @return \Drupal\Core\Batch\BatchBuilder * A batch builder object. @@ -107,9 +107,8 @@ public function getBatch() { /** * Generate hashes of the files. * - * Generate hashes as Quant's API would for the file content. - * This will reduce the number of files that we need to seed - * in the final batch operation. + * Generate hashes as Quant's API would for the file content. This will reduce + * the number of files that we need to seed in the final batch operation. * * @param array $files * List of file URIs. @@ -131,9 +130,8 @@ public function getHashes(array $files, &$context) { /** * Processes the hashed records and generates the deploy batch. * - * Takes the computed file hashes and evaluates which files - * need to be sent back to Quant. Will then create another batch - * operation to seed the data. + * Takes the computed file hashes and evaluates which files need to be sent + * to Quant. Then, it creates another batch operation to seed the data. * * @param array|\ArrayAccess $context * The batch context. @@ -184,7 +182,7 @@ public function checkRequiredFiles(&$context) { * @return string */ public function pathToUri($file_path) { - // Strip directory and index.html from paths to match regular Quant processing. + // Strip directory and index.html to match regular Quant processing. $uri = str_replace($this->static->getStaticDirectory(), '', $file_path); $uri = str_replace('/index.html', '', $uri); return $uri; @@ -194,7 +192,7 @@ public function pathToUri($file_path) { * Deploy a file to Quant. * * @var \Drupal\quant\Plugin\QueueItem $item - * The file item to send to Quants API. + * The file item to send to Quant API. */ public function deploy($item, &$context) { \Drupal::logger('quant_tome')->notice('Sending %s', [ @@ -203,6 +201,14 @@ public function deploy($item, &$context) { $item->send(); } + /** + * Finish deploy process. + * + * @param bool $success + * TRUE if batch successfully completed. + * @param array $context + * Batch context. + */ public function finish($success, &$context) { if ($success) { \Drupal::logger('quant_tome')->info('Complete!'); diff --git a/src/Plugin/QueueItem/RouteItem.php b/src/Plugin/QueueItem/RouteItem.php index 0db97ae8..450a4f6a 100644 --- a/src/Plugin/QueueItem/RouteItem.php +++ b/src/Plugin/QueueItem/RouteItem.php @@ -19,8 +19,18 @@ class RouteItem implements QuantQueueItemInterface { */ private $route; + /** + * URI for the file. + * + * @var string + */ private $uri; + /** + * Path to the file. + * + * @var string + */ private $file_path; /** @@ -33,11 +43,10 @@ public function __construct(array $data = []) { throw new \UnexpectedValueException(self::class . ' requires a string value.'); } - // Ensure route starts with a slash. + // Ensure route starts with a slash and has no empty spaces. if (substr($route, 0, 1) != '/') { $route = "/{$route}"; } - $route = trim($route); $this->route = $route; @@ -51,11 +60,11 @@ public function __construct(array $data = []) { public function send() { // Wrapper for routes that resolve as files. - $ext = pathinfo($this->file_path, PATHINFO_EXTENSION); + $extension = pathinfo($this->file_path, PATHINFO_EXTENSION); $response = FALSE; - if (file_exists($this->file_path) && !empty($ext)) { - if ($ext != 'html') { + if (file_exists($this->file_path) && !empty($extension)) { + if ($extension != 'html') { $file_item = new FileItem([ 'file' => $this->file_path, 'url' => $this->uri, @@ -65,7 +74,8 @@ public function send() { } // Synthetic response - load the content directly. $response = [file_get_contents($this->file_path), 'text/html; charset=UTF-8']; - } else { + } + else { $response = Seed::markupFromRoute($this->route); } From 0408d0a02d326e2fa5e795da5e62f06e536f01ed Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Wed, 21 Sep 2022 20:05:51 -0700 Subject: [PATCH 5/8] Minor linting updates. --- .../src/Commands/QuantTomeCommands.php | 2 +- .../src/EventSubscriber/RedirectSubscriber.php | 1 + modules/quant_tome/src/QuantTomeBatch.php | 17 ++++++++++++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/modules/quant_tome/src/Commands/QuantTomeCommands.php b/modules/quant_tome/src/Commands/QuantTomeCommands.php index 7e545592..3355f483 100644 --- a/modules/quant_tome/src/Commands/QuantTomeCommands.php +++ b/modules/quant_tome/src/Commands/QuantTomeCommands.php @@ -21,7 +21,7 @@ class QuantTomeCommands extends DrushCommands { /** * QuantTomeCommands constructor. * - * @param \Drupal\quant_tome\QuantTomeBatch; + * @param \Drupal\quant_tome\QuantTomeBatch $batch * The batch service. */ public function __construct(QuantTomeBatch $batch) { diff --git a/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php b/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php index 7c5e5de0..b6e08286 100644 --- a/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php +++ b/modules/quant_tome/src/EventSubscriber/RedirectSubscriber.php @@ -64,4 +64,5 @@ public static function getSubscribedEvents() { $events[KernelEvents::RESPONSE][] = ['onResponse']; return $events; } + } diff --git a/modules/quant_tome/src/QuantTomeBatch.php b/modules/quant_tome/src/QuantTomeBatch.php index 7352bf56..5a8efa28 100644 --- a/modules/quant_tome/src/QuantTomeBatch.php +++ b/modules/quant_tome/src/QuantTomeBatch.php @@ -12,6 +12,9 @@ use Drupal\tome_base\PathTrait; use Drupal\tome_static\StaticGeneratorInterface; +/** + * Batch process for Tome static content. + */ class QuantTomeBatch { use PathTrait; @@ -54,6 +57,8 @@ class QuantTomeBatch { * The file system interface. * @param \Drupal\quant_api\Client\QuantClient $client * The Quant API client. + * @param \Drupal\Core\Queue\QueueFactory $queue_factory + * The queue factory. */ public function __construct(StaticGeneratorInterface $static, FileSystemInterface $file_system, QuantClient $client, QueueFactory $queue_factory) { $this->static = $static; @@ -110,6 +115,8 @@ public function getBatch() { * Generate hashes as Quant's API would for the file content. This will reduce * the number of files that we need to seed in the final batch operation. * + * @todo Quant meta look up or local? + * * @param array $files * List of file URIs. * @param array|\ArrayAccess &$context @@ -121,8 +128,6 @@ public function getHashes(array $files, &$context) { $file_hashes[$file] = md5(file_get_contents($file)); } - // @TODO: Quant meta look up or local? - $context['results']['files'] = isset($context['results']['files']) ? $context['results']['files'] : []; $context['results']['files'] = array_merge($context['results']['files'], $file_hashes); } @@ -180,6 +185,7 @@ public function checkRequiredFiles(&$context) { * The file path. * * @return string + * URI based on file path. */ public function pathToUri($file_path) { // Strip directory and index.html to match regular Quant processing. @@ -194,7 +200,7 @@ public function pathToUri($file_path) { * @var \Drupal\quant\Plugin\QueueItem $item * The file item to send to Quant API. */ - public function deploy($item, &$context) { + public function deploy($item, array &$context) { \Drupal::logger('quant_tome')->notice('Sending %s', [ '%s' => $item->log(), ]); @@ -209,10 +215,11 @@ public function deploy($item, &$context) { * @param array $context * Batch context. */ - public function finish($success, &$context) { + public function finish($success, array &$context) { if ($success) { \Drupal::logger('quant_tome')->info('Complete!'); - } else { + } + else { \Drupal::logger('quant_tome')->error('Failed to deploy all files, check the logs!'); } } From 96ca1e6e046554bbe9aa1226830fac128ccb8d59 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Wed, 21 Sep 2022 20:08:54 -0700 Subject: [PATCH 6/8] Add the rest of linting fixes. --- src/Plugin/QueueItem/RouteItem.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Plugin/QueueItem/RouteItem.php b/src/Plugin/QueueItem/RouteItem.php index 450a4f6a..226df7e1 100644 --- a/src/Plugin/QueueItem/RouteItem.php +++ b/src/Plugin/QueueItem/RouteItem.php @@ -31,7 +31,7 @@ class RouteItem implements QuantQueueItemInterface { * * @var string */ - private $file_path; + private $filePath; /** * {@inheritdoc} @@ -51,7 +51,7 @@ public function __construct(array $data = []) { $this->route = $route; $this->uri = isset($data['uri']) ? $data['uri'] : strtok($route, '?'); - $this->file_path = isset($data['file_path']) ? $data['file_path'] : DRUPAL_ROOT . strtok($route, '?'); + $this->filePath = isset($data['file_path']) ? $data['file_path'] : DRUPAL_ROOT . strtok($route, '?'); } /** @@ -60,20 +60,23 @@ public function __construct(array $data = []) { public function send() { // Wrapper for routes that resolve as files. - $extension = pathinfo($this->file_path, PATHINFO_EXTENSION); + $extension = pathinfo($this->filePath, PATHINFO_EXTENSION); $response = FALSE; - if (file_exists($this->file_path) && !empty($extension)) { + if (file_exists($this->filePath) && !empty($extension)) { if ($extension != 'html') { $file_item = new FileItem([ - 'file' => $this->file_path, + 'file' => $this->filePath, 'url' => $this->uri, ]); $file_item->send(); return; } - // Synthetic response - load the content directly. - $response = [file_get_contents($this->file_path), 'text/html; charset=UTF-8']; + // Get the content from the file. + $response = [ + file_get_contents($this->filePath), + 'text/html; charset=UTF-8' + ]; } else { $response = Seed::markupFromRoute($this->route); From a5d2efd9868e94a8292bdfd2e5a24ea379652b61 Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Wed, 21 Sep 2022 20:10:44 -0700 Subject: [PATCH 7/8] Final linting fix. --- src/Plugin/QueueItem/RouteItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/QueueItem/RouteItem.php b/src/Plugin/QueueItem/RouteItem.php index 226df7e1..56e8ad71 100644 --- a/src/Plugin/QueueItem/RouteItem.php +++ b/src/Plugin/QueueItem/RouteItem.php @@ -75,7 +75,7 @@ public function send() { // Get the content from the file. $response = [ file_get_contents($this->filePath), - 'text/html; charset=UTF-8' + 'text/html; charset=UTF-8', ]; } else { From c63da40ea3997f1d97d3e221e31b96bcaee76b6b Mon Sep 17 00:00:00 2001 From: Kristen Pol Date: Wed, 21 Sep 2022 21:13:50 -0700 Subject: [PATCH 8/8] Use URI rather than URL for redirect destination. --- modules/quant_tome/src/QuantTomeBatch.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/quant_tome/src/QuantTomeBatch.php b/modules/quant_tome/src/QuantTomeBatch.php index 5a8efa28..e439c2b4 100644 --- a/modules/quant_tome/src/QuantTomeBatch.php +++ b/modules/quant_tome/src/QuantTomeBatch.php @@ -153,12 +153,15 @@ public function checkRequiredFiles(&$context) { while (!feof($handle)) { $line = fgets($handle); $redirect = explode(' ', $line); - if (empty($redirect[0])) { + $source = trim($redirect[0]); + if (empty($source)) { break; } + // Only use the destination URI. + $destination = parse_url(trim($redirect[1]), PHP_URL_PATH); $queue->createItem(new RedirectItem([ - 'source' => trim($redirect[0]), - 'destination' => trim($redirect[1]), + 'source' => $source, + 'destination' => $destination, 'status_code' => 301, ])); } @@ -179,13 +182,13 @@ public function checkRequiredFiles(&$context) { } /** - * Convert the path to a URI used by Quant. + * Convert the path to a URI. * * @param string $file_path * The file path. * * @return string - * URI based on file path. + * URI based on the file path. */ public function pathToUri($file_path) { // Strip directory and index.html to match regular Quant processing.