Skip to content

Commit

Permalink
Merge pull request #235 from stooit/feature/cron
Browse files Browse the repository at this point in the history
D8/9 cron support
  • Loading branch information
stooit authored Oct 6, 2020
2 parents ffe6c63 + b8be20b commit 0cd37c5
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 4 deletions.
2 changes: 1 addition & 1 deletion modules/quant_api/quant_api.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ quant_api.settings_form:
_form: '\Drupal\quant_api\Form\SettingsForm'
_title: 'Quant API Configuration'
requirements:
_permission: 'access administration pages'
_permission: 'configure quant'
options:
_admin_route: TRUE
10 changes: 10 additions & 0 deletions modules/quant_cron/quant_cron.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Quant Cron
description: Support running Quant processes during cron runs
package: Quant

type: module
core_version_requirement: ^8 || ^9
core: 8.x

dependencies:
- drupal:quant
4 changes: 4 additions & 0 deletions modules/quant_cron/quant_cron.links.task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
quant_cron.settings_form:
route_name: quant_cron.settings_form
title: 'Cron'
base_route: quant.config
82 changes: 82 additions & 0 deletions modules/quant_cron/quant_cron.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

use Drupal\quant\Seed;
use Drupal\Core\Form\FormState;
use Drupal\quant\Event\CollectEntitiesEvent;
use Drupal\quant\Event\CollectFilesEvent;
use Drupal\quant\Event\CollectRedirectsEvent;
use Drupal\quant\Event\CollectRoutesEvent;
use Drupal\quant\Event\QuantCollectionEvents;

function quant_cron_cron() {

// Load the settings form.
$form_state = new FormState();
$form_state->setRebuild();
\Drupal::formBuilder()->buildForm('Drupal\quant_cron\Form\CronSettingsForm', $form_state);
$event_dispatcher = \Drupal::service('event_dispatcher');


$batch = [
'title' => t('Exporting to Quant...'),
'operations' => [],
'init_message' => t('Commencing'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('An error occurred during processing'),
'finished' => '\Drupal\quant\Seed::finishedSeedCallback',
];

$assets = [];
$routes = [];
$redirects = [];

// Lunr.
if ($form_state->getValue('lunr')) {
$assets = array_merge($assets, Seed::findLunrAssets());
$routes = array_merge($routes, Seed::findLunrRoutes());
}

// Custom routes.
if ($form_state->getValue('routes_export')) {
foreach (explode(PHP_EOL, $cron_config->get('routes_export')) as $route) {
if (strpos((trim($route)), '/') !== 0) {
continue;
}
$routes[] = trim($route);
}
}

if ($form_state->getValue('redirects')) {
// Collect the redirects for the seed.
$event = new CollectRedirectsEvent([], $form_state);
$event_dispatcher->dispatch(QuantCollectionEvents::REDIRECTS, $event);
while ($redirect = $event->getEntity()) {
$batch['operations'][] = ['\Drupal\quant\Seed::exportRedirect', [$redirect]];
}
}

if ($form_state->getValue('entity_node')) {
$revisions = $form_state->getValue('entity_node_revisions');
$event = new CollectEntitiesEvent([], $revisions, $form_state);
$event_dispatcher->dispatch(QuantCollectionEvents::ENTITIES, $event);
while ($entity = $event->getEntity()) {
$batch['operations'][] = ['\Drupal\quant\Seed::exportNode', [$entity]];
}
}

$event = new CollectRoutesEvent($routes, $form_state);
$event_dispatcher->dispatch(QuantCollectionEvents::ROUTES, $event);
while ($route = $event->getRoute()) {
$batch['operations'][] = ['\Drupal\quant\Seed::exportRoute', [$route]];
}

$event = new CollectFilesEvent($assets, $form_state);
$event_dispatcher->dispatch(QuantCollectionEvents::FILES, $event);
while ($file = $event->getFilePath()) {
$batch['operations'][] = ['\Drupal\quant\Seed::exportFile', [$file]];
}

batch_set($batch);
drush_backend_batch_process();

}
9 changes: 9 additions & 0 deletions modules/quant_cron/quant_cron.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
quant_cron.settings_form:
path: '/admin/config/development/quant/cron'
defaults:
_form: '\Drupal\quant_cron\Form\CronSettingsForm'
_title: 'Quant Cron Configuration'
requirements:
_permission: 'configure quant'
options:
_admin_route: TRUE
202 changes: 202 additions & 0 deletions modules/quant_cron/src/Form/CronSettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

namespace Drupal\quant_cron\Form;

use Drupal\node\Entity\Node;
use Drupal\quant\Seed;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\quant\QuantStaticTrait;

/**
* Contains a form for configuring cron events.
*
* @internal
*/
class CronSettingsForm extends FormBase {

use QuantStaticTrait;

/**
* {@inheritdoc}.
*/
public function getFormId() {
return 'quant_cron_settings_form';
}

/**
* {@inheritdoc}.
*/
public function buildForm(array $form, FormStateInterface $form_state) {

$warnings = $this->getWarnings();
$config = $this->config('quant_cron.settings');
$moduleHandler = \Drupal::moduleHandler();

if (!empty($warnings)) {
$form['warnings'] = [
'#type' => 'container',
'title' => [
'#markup' => '<strong>' . $this->t('Build warnings') . '</strong>',
],
'list' => [
'#theme' => 'item_list',
'#items' => [],
],
];
foreach ($warnings as $warning) {
$form['warnings']['list']['#items'][] = [
'#markup' => $warning,
];
}
}

$form['entity_node'] = [
'#type' => 'checkbox',
'#title' => $this->t('Nodes'),
'#description' => $this->t('Exports the latest revision of each node.'),
'#default_value' => !empty($config->get('entity_node', '')),
];

// Seed by language.
// Only active if there are more than one active languages.
$languages = \Drupal::languageManager()->getLanguages();

if (count($languages) > 1) {
$defaultLanguage = \Drupal::languageManager()->getDefaultLanguage();
$language_codes = [];

foreach ($languages as $langcode => $language) {
$default = ($defaultLanguage->getId() == $langcode) ? ' (Default)' : '';
$language_codes[$langcode] = $language->getName() . $default;
}

$form['entity_node_languages'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Languages'),
'#description' => $this->t('Optionally restrict to these languages. If no options are selected all languages will be exported.'),
'#options' => $language_codes,
'#states' => [
'visible' => [
':input[name="entity_node"]' => ['checked' => TRUE],
],
],
'#default_value' => $config->get('entity_node_languages'),
];
}

// Seed by bundle.
$types = \Drupal::entityTypeManager()
->getStorage('node_type')
->loadMultiple();

$content_types = [];
foreach($types as $type) {
$content_types[$type->id()] = $type->label();
}

$form['entity_node_bundles'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Enabled bundles'),
'#description' => $this->t('Optionally restrict to these content types.'),
'#options' => $content_types,
'#states' => [
'visible' => [
':input[name="entity_node"]' => ['checked' => TRUE],
],
],
'#default_value' => $config->get('entity_node_bundles'),
];

$form['entity_taxonomy_term'] = [
'#type' => 'checkbox',
'#title' => $this->t('Taxonomy terms'),
'#description' => $this->t('Exports taxonomy term pages.'),
'#default_value' => $config->get('entity_taxonomy_term'),
];

// @todo: Implement these as plugins.
$form['theme_assets'] = [
'#type' => 'checkbox',
'#title' => $this->t('Theme assets'),
'#description' => $this->t('Images, fonts and favicon in the public theme.'),
'#default_value' => $config->get('theme_assets'),
];

$form['views_pages'] = [
'#type' => 'checkbox',
'#title' => $this->t('Views (Pages)'),
'#description' => $this->t('Exports all views with a Page display accessible to anonymous users.'),
'#default_value' => $config->get('views_pages'),
];

$form['routes'] = [
'#type' => 'checkbox',
'#title' => $this->t('Custom routes'),
'#description' => $this->t('Exports custom list of routes.'),
'#default_value' => !empty($config->get('routes_export')),
];

$form['routes_textarea'] = [
'#type' => 'textarea',
'#title' => $this->t('Routes'),
'#description' => $this->t('Add routes to export, each on a new line.'),
'#states' => [
'visible' => [
':input[name="routes"]' => ['checked' => TRUE],
],
],
'#default_value' => $config->get('routes_export'),
];

$form['robots'] = [
'#type' => 'checkbox',
'#title' => $this->t('Robots.txt'),
'#description' => $this->t('Export robots.txt to Quant.'),
'#default_value' => $config->get('robots'),
];

if ($moduleHandler->moduleExists('lunr')) {
$form['lunr'] = [
'#type' => 'checkbox',
'#title' => 'Lunr search assets',
'#description' => $this->t('Exports required lunr javascript libraries and all search indexes for decoupled search.'),
'#default_value' => $config->get('lunr'),
];
}

$form['actions'] = [
'#type' => 'actions',
];

$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save cron settings'),
];

return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory->getEditable('quant_cron.settings');
$config->set('routes_export', $form_state->getValue('routes_textarea'))->save();
$config->set('entity_node', $form_state->getValue('entity_node'))->save();
$config->set('entity_node_languages', $form_state->getValue('entity_node_languages'))->save();
$config->set('entity_node_bundles', $form_state->getValue('entity_node_bundles'))->save();
$config->set('entity_taxonomy_term', $form_state->getValue('entity_taxonomy_term'))->save();
$config->set('theme_assets', $form_state->getValue('theme_assets'))->save();
$config->set('views_pages', $form_state->getValue('views_pages'))->save();
$config->set('robots', $form_state->getValue('robots'))->save();
$config->set('lunr', $form_state->getValue('lunr'))->save();
}

}
6 changes: 3 additions & 3 deletions src/EventSubscriber/CollectionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function collectFiles(CollectFilesEvent $event) {
die;
}

