Elgg  Version master
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 
65  $options = [
66  'limit' => $this->option('limit'),
67  'image_folder' => $this->option('image_folder'),
68  'type' => $this->option('type'),
69  'create_since' => $this->option('create_since'),
70  'create_until' => $this->option('create_until'),
71  'create' => (bool) $this->argument('create'),
72  'interactive' => !(bool) $this->option('no-interaction'),
73  'cli_command' => $this,
74  ];
75 
76  try {
77  _elgg_services()->seeder->seed($options);
78  } catch (\Exception $e) {
79  elgg_log($e->getMessage(), 'ERROR');
80 
81  return $e->getCode() ?: 3;
82  }
83 
84  return self::SUCCESS;
85  }
86 }
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