Elgg  Version 4.3
DatabaseSeedCommand.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
7 
12 
16  protected function configure() {
17  $seeders = _elgg_services()->seeder->getSeederClasses();
18  $types = [];
19  foreach ($seeders as $seed) {
20  $types[] = $seed::getType();
21  }
22 
23  $this->setName('database:seed')
24  ->setDescription(elgg_echo('cli:database:seed:description'))
25  ->addOption('limit', 'l', InputOption::VALUE_OPTIONAL,
26  elgg_echo('cli:database:seed:option:limit')
27  )
28  ->addOption('image_folder', null, InputOption::VALUE_OPTIONAL,
29  elgg_echo('cli:database:seed:option:image_folder')
30  )
31  ->addOption('type', 't', InputOption::VALUE_OPTIONAL,
32  elgg_echo('cli:database:seed:option:type', [implode('|', $types)])
33  )
34  ->addOption('create_since', null, InputOption::VALUE_OPTIONAL,
35  elgg_echo('cli:database:seed:option:create_since'), 'now'
36  )
37  ->addOption('create_until', null, InputOption::VALUE_OPTIONAL,
38  elgg_echo('cli:database:seed:option:create_until'), 'now'
39  )
40  ->addArgument('create', InputArgument::OPTIONAL,
41  elgg_echo('cli:database:seed:argument:create')
42  );
43  }
44 
48  protected function command() {
49 
50  if (!class_exists('\Faker\Generator')) {
51  elgg_log(elgg_echo('cli:database:seed:log:error:faker'), 'ERROR');
52 
53  return 1;
54  }
55 
56  set_time_limit(0);
57 
58  if (elgg_is_logged_in()) {
59  elgg_log(elgg_echo('cli:database:seed:log:error:logged_in'), 'ERROR');
60 
61  return 2;
62  }
63 
64  _elgg_services()->set('mailer', new \Laminas\Mail\Transport\InMemory());
65 
66  $options = [
67  'limit' => (int) $this->option('limit') ? : 20,
68  'image_folder' => $this->option('image_folder'),
69  'type' => $this->option('type'),
70  'create_since' => $this->option('create_since'),
71  'create_until' => $this->option('create_until'),
72  'create' => (bool) $this->argument('create'),
73  ];
74 
75  try {
76  _elgg_services()->seeder->seed($options);
77  } catch (\Exception $e) {
78  elgg_log($e->getMessage(), 'ERROR');
79 
80  return $e->getCode() ? : 3;
81  }
82 
83  return 0;
84  }
85 }
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:43
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
$options
Elgg admin footer.
Definition: footer.php:6
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:399
Abstract command with some utility methods.
Definition: Command.php:12
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
argument($name)
Returns argument value.
option($name)
Returns option value.
elgg-cli database:seed [–limit]
Namespace for generating in-memory filesystems.
Definition: InMemory.php:14