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 $limit = 20;
25 
29  protected $create = false;
30 
34  protected $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  }
50 
51  $this->create = (bool) elgg_extract('create', $options, $this->create);
52  $this->setCreateSince(elgg_extract('create_since', $options, 'now'));
53  $this->setCreateUntil(elgg_extract('create_until', $options, 'now'));
54  }
55 
63  final public static function register(\Elgg\Event $event) {
64  $seeds = $event->getValue();
65 
66  $seeds[] = static::class;
67 
68  return $seeds;
69  }
70 
76  final public function getCount(): int {
77  if ($this->create) {
78  return $this->seeded_counter;
79  }
80 
81  $defaults = [
82  'metadata_names' => '__faker',
83  ];
84  $options = array_merge($defaults, $this->getCountOptions());
85 
87  }
88 
96  public function advance(int $step = 1): void {
97  $this->seeded_counter += $step;
98 
99  $this->progressAdvance($step);
100  }
101 
107  abstract public function seed();
108 
114  abstract public function unseed();
115 
121  abstract public static function getType(): string;
122 
129  abstract protected function getCountOptions(): array;
130 }
$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
$options
Elgg admin footer.
Definition: footer.php:6
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
trait Progressing
Progress reporting.
Definition: Progressing.php:12
advance(int $step=1)
Advance progressbar.
Definition: Seed.php:96
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:515
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:41
__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:76
Models an event passed to event handlers.
Definition: Event.php:11