Elgg  Version master
Seeder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
10 use Elgg\Invoker;
11 
19 class Seeder {
20 
22 
23  protected Progress $progress;
24 
25  protected Invoker $invoker;
26 
28 
37  public function __construct(
38  EventsService $events,
39  Progress $progress,
40  Invoker $invoker,
41  Translator $translator
42  ) {
43  $this->events = $events;
44  $this->progress = $progress;
45  $this->invoker = $invoker;
46  $this->translator = $translator;
47  }
48 
62  public function seed(array $options = []): void {
64  $defaults = [
65  'limit' => null,
66  'image_folder' => elgg_get_config('seeder_local_image_folder'),
67  'type' => '',
68  'create' => false,
69  'create_since' => 'now',
70  'create_until' => 'now',
71  'interactive' => true,
72  'cli_command' => null,
73  ];
74  $options = array_merge($defaults, $options);
75 
76  // set global configuration
77  if ($options['image_folder'] !== $defaults['image_folder']) {
78  elgg_set_config('seeder_local_image_folder', $options['image_folder']);
79  }
80 
81  unset($options['image_folder']);
82 
83  // fetch CLI command
84  $cli_command = $options['cli_command'];
85  unset($options['cli_command']);
86 
87  // interactive mode
88  $interactive = $options['interactive'] && empty($options['type']);
89  unset($options['interactive']);
90 
91  $seeds = $this->getSeederClasses();
92  foreach ($seeds as $seed) {
93  $seed_options = $options;
94 
95  // check for type limitation
96  if (!empty($seed_options['type']) && $seed_options['type'] !== $seed::getType()) {
97  continue;
98  }
99 
100  // check the seed limit
101  $seed_options['limit'] = $seed_options['limit'] ?? $seed::getDefaultLimit();
102  if ($interactive && $cli_command instanceof Command) {
103  $seed_options['limit'] = (int) $cli_command->ask($this->translator->translate('cli:database:seed:ask:limit', [$seed::getType()]), $seed_options['limit'], false, false);
104  }
105 
106  if ($seed_options['limit'] < 1) {
107  // skip seeding
108  continue;
109  }
110 
111  /* @var $seeder Seed */
112  $seeder = new $seed($seed_options);
113 
114  $progress_bar = $this->progress->start($seed, $seed_options['limit']);
115 
116  $seeder->setProgressBar($progress_bar);
117 
118  $seeder->seed();
119 
120  $this->progress->finish($progress_bar);
121  }
122  });
123  }
124 
133  public function unseed(array $options = []): void {
135  $defaults = [
136  'type' => '',
137  ];
138  $options = array_merge($defaults, $options);
139 
140  $seeds = $this->getSeederClasses();
141 
142  foreach ($seeds as $seed) {
143  // check for type limitation
144  if (!empty($options['type']) && $options['type'] !== $seed::getType()) {
145  continue;
146  }
147 
148  /* @var $seeder Seed */
149  $seeder = new $seed();
150 
151  $progress_bar = $this->progress->start($seed, $seeder->getCount());
152 
153  $seeder->setProgressBar($progress_bar);
154 
155  $seeder->unseed();
156 
157  $this->progress->finish($progress_bar);
158  }
159  });
160  }
161 
167  public function getSeederClasses(): array {
168  $result = [];
169 
170  $seeds = $this->events->triggerResults('seeds', 'database', [], []);
171  foreach ($seeds as $seed) {
172  if (!class_exists($seed)) {
173  elgg_log("Seeding class {$seed} not found", 'ERROR');
174  continue;
175  }
176 
177  if (!is_subclass_of($seed, Seed::class)) {
178  elgg_log("Seeding class {$seed} does not extend " . Seed::class, 'ERROR');
179  continue;
180  }
181 
182  $result[] = $seed;
183  }
184 
185  return $result;
186  }
187 }
const ELGG_DISABLE_SYSTEM_LOG
Definition: constants.php:134
__construct(EventsService $events, Progress $progress, Invoker $invoker, Translator $translator)
Seeder constructor.
Definition: Seeder.php:37
getSeederClasses()
Get the class names of all registered seeders (verified to work for seeding)
Definition: Seeder.php:167
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
seed(array $options=[])
Load seed scripts.
Definition: Seeder.php:62
$defaults
Generic entity header upload helper.
Definition: header.php:6
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
Events service.
Seeder class.
Definition: Seeder.php:19
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
elgg_set_config(string $name, $value)
Set an Elgg configuration value.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:130
Translator $translator
Definition: Seeder.php:27
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:132
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
CLI Progress reporter.
Definition: Progress.php:11
unseed(array $options=[])
Remove all seeded entities.
Definition: Seeder.php:133
const ELGG_SHOW_DELETED_ENTITIES
Definition: constants.php:136
Abstract command with some utility methods.
Definition: Command.php:12
Invoker $invoker
Definition: Seeder.php:25
Progress $progress
Definition: Seeder.php:23
EventsService $events
Definition: Seeder.php:21
Invocation service.
Definition: Invoker.php:10