Elgg  Version 3.0
Seeder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
8 
16 class Seeder {
17 
21  protected $hooks;
22 
26  protected $progress;
27 
34  public function __construct(
37  ) {
38  $this->hooks = $hooks;
39  $this->progress = $progress;
40  }
41 
49  public function seed($limit = null) {
50  if (!is_numeric($limit)) {
51  $limit = max(elgg_get_config('default_limit'), 20);
52  }
53 
54  $ia = _elgg_services()->session->setIgnoreAccess(true);
55 
56  $ha = _elgg_services()->session->getDisabledEntityVisibility();
57  _elgg_services()->session->setDisabledEntityVisibility(true);
58 
59  $seeds = $this->hooks->trigger('seeds', 'database', null, []);
60 
61  foreach ($seeds as $seed) {
62  if (!class_exists($seed)) {
63  elgg_log("Seeding class $seed not found", 'ERROR');
64  continue;
65  }
66  if (!is_subclass_of($seed, Seed::class)) {
67  elgg_log("Seeding class $seed does not extend " . Seed::class, 'ERROR');
68  continue;
69  }
70 
71  $seeder = new $seed($limit);
72  /* @var $seeder Seed */
73 
74  $progress_bar = $this->progress->start($seed, $limit);
75 
76  $seeder->setProgressBar($progress_bar);
77 
78  $seeder->seed();
79 
80  $this->progress->finish($progress_bar);
81  }
82 
83  _elgg_services()->session->setDisabledEntityVisibility($ha);
84  _elgg_services()->session->setIgnoreAccess($ia);
85  }
86 
91  public function unseed() {
92 
93  $ia = _elgg_services()->session->setIgnoreAccess(true);
94 
95  $ha = _elgg_services()->session->getDisabledEntityVisibility();
96  _elgg_services()->session->setDisabledEntityVisibility(true);
97 
98  $seeds = $this->hooks->trigger('seeds', 'database', null, []);
99 
100  foreach ($seeds as $seed) {
101  if (!class_exists($seed)) {
102  elgg_log("Seeding class $seed not found", 'ERROR');
103  continue;
104  }
105  if (!is_subclass_of($seed, Seed::class)) {
106  elgg_log("Seeding class $seed does not extend " . Seed::class, 'ERROR');
107  continue;
108  }
109  $seeder = new $seed();
110  /* @var $seeder Seed */
111 
112  $progress_bar = $this->progress->start($seed);
113 
114  $seeder->setProgressBar($progress_bar);
115 
116  $seeder->unseed();
117 
118  $this->progress->finish($progress_bar);
119  }
120 
121  _elgg_services()->session->setDisabledEntityVisibility($ha);
122  _elgg_services()->session->setIgnoreAccess($ia);
123  }
124 }
Seeder class.
Definition: Seeder.php:16
unseed()
Remove all seeded entities.
Definition: Seeder.php:91
$limit
Definition: userpicker.php:52
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
__construct(PluginHooksService $hooks, Progress $progress)
Seeder constructor.
Definition: Seeder.php:34
CLI Progress reporter.
Definition: Progress.php:11
seed($limit=null)
Load seed scripts.
Definition: Seeder.php:49
class
Definition: placeholder.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
elgg_get_config($name, $default=null)
Get an Elgg configuration value.