1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Cli; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputOption; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* elgg-cli database:seed [--limit] |
10
|
|
|
*/ |
11
|
|
|
class DatabaseSeedCommand extends Command { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
1 |
|
protected function configure() { |
17
|
1 |
|
$seeders = _elgg_services()->seeder->getSeederClasses(); |
18
|
1 |
|
$types = []; |
19
|
1 |
|
foreach ($seeders as $seed) { |
20
|
1 |
|
$types[] = $seed::getType(); |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
$this->setName('database:seed') |
24
|
1 |
|
->setDescription(elgg_echo('cli:database:seed:description')) |
25
|
1 |
|
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, |
26
|
1 |
|
elgg_echo('cli:database:seed:option:limit') |
27
|
1 |
|
) |
28
|
1 |
|
->addOption('image_folder', null, InputOption::VALUE_OPTIONAL, |
29
|
1 |
|
elgg_echo('cli:database:seed:option:image_folder') |
30
|
1 |
|
) |
31
|
1 |
|
->addOption('type', 't', InputOption::VALUE_OPTIONAL, |
32
|
1 |
|
elgg_echo('cli:database:seed:option:type', [implode('|', $types)]) |
33
|
1 |
|
) |
34
|
1 |
|
->addOption('create_since', null, InputOption::VALUE_OPTIONAL, |
35
|
1 |
|
elgg_echo('cli:database:seed:option:create_since'), 'now' |
36
|
1 |
|
) |
37
|
1 |
|
->addOption('create_until', null, InputOption::VALUE_OPTIONAL, |
38
|
1 |
|
elgg_echo('cli:database:seed:option:create_until'), 'now' |
39
|
1 |
|
) |
40
|
1 |
|
->addArgument('create', InputArgument::OPTIONAL, |
41
|
1 |
|
elgg_echo('cli:database:seed:argument:create') |
42
|
1 |
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
1 |
|
protected function command() { |
49
|
1 |
|
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
|
1 |
|
set_time_limit(0); |
56
|
|
|
|
57
|
1 |
|
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
|
1 |
|
_elgg_services()->set('mailer', new \Laminas\Mail\Transport\InMemory()); |
64
|
|
|
|
65
|
1 |
|
$options = [ |
66
|
1 |
|
'limit' => $this->option('limit'), |
67
|
1 |
|
'image_folder' => $this->option('image_folder'), |
68
|
1 |
|
'type' => $this->option('type'), |
69
|
1 |
|
'create_since' => $this->option('create_since'), |
70
|
1 |
|
'create_until' => $this->option('create_until'), |
71
|
1 |
|
'create' => (bool) $this->argument('create'), |
72
|
1 |
|
'interactive' => !(bool) $this->option('no-interaction'), |
73
|
1 |
|
'cli_command' => $this, |
74
|
1 |
|
]; |
75
|
|
|
|
76
|
|
|
try { |
77
|
1 |
|
_elgg_services()->seeder->seed($options); |
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
elgg_log($e->getMessage(), 'ERROR'); |
80
|
|
|
|
81
|
|
|
return $e->getCode() ?: 3; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return self::SUCCESS; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|