Elgg  Version 5.1
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  if (!class_exists('\Faker\Generator')) {
50  elgg_log(elgg_echo('cli:database:seed:log:error:faker'), 'ERROR');
51 
52  return self::FAILURE;
53  }
54 
55  set_time_limit(0);
56 
57  if (elgg_is_logged_in()) {
58  elgg_log(elgg_echo('cli:database:seed:log:error:logged_in'), 'ERROR');
59 
60  return self::INVALID;
61  }
62 
63  _elgg_services()->set('mailer', new \Laminas\Mail\Transport\InMemory());
64  _elgg_services()->events->registerHandler('enqueue', 'notification', '\Elgg\Values::getFalse', 99999);
65 
66  $options = [
67  'limit' => $this->option('limit'),
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  'interactive' => !(bool) $this->option('no-interaction'),
74  'cli_command' => $this,
75  ];
76 
77  try {
78  _elgg_services()->seeder->seed($options);
79  } catch (\Exception $e) {
80  elgg_log($e->getMessage(), 'ERROR');
81 
82  return $e->getCode() ?: 3;
83  }
84 
85  return self::SUCCESS;
86  }
87 }
elgg_is_logged_in()
Returns whether or not the user is currently logged in.
Definition: sessions.php:43
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$options
Elgg admin footer.
Definition: footer.php:6
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
Abstract command with some utility methods.
Definition: Command.php:12
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
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