Skip to content

Commit

Permalink
feat(cli): interactively set number of seeded items per seeder
Browse files Browse the repository at this point in the history
The seeders can now have their own default limit
  • Loading branch information
jeabakker committed Jul 7, 2023
1 parent 2709c2d commit 337b1bd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/intro/elgg-cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Available commands
vendor/bin/elgg-cli install [--no-plugins] [-c|--config CONFIG]
# Seed the database with fake entities
# limit: (int) number of items to seed
# limit: (int) number of items to seed (will be asked interactively for each seeder unless the -n/--no-interaction is used or only one type is seeded)
# type: (string) only seed given entity type
# create_since: (string) a compatible PHP date/time string to set the lower bound entity time created (eg, '-5 months')
# create_until: (string) a compatible PHP date/time string to set the upper bound entity time created (eg, 'yesterday')
Expand Down
4 changes: 3 additions & 1 deletion engine/classes/Elgg/Cli/DatabaseSeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ protected function command() {
_elgg_services()->set('mailer', new \Laminas\Mail\Transport\InMemory());

$options = [
'limit' => (int) $this->option('limit') ?: 20,
'limit' => $this->option('limit'),
'image_folder' => $this->option('image_folder'),
'type' => $this->option('type'),
'create_since' => $this->option('create_since'),
'create_until' => $this->option('create_until'),
'create' => (bool) $this->argument('create'),
'interactive' => !(bool) $this->option('no-interaction'),
'cli_command' => $this,
];

try {
Expand Down
49 changes: 39 additions & 10 deletions engine/classes/Elgg/Database/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Elgg\Database;

use Elgg\Cli\Command;
use Elgg\Cli\Progress;
use Elgg\Database\Seeds\Seed;
use Elgg\EventsService;
use Elgg\I18n\Translator;
use Elgg\Invoker;

/**
Expand All @@ -21,22 +23,27 @@ class Seeder {
protected Progress $progress;

protected Invoker $invoker;

protected Translator $translator;

/**
* Seeder constructor.
*
* @param EventsService $events Events service
* @param Progress $progress Progress helper
* @param Invoker $invoker Invoker service
* @param EventsService $events Events service
* @param Progress $progress Progress helper
* @param Invoker $invoker Invoker service
* @param Translator $translator Translator
*/
public function __construct(
EventsService $events,
Progress $progress,
Invoker $invoker
Invoker $invoker,
Translator $translator
) {
$this->events = $events;
$this->progress = $progress;
$this->invoker = $invoker;
$this->translator = $translator;
}

/**
Expand All @@ -55,34 +62,56 @@ public function __construct(
public function seed(array $options = []): void {
$this->invoker->call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_DISABLE_SYSTEM_LOG, function() use ($options) {
$defaults = [
'limit' => max(elgg_get_config('default_limit'), 20),
'limit' => null,
'image_folder' => elgg_get_config('seeder_local_image_folder'),
'type' => '',
'create' => false,
'create_since' => 'now',
'create_until' => 'now',
'interactive' => true,
'cli_command' => null,
];
$options = array_merge($defaults, $options);

$seeds = $this->getSeederClasses();

// set global configuration
if ($options['image_folder'] !== $defaults['image_folder']) {
elgg_set_config('seeder_local_image_folder', $options['image_folder']);
}

unset($options['image_folder']);

// fetch CLI command
$cli_command = $options['cli_command'];
unset($options['cli_command']);

// interactive mode
$interactive = $options['interactive'] && empty($options['type']);
unset($options['interactive']);

$seeds = $this->getSeederClasses();
foreach ($seeds as $seed) {
$seed_options = $options;

// check for type limitation
if (!empty($options['type']) && $options['type'] !== $seed::getType()) {
if (!empty($seed_options['type']) && $seed_options['type'] !== $seed::getType()) {
continue;
}

// check the seed limit
$seed_options['limit'] = $seed_options['limit'] ?? $seed::getDefaultLimit();
if ($interactive && $cli_command instanceof Command) {
$seed_options['limit'] = (int) $cli_command->ask($this->translator->translate('cli:database:seed:ask:limit', [$seed::getType()]), $seed_options['limit'], false, false);
}

if ($seed_options['limit'] < 1) {
// skip seeding
continue;
}

/* @var $seeder Seed */
$seeder = new $seed($options);
$seeder = new $seed($seed_options);

$progress_bar = $this->progress->start($seed, $options['limit']);
$progress_bar = $this->progress->start($seed, $seed_options['limit']);

$seeder->setProgressBar($progress_bar);

Expand Down
17 changes: 14 additions & 3 deletions engine/classes/Elgg/Database/Seeds/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ abstract class Seed implements Seedable {
/**
* @var int Max number of items to be created by the seed
*/
protected $limit = 20;
protected int $limit;

/**
* @var bool Create new entities
*/
protected $create = false;
protected bool $create = false;

/**
* @var int Number of seeded entities
*/
protected $seeded_counter = 0;
protected int $seeded_counter = 0;

/**
* Seed constructor.
Expand All @@ -46,6 +46,8 @@ public function __construct(array $options = []) {
$limit = (int) elgg_extract('limit', $options);
if ($limit > 0) {
$this->limit = $limit;
} else {
$this->limit = static::getDefaultLimit();
}

$this->create = (bool) elgg_extract('create', $options, $this->create);
Expand Down Expand Up @@ -98,6 +100,15 @@ public function advance(int $step = 1): void {

$this->progressAdvance($step);
}

/**
* Get the default number of content to seed
*
* @return int
*/
public static function getDefaultLimit(): int {
return max(elgg_get_config('default_limit'), 20);
}

/**
* Populate database
Expand Down
1 change: 1 addition & 0 deletions languages/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,7 @@
'cli:database:seed:option:create_until' => "A PHP time string to set the upper bound creation time of seeded entities",
'cli:database:seed:log:error:faker' => "This is a developer tool currently intended for testing purposes only. Please refrain from using it.",
'cli:database:seed:log:error:logged_in' => "Database seeding should not be run with a logged in user",
'cli:database:seed:ask:limit' => "How many items to seed for the '%s' seeder",

'cli:database:seeders:description' => "List all available database seeders with the current count of seeded entities",
'cli:database:seeders:handler' => "Seed handler",
Expand Down

0 comments on commit 337b1bd

Please sign in to comment.