$directoryIterator = new \RecursiveDirectoryIterator($themePath, RecursiveDirectoryIterator::SKIP_DOTS);
$directoryIterator = new \RecursiveDirectoryIterator($themePath, \RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new \RecursiveIteratorIterator($directoryIterator);
$regex = new \RegexIterator($iterator, '/^.+(.jpe?g|.png|.svg|.ttf|.woff|.woff2|.otf|.ico)$/i', \RecursiveRegexIterator::GET_MATCH);

Expand All @@ -163,12 +163,12 @@ public function collectFiles(CollectFilesEvent $event) {
$iterator = new \AppendIterator();

if (is_dir($filesPath . '/css')) {
$directoryIteratorCss = new \RecursiveDirectoryIterator($filesPath . '/css', RecursiveDirectoryIterator::SKIP_DOTS);
$directoryIteratorCss = new \RecursiveDirectoryIterator($filesPath . '/css', \RecursiveDirectoryIterator::SKIP_DOTS);
$iterator->append(new \RecursiveIteratorIterator($directoryIteratorCss));
}

if (is_dir($filesPath . '/js')) {
$directoryIteratorJs = new \RecursiveDirectoryIterator($filesPath . '/js', RecursiveDirectoryIterator::SKIP_DOTS);
$directoryIteratorJs = new \RecursiveDirectoryIterator($filesPath . '/js', \RecursiveDirectoryIterator::SKIP_DOTS);
$iterator->append(new \RecursiveIteratorIterator($directoryIteratorJs));
}

Expand Down

0 comments on commit 0cd37c5

Please sign in to comment.