1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eccube\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Faker\Factory as Faker; |
10
|
|
|
|
11
|
|
|
class GenerateDummyDataCommand extends \Knp\Command\Command |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected $app; |
15
|
|
|
|
16
|
|
|
protected function configure() |
17
|
|
|
{ |
18
|
|
|
$this |
19
|
|
|
->setName('dummydata:generate') |
20
|
|
|
->setDescription('Dummy data generator') |
21
|
|
|
->addOption('with-locale', null, InputOption::VALUE_REQUIRED, 'Set to the locale.', 'ja_JP') |
22
|
|
|
->addOption('without-image', null, InputOption::VALUE_NONE, 'Do not generate images.') |
23
|
|
|
->addOption('with-image', null, InputOption::VALUE_REQUIRED, 'Generate image type of abstract|animals|business|cats|city|food|nightlife|fashion|people|nature|sports|technics|transport', 'cats') |
24
|
|
|
->addOption('products', null, InputOption::VALUE_REQUIRED, 'Number of Products.', 100) |
25
|
|
|
->addOption('orders', null, InputOption::VALUE_REQUIRED, 'Number of Orders.', 10) |
26
|
|
|
->addOption('customers', null, InputOption::VALUE_REQUIRED, 'Number of Customers.', 100) |
27
|
|
|
->setHelp(<<<EOF |
28
|
|
|
The <info>%command.name%</info> command generate of dummy data. |
29
|
|
|
|
30
|
|
|
<info>php %command.full_name%</info> |
31
|
|
|
|
32
|
|
|
Generate of dummy data with images. |
33
|
|
|
|
34
|
|
|
<info>php %command.full_name% --without-image</info> |
35
|
|
|
|
36
|
|
|
Generate of dummy data without images, use for options to faster. |
37
|
|
|
; |
38
|
|
|
EOF |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
|
|
$locale = $input->getOption('with-locale'); |
45
|
|
|
$imageType = $input->getOption('with-image'); |
46
|
|
|
$notImage = $input->getOption('without-image'); |
47
|
|
|
$numberOfProducts = $input->getOption('products'); |
48
|
|
|
$numberOfOrder = $input->getOption('orders'); |
49
|
|
|
$numberOfCustomer = $input->getOption('customers'); |
50
|
|
|
|
51
|
|
|
$this->app = $this->getSilexApplication(); |
52
|
|
|
$this->app->register(new \Eccube\Tests\ServiceProvider\FixtureServiceProvider()); |
53
|
|
|
$Customers = []; |
54
|
|
|
$Products = []; |
55
|
|
|
|
56
|
|
|
$faker = Faker::create($locale); |
57
|
|
|
for ($i = 0; $i < $numberOfCustomer; $i++) { |
58
|
|
|
$email = microtime(true).'.'.$faker->safeEmail; |
59
|
|
|
$Customer = $this->app['eccube.fixture.generator.locale']($locale)->createCustomer($email); |
60
|
|
|
$Customer->setBirth($faker->dateTimeBetween('-110 years', '- 5 years')); |
61
|
|
View Code Duplication |
switch ($output->getVerbosity()) { |
62
|
|
|
case OutputInterface::VERBOSITY_QUIET: |
63
|
|
|
break; |
64
|
|
|
case OutputInterface::VERBOSITY_NORMAL: |
65
|
|
|
$output->write('C'); |
66
|
|
|
break; |
67
|
|
|
case OutputInterface::VERBOSITY_VERBOSE: |
68
|
|
|
case OutputInterface::VERBOSITY_VERY_VERBOSE: |
69
|
|
|
case OutputInterface::VERBOSITY_DEBUG: |
70
|
|
|
$output->writeln('Customer: id='.$Customer->getId().' '.$Customer->getEmail()); |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
View Code Duplication |
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL && ($i % 100) === 0 && $i > 0) { |
74
|
|
|
$output->writeln(' ...'.$i); |
75
|
|
|
} |
76
|
|
|
$Customers[] = $Customer; |
77
|
|
|
} |
78
|
|
|
for ($i = 0; $i < $numberOfProducts; $i++) { |
79
|
|
|
$Product = $this->app['eccube.fixture.generator.locale']($locale)->createProduct(null, 3, $notImage ? null : $imageType); |
80
|
|
View Code Duplication |
switch ($output->getVerbosity()) { |
81
|
|
|
case OutputInterface::VERBOSITY_QUIET: |
82
|
|
|
break; |
83
|
|
|
case OutputInterface::VERBOSITY_NORMAL: |
84
|
|
|
$output->write('P'); |
85
|
|
|
break; |
86
|
|
|
case OutputInterface::VERBOSITY_VERBOSE: |
87
|
|
|
case OutputInterface::VERBOSITY_VERY_VERBOSE: |
88
|
|
|
case OutputInterface::VERBOSITY_DEBUG: |
89
|
|
|
$output->writeln('Product: id='.$Product->getId().' '.$Product->getName()); |
90
|
|
|
break; |
91
|
|
|
} |
92
|
|
View Code Duplication |
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL && ($i % 100) === 0 && $i > 0) { |
93
|
|
|
$output->writeln(' ...'.$i); |
94
|
|
|
} |
95
|
|
|
$Products[] = $Product; |
96
|
|
|
} |
97
|
|
|
$Deliveries = $this->app['eccube.repository.delivery']->findAll(); |
98
|
|
|
$j = 0; |
99
|
|
|
foreach ($Customers as $Customer) { |
100
|
|
|
$Delivery = $Deliveries[$faker->numberBetween(0, count($Deliveries) - 1)]; |
101
|
|
|
$Product = $Products[$faker->numberBetween(0, $numberOfProducts - 1)]; |
102
|
|
|
$charge = $faker->randomNumber(4); |
103
|
|
|
$discount = $faker->randomNumber(4); |
104
|
|
|
for ($i = 0; $i < $numberOfOrder; $i++) { |
105
|
|
|
$Order = $this->app['eccube.fixture.generator.locale']($locale)->createOrder($Customer, $Product->getProductClasses()->toArray(), $Delivery, $charge, $discount); |
106
|
|
|
$Status = $this->app['eccube.repository.order_status']->find($faker->numberBetween(1, 8)); |
107
|
|
|
$Order->setOrderStatus($Status); |
108
|
|
|
$Order->setOrderDate($faker->dateTimeThisYear()); |
109
|
|
|
switch ($output->getVerbosity()) { |
110
|
|
|
case OutputInterface::VERBOSITY_QUIET: |
111
|
|
|
break; |
112
|
|
|
case OutputInterface::VERBOSITY_NORMAL: |
113
|
|
|
$output->write('O'); |
114
|
|
|
break; |
115
|
|
|
case OutputInterface::VERBOSITY_VERBOSE: |
116
|
|
|
case OutputInterface::VERBOSITY_VERY_VERBOSE: |
117
|
|
|
case OutputInterface::VERBOSITY_DEBUG: |
118
|
|
|
$output->writeln('Order: id='.$Order->getId()); |
119
|
|
|
break; |
120
|
|
|
} |
121
|
|
|
$this->app['orm.em']->flush($Order); |
122
|
|
|
$j++; |
123
|
|
View Code Duplication |
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL && ($j % 100) === 0 && $j > 0) { |
124
|
|
|
$output->writeln(' ...'.$j); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
$output->writeln(''); |
129
|
|
|
$output->writeln(sprintf("%s <info>success</info>", 'dummydata:generate')); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|