Elgg  Version master
Seeder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
8 use Elgg\Invoker;
9 
17 class Seeder {
18 
20 
21  protected Progress $progress;
22 
23  protected Invoker $invoker;
24 
32  public function __construct(
33  EventsService $events,
34  Progress $progress,
35  Invoker $invoker
36  ) {
37  $this->events = $events;
38  $this->progress = $progress;
39  $this->invoker = $invoker;
40  }
41 
55  public function seed(array $options = []): void {
56  $this->invoker->call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_DISABLE_SYSTEM_LOG, function() use ($options) {
57  $defaults = [
58  'limit' => max(elgg_get_config('default_limit'), 20),
59  'image_folder' => elgg_get_config('seeder_local_image_folder'),
60  'type' => '',
61  'create' => false,
62  'create_since' => 'now',
63  'create_until' => 'now',
64  ];
65  $options = array_merge($defaults, $options);
66 
67  $seeds = $this->getSeederClasses();
68 
69  // set global configuration
70  if ($options['image_folder'] !== $defaults['image_folder']) {
71  elgg_set_config('seeder_local_image_folder', $options['image_folder']);
72  }
73 
74  unset($options['image_folder']);
75 
76  foreach ($seeds as $seed) {
77  // check for type limitation
78  if (!empty($options['type']) && $options['type'] !== $seed::getType()) {
79  continue;
80  }
81 
82  /* @var $seeder Seed */
83  $seeder = new $seed($options);
84 
85  $progress_bar = $this->progress->start($seed, $options['limit']);
86 
87  $seeder->setProgressBar($progress_bar);
88 
89  $seeder->seed();
90 
91  $this->progress->finish($progress_bar);
92  }
93  });
94  }
95 
104  public function unseed(array $options = []): void {
105  $this->invoker->call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_DISABLE_SYSTEM_LOG, function() use ($options) {
106  $defaults = [
107  'type' => '',
108  ];
109  $options = array_merge($defaults, $options);
110 
111  $seeds = $this->getSeederClasses();
112 
113  foreach ($seeds as $seed) {
114  // check for type limitation
115  if (!empty($options['type']) && $options['type'] !== $seed::getType()) {
116  continue;
117  }
118 
119  /* @var $seeder Seed */
120  $seeder = new $seed();
121 
122  $progress_bar = $this->progress->start($seed, $seeder->getCount());
123 
124  $seeder->setProgressBar($progress_bar);
125 
126  $seeder->unseed();
127 
128  $this->progress->finish($progress_bar);
129  }
130  });
131  }
132 
138  public function getSeederClasses(): array {
139  $result = [];
140 
141  $seeds = $this->events->triggerResults('seeds', 'database', [], []);
142  foreach ($seeds as $seed) {
143  if (!class_exists($seed)) {
144  elgg_log("Seeding class {$seed} not found", 'ERROR');
145  continue;
146  }
147 
148  if (!is_subclass_of($seed, Seed::class)) {
149  elgg_log("Seeding class {$seed} does not extend " . Seed::class, 'ERROR');
150  continue;
151  }
152 
153  $result[] = $seed;
154  }
155 
156  return $result;
157  }
158 }
const ELGG_DISABLE_SYSTEM_LOG
Definition: constants.php:134
getSeederClasses()
Get the class names of all registered seeders (verified to work for seeding)
Definition: Seeder.php:138
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
seed(array $options=[])
Load seed scripts.
Definition: Seeder.php:55
$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.
$options
Elgg admin footer.
Definition: footer.php:6
Seeder class.
Definition: Seeder.php:17
elgg_set_config(string $name, $value)
Set an Elgg configuration value.
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:130
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:104
__construct(EventsService $events, Progress $progress, Invoker $invoker)
Seeder constructor.
Definition: Seeder.php:32
Invoker $invoker
Definition: Seeder.php:23
Progress $progress
Definition: Seeder.php:21
EventsService $events
Definition: Seeder.php:19
Invocation service.
Definition: Invoker.php:10