Skip to content

Commit

Permalink
Merge pull request #19 from quantcdn/feature/drush-seed
Browse files Browse the repository at this point in the history
Drush seed
  • Loading branch information
steveworley authored Jul 13, 2021
2 parents 5873d6e + b96eb1b commit afca05f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
4 changes: 4 additions & 0 deletions modules/quant_api/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,28 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => $this->t('API Endpoint'),
'#description' => $this->t('e.g: https://api.quantcdn.io'),
'#default_value' => $config->get('api_endpoint'),
'#required' => TRUE,
];

$form['api_account'] = [
'#type' => 'textfield',
'#title' => $this->t('API Account'),
'#default_value' => $config->get('api_account'),
'#required' => TRUE,
];

$form['api_project'] = [
'#type' => 'textfield',
'#title' => $this->t('API Project'),
'#default_value' => $config->get('api_project'),
'#required' => TRUE,
];

$form['api_token'] = [
'#type' => 'password',
'#title' => $this->t('API Token'),
'#default_value' => $config->get('api_token'),
'#required' => TRUE,
];

return parent::buildForm($form, $form_state);
Expand Down
110 changes: 110 additions & 0 deletions src/Commands/QuantDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
namespace Drupal\quant\Commands;

use Drush\Commands\DrushCommands;
use Drupal\Core\Form\FormState;
use Drupal\quant\Seed;
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;

/**
* A drush command file.
Expand All @@ -28,4 +35,107 @@ public function message($options = ['threads' => 5]) {
}
}

/**
* Clear the quant queue.
*
* @command quant:clear-queue
* @aliases quant-queue-clear
* @usage quant:clear-queue
*/
public function clear($options = []) {
$queue_factory = \Drupal::service('queue');
$queue = $queue_factory->get('quant_seed_worker');
$queue->deleteQueue();
$this->output()->writeln("Removed all items from Quant queue.");
}

/**
* Drush command to prepare the seed.
*
* @command quant:seed-queue
* @aliases quant-queue-seed
* @options reset
* Reset the current queue.
* @usage quant:seed-queue
*/
public function prepare($options = ['reset' => 'true']) {
$this->output()->writeln("Preparing seed...");

$config = \Drupal::configFactory()->getEditable('quant.settings');

$queue_factory = \Drupal::service('queue');
$queue = $queue_factory->get('quant_seed_worker');

$dispatcher = \Drupal::service('event_dispatcher');

if ($options['reset'] == 'true') {
$queue->deleteQueue();
}

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

// Prepare the form state based on the config.
$form_state = new FormState();
$config_keys = [
'entity_node',
'entity_node_languages',
'entity_node_bundles',
'entity_node_revisions',
'entity_taxonomy_term',
'theme_assets',
'views_pages',
'redirects',
'routes',
'routes_textarea',
'robots',
'lunr',
];

foreach ($config_keys as $key) {
$form_state->setValue($key, $config->get($key));
}

if ($config->get('lunr')) {
$assets = array_merge($assets, Seed::findLunrAssets());
$routes = array_merge($routes, Seed::findLunrRoutes());
}

if ($form_state->getValue('routes_textarea')) {
foreach (explode(PHP_EOL, $form_state->getValue('routes')) 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);
$dispatcher->dispatch(QuantCollectionEvents::REDIRECTS, $event);
}

if ($form_state->getValue('entity_node')) {
$event = new CollectEntitiesEvent($form_state);
$dispatcher->dispatch(QuantCollectionEvents::ENTITIES, $event);
}

$event = new CollectRoutesEvent($form_state);
$dispatcher->dispatch(QuantCollectionEvents::ROUTES, $event);

foreach ($routes as $route) {
$event->queueItem($route);
}

$event = new CollectFilesEvent($form_state);
$dispatcher->dispatch(QuantCollectionEvents::FILES, $event);

foreach ($assets as $asset) {
$event->queueItem($asset);
}

$this->output()->writeln('Successfully added [' . $queue->numberOfItems() . '] to the queue');
}

}
2 changes: 1 addition & 1 deletion src/Plugin/QueueItem/NodeItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NodeItem implements QuantQueueItemInterface {
public function __construct(array $data = []) {
$this->id = $data['id'];
$this->vid = isset($data['vid']) ? $data['vid'] : FALSE;
$this->filter = array_filter($data['lang_filter']);
$this->filter = isset($data['lang_filter']) && is_array($data['lang_filter']) ? array_filter($data['lang_filter']) : [];
}

/**
Expand Down

0 comments on commit afca05f

Please sign in to comment.