/
FakerController.php
175 lines (149 loc) · 5.41 KB
/
FakerController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace MiksIr\Yii2DbFaker;
use MiksIr\Yii2DbFaker\dbprovider\DbProviderInterface;
use MiksIr\Yii2DbFaker\generator\GeneratorInterface;
use yii\console\Controller;
use yii\di\Container;
use yii\helpers\Console;
/**
* This command can fill your database with fake data.
*/
class FakerController extends Controller
{
/**
* @var int number of data rows
*/
public $count = 1000;
/**
* @var int do not ask confirmation
*/
public $force = 0;
/**
* @var string class name of data generator
*/
public $generator = 'FakerGenerator';
private $generator_fqn;
/** @var GeneratorInterface */
private $generator_obj;
/**
* @var string class of database store provider
*/
public $dbprovider = 'Csv';
private $dbprovider_fqn;
/** @var DbProviderInterface */
private $dbprovider_obj;
/**
* @inheritdoc
*/
public function options($actionID)
{
return array_merge(parent::options($actionID), [
'count', 'force', 'generator', 'dbprovider'
]);
}
public function beforeAction($action)
{
$this->generator_fqn = $this->fixNamespace($this->generator, 'MiksIr\\Yii2DbFaker\\generator');
$this->dbprovider_fqn = $this->fixNamespace($this->dbprovider, 'MiksIr\\Yii2DbFaker\\dbprovider');
return parent::beforeAction($action);
}
public function actionHelp()
{
$this->stdout("Usage: faker/generate option=value option=value ...\n", Console::FG_YELLOW);
if ($gen_help = $this->help_of_class($this->generator_fqn, 'generator')) {
$this->stdout("Options for current generator \"{$this->generator}\":\n", Console::FG_GREEN);
$this->stdout($gen_help);
}
if ($db_help = $this->help_of_class($this->dbprovider_fqn, 'dbprovider')) {
$this->stdout("Options for current dbprovider \"{$this->dbprovider}\":\n", Console::FG_GREEN);
$this->stdout($db_help);
}
$this->stdout("Also, you can use global options:\n", Console::FG_GREEN);
$this->stdout("\t--generator - class name of generator\n".
"\t--dbprovider - class name of database storage\n".
"\t--count=10000 - number of rows to generate\n");
}
/**
* Generate fake data and
*/
public function actionGenerate()
{
$input = $this->parseArguments(func_get_args());
$container = new Container();
$container->set(GeneratorInterface::class, array_merge(['class' => $this->generator_fqn], $input['generator']));
$container->set(DbProviderInterface::class, array_merge(['class' => $this->dbprovider_fqn], $input['dbprovider']));
$this->generator_obj = $container->get(GeneratorInterface::class);
if (!$this->force && !$this->confirmGeneration()) {
return;
}
$this->dbprovider_obj = $container->get(DbProviderInterface::class);
Console::startProgress(0, $this->count);
foreach ($this->dbprovider_obj->export($this->count) as $count) {
Console::updateProgress($this->count - $count, $this->count);
}
Console::endProgress(true);
}
private function parseArguments($lines)
{
$args = [
'generator' => [],
'dbprovider' => [],
];
foreach ($lines as $line) {
list($key, $value) = explode('=', $line);
if (strstr($key, '_')) {
list ($prefix, $key) = explode('_', $key);
$args[$prefix][$key] = $value;
} else {
$args[$key] = $value;
}
}
return $args;
}
private function fixNamespace($class, $default_namespace)
{
$class = str_replace('/', '\\', $class);
if (!strstr($class, '\\')) {
$class = $default_namespace . '\\' . $class;
}
return $class;
}
private function help_of_class($class_name, $prefix)
{
$help = '';
$class = new \ReflectionClass($class_name);
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$name = $property->getName();
$defaultValue = $property->getValue($this);
$tags = $this->parseDocCommentTags($property);
if (isset($tags['var']) || isset($tags['property'])) {
$doc = isset($tags['var']) ? $tags['var'] : $tags['property'];
if (is_array($doc)) {
$doc = reset($doc);
}
if (preg_match('/^(\S+)\s*(.*)/s', $doc, $matches)) {
$type = $matches[1];
$comment = $matches[2];
} else {
$type = null;
$comment = $doc;
}
} else {
$type = null;
$comment = "???";
}
$name = $prefix . "_" . $name;
$help .= "\t{$name} - {$comment}" . ($defaultValue ? " (defalt: {$defaultValue})" : "") . "\n";
}
return $help;
}
private function confirmGeneration()
{
$this->stdout("Example of fake data (1 row):\n", Console::FG_YELLOW);
$fake_data = $this->generator_obj->generate()->current();
foreach ($fake_data as $field=>$data) {
$this->stdout("\t{$field}: {$data}\n");
}
return $this->confirm("Generate {$this->count} rows?", Console::FG_YELLOW);
}
}