Elgg  Version 4.3
Seeder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
7 use Elgg\Invoker;
9 
17 class Seeder {
18 
22  protected $hooks;
23 
27  protected $progress;
28 
32  protected $invoker;
33 
41  public function __construct(
45  ) {
46  $this->hooks = $hooks;
47  $this->progress = $progress;
48  $this->invoker = $invoker;
49  }
50 
64  public function seed(array $options = []): void {
65  $this->invoker->call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_DISABLE_SYSTEM_LOG, function() use ($options) {
66  $defaults = [
67  'limit' => max(elgg_get_config('default_limit'), 20),
68  'image_folder' => elgg_get_config('seeder_local_image_folder'),
69  'type' => '',
70  'create' => false,
71  'create_since' => 'now',
72  'create_until' => 'now',
73  ];
74  $options = array_merge($defaults, $options);
75 
76  $seeds = $this->getSeederClasses();
77 
78  // set global configuration
79  if ($options['image_folder'] !== $defaults['image_folder']) {
80  elgg_set_config('seeder_local_image_folder', $options['image_folder']);
81  }
82  unset($options['image_folder']);
83 
84  foreach ($seeds as $seed) {
85  // check for type limitation
86  if (!empty($options['type']) && $options['type'] !== $seed::getType()) {
87  continue;
88  }
89 
90  /* @var $seeder Seed */
91  $seeder = new $seed($options);
92 
93  $progress_bar = $this->progress->start($seed, $options['limit']);
94 
95  $seeder->setProgressBar($progress_bar);
96 
97  $seeder->seed();
98 
99  $this->progress->finish($progress_bar);
100  }
101  });
102  }
103 
112  public function unseed(array $options = []): void {
113  $this->invoker->call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES | ELGG_DISABLE_SYSTEM_LOG, function() use ($options) {
114  $defaults = [
115  'type' => '',
116  ];
117  $options = array_merge($defaults, $options);
118 
119  $seeds = $this->getSeederClasses();
120 
121  foreach ($seeds as $seed) {
122  // check for type limitation
123  if (!empty($options['type']) && $options['type'] !== $seed::getType()) {
124  continue;
125  }
126 
127  /* @var $seeder Seed */
128  $seeder = new $seed();
129 
130  $progress_bar = $this->progress->start($seed, $seeder->getCount());
131 
132  $seeder->setProgressBar($progress_bar);
133 
134  $seeder->unseed();
135 
136  $this->progress->finish($progress_bar);
137  }
138  });
139  }
140 
146  public function getSeederClasses(): array {
147  $result = [];
148 
149  $seeds = $this->hooks->trigger('seeds', 'database', null, []);
150  foreach ($seeds as $seed) {
151  if (!class_exists($seed)) {
152  elgg_log("Seeding class {$seed} not found", 'ERROR');
153  continue;
154  }
155  if (!is_subclass_of($seed, Seed::class)) {
156  elgg_log("Seeding class {$seed} does not extend " . Seed::class, 'ERROR');
157  continue;
158  }
159 
160  $result[] = $seed;
161  }
162 
163  return $result;
164  }
165 }
const ELGG_DISABLE_SYSTEM_LOG
Definition: constants.php:150
getSeederClasses()
Get the class names of all registered seeders (verified to work for seeding)
Definition: Seeder.php:146
seed(array $options=[])
Load seed scripts.
Definition: Seeder.php:64
$defaults
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
$options
Elgg admin footer.
Definition: footer.php:6
Seeder class.
Definition: Seeder.php:17
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:146
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:148
elgg_set_config($name, $value)
Set an Elgg configuration value.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:399
CLI Progress reporter.
Definition: Progress.php:11
unseed(array $options=[])
Remove all seeded entities.
Definition: Seeder.php:112
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
Invocation service.
Definition: Invoker.php:12
__construct(PluginHooksService $hooks, Progress $progress, Invoker $invoker)
Seeder constructor.
Definition: Seeder.php:41