Elgg  Version master
Seeder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
7 use Elgg\Config;
11 use Elgg\Invoker;
12 
20 class Seeder {
21 
31  public function __construct(
32  protected EventsService $events,
33  protected Progress $progress,
34  protected Invoker $invoker,
35  protected Translator $translator,
36  protected Config $config
37  ) {
38  }
39 
53  public function seed(array $options = []): void {
55  $defaults = [
56  'limit' => null,
57  'image_folder' => elgg_get_config('seeder_local_image_folder'),
58  'type' => '',
59  'create' => false,
60  'create_since' => 'now',
61  'create_until' => 'now',
62  'interactive' => true,
63  'cli_command' => null,
64  ];
65  $options = array_merge($defaults, $options);
66 
67  // set global configuration
68  if ($options['image_folder'] !== $defaults['image_folder']) {
69  elgg_set_config('seeder_local_image_folder', $options['image_folder']);
70  }
71 
72  unset($options['image_folder']);
73 
74  // fetch CLI command
75  $cli_command = $options['cli_command'];
76  unset($options['cli_command']);
77 
78  // interactive mode
79  $interactive = $options['interactive'] && empty($options['type']);
80  unset($options['interactive']);
81 
82  $seeds = $this->getSeederClasses();
83  foreach ($seeds as $seed) {
84  $seed_options = $options;
85 
86  // check for type limitation
87  if (!empty($seed_options['type']) && $seed_options['type'] !== $seed::getType()) {
88  continue;
89  }
90 
91  // check the seed limit
92  $seed_options['limit'] = $seed_options['limit'] ?? $seed::getDefaultLimit();
93  if ($interactive && $cli_command instanceof Command) {
94  $seed_options['limit'] = (int) $cli_command->ask($this->translator->translate('cli:database:seed:ask:limit', [$seed::getType()]), $seed_options['limit'], false, false);
95  }
96 
97  if ($seed_options['limit'] < 1) {
98  // skip seeding
99  continue;
100  }
101 
102  /* @var $seeder Seed */
103  $seeder = new $seed($seed_options);
104 
105  $progress_bar = $this->progress->start($seed, $seed_options['limit']);
106 
107  $seeder->setProgressBar($progress_bar);
108 
109  $seeder->seed();
110 
111  $this->progress->finish($progress_bar);
112  }
113  });
114  }
115 
124  public function unseed(array $options = []): void {
126  $defaults = [
127  'type' => '',
128  ];
129  $options = array_merge($defaults, $options);
130 
131  $seeds = $this->getSeederClasses();
132 
133  // disable trash during unseed, everything needs te be permanently removed
134  $trash_enabled = $this->config->trash_enabled;
135  $this->config->trash_enabled = false;
136  foreach ($seeds as $seed) {
137  // check for type limitation
138  if (!empty($options['type']) && $options['type'] !== $seed::getType()) {
139  continue;
140  }
141 
142  /* @var $seeder Seed */
143  $seeder = new $seed();
144 
145  $progress_bar = $this->progress->start($seed, $seeder->getCount());
146 
147  $seeder->setProgressBar($progress_bar);
148 
149  $seeder->unseed();
150 
151  $this->progress->finish($progress_bar);
152  }
153 
154  // restore trash config setting
155  $this->config->trash_enabled = $trash_enabled;
156  });
157  }
158 
164  public function getSeederClasses(): array {
165  $result = [];
166 
167  $seeds = $this->events->triggerResults('seeds', 'database', [], []);
168  foreach ($seeds as $seed) {
169  if (!class_exists($seed)) {
170  elgg_log("Seeding class {$seed} not found", \Psr\Log\LogLevel::ERROR);
171  continue;
172  }
173 
174  if (!is_subclass_of($seed, Seed::class)) {
175  elgg_log("Seeding class {$seed} does not extend " . Seed::class, \Psr\Log\LogLevel::ERROR);
176  continue;
177  }
178 
179  $result[] = $seed;
180  }
181 
182  return $result;
183  }
184 }
Abstract command with some utility methods.
Definition: Command.php:12
CLI Progress reporter.
Definition: Progress.php:11
Seeder class.
Definition: Seeder.php:20
unseed(array $options=[])
Remove all seeded entities.
Definition: Seeder.php:124
seed(array $options=[])
Load seed scripts.
Definition: Seeder.php:53
__construct(protected EventsService $events, protected Progress $progress, protected Invoker $invoker, protected Translator $translator, protected Config $config)
Seeder constructor.
Definition: Seeder.php:31
getSeederClasses()
Get the class names of all registered seeders (verified to work for seeding)
Definition: Seeder.php:164
Abstract seed.
Definition: Seed.php:14
Events service.
Invocation service.
Definition: Invoker.php:10
elgg_set_config(string $name, $value)
Set an Elgg configuration value.
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
const ELGG_DISABLE_SYSTEM_LOG
Definition: constants.php:125
const ELGG_SHOW_DELETED_ENTITIES
Definition: constants.php:127
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:88
$defaults
Generic entity header upload helper.
Definition: header.php:6