Elgg  Version 4.3
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\Hook $hook) {
64  $seeds = $hook->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) {
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 }
trait Seeding
Seeding trait Can be used to easily create new random users, groups and objects in the database...
Definition: Seeding.php:24
$defaults
getCountOptions()
Get options for elgg_count_entities()
$options
Elgg admin footer.
Definition: footer.php:6
setCreateUntil($until= 'now')
Set a time for entities to be created until.
Definition: TimeHelpers.php:45
Models an event passed to hook handlers.
Definition: Hook.php:11
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:556
Provides seedable methods for database seeding and unit tests.
Definition: Seedable.php:8
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
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