Elgg  Version master
Seed.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Seeds;
4 
7 
14 abstract class Seed implements Seedable {
15 
16  use Seeding;
17  use Progressing {
18  advance as private progressAdvance;
19  }
20 
24  protected int $limit;
25 
29  protected bool $create = false;
30 
34  protected int $seeded_counter = 0;
35 
45  public function __construct(array $options = []) {
46  $limit = (int) elgg_extract('limit', $options);
47  if ($limit > 0) {
48  $this->limit = $limit;
49  } else {
50  $this->limit = static::getDefaultLimit();
51  }
52 
53  $this->create = (bool) elgg_extract('create', $options, $this->create);
54  $this->setCreateSince(elgg_extract('create_since', $options, 'now'));
55  $this->setCreateUntil(elgg_extract('create_until', $options, 'now'));
56  }
57 
65  final public static function register(\Elgg\Event $event) {
66  $seeds = $event->getValue();
67 
68  $seeds[] = static::class;
69 
70  return $seeds;
71  }
72 
78  final public function getCount(): int {
79  if ($this->create) {
80  return $this->seeded_counter;
81  }
82 
83  $defaults = [
84  'metadata_names' => '__faker',
85  ];
86  $options = array_merge($defaults, $this->getCountOptions());
87 
89  }
90 
98  public function advance(int $step = 1): void {
99  $this->seeded_counter += $step;
100 
101  $this->progressAdvance($step);
102  }
103 
109  public static function getDefaultLimit(): int {
110  return max(elgg_get_config('default_limit'), 20);
111  }
112 
118  abstract public function seed();
119 
125  abstract public function unseed();
126 
132  abstract public static function getType(): string;
133 
140  abstract protected function getCountOptions(): array;
141 }
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
$defaults
Generic entity header upload helper.
Definition: header.php:6
trait Seeding
Seeding trait Can be used to easily create new random users, groups and objects in the database...
Definition: Seeding.php:24
getCountOptions()
Get options for elgg_count_entities()
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
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
setCreateUntil($until= 'now')
Set a time for entities to be created until.
Definition: TimeHelpers.php:45
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
trait Progressing
Progress reporting.
Definition: Progressing.php:12
advance(int $step=1)
Advance progressbar.
Definition: Seed.php:98
static getDefaultLimit()
Get the default number of content to seed.
Definition: Seed.php:109
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
Provides seedable methods for database seeding and unit tests.
Definition: Seedable.php:8
static getType()
Get the (un)seeding type of this handler.
seed()
Populate database.
$step
Definition: time.php:40
__construct(array $options=[])
Seed constructor.
Definition: Seed.php:45
setCreateSince($since= 'now')
Set a time for entities to be created after.
Definition: TimeHelpers.php:33
Abstract seed.
Definition: Seed.php:14
unseed()
Removed seeded rows from database.
getCount()
Get the count of the seeded entities.
Definition: Seed.php:78
Models an event passed to event handlers.
Definition: Event.php:11