1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smartbox\CoreBundle\Command\Fixtures; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\SerializerInterface; |
6
|
|
|
use Smartbox\CoreBundle\Type\Context\ContextFactory; |
7
|
|
|
use Smartbox\CoreBundle\Utils\Generator\RandomFixtureGenerator; |
8
|
|
|
use Smartbox\CoreBundle\Utils\Helper\NamespaceResolver; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class GenerateRandomFixtureCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** @var InputInterface */ |
18
|
|
|
protected $in; |
19
|
|
|
|
20
|
|
|
/** @var OutputInterface */ |
21
|
|
|
protected $out; |
22
|
|
|
|
23
|
|
|
protected $version; |
24
|
|
|
|
25
|
|
|
protected $group; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var SerializerInterface |
29
|
|
|
*/ |
30
|
|
|
protected $serializer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var NamespaceResolver |
34
|
|
|
*/ |
35
|
|
|
protected $namespaceResolver; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RandomFixtureGenerator |
39
|
|
|
*/ |
40
|
|
|
private $randomFixtureGenerator; |
41
|
|
|
|
42
|
|
|
public function __construct(string $name, SerializerInterface $serializer, NamespaceResolver $namespaceResolver, RandomFixtureGenerator $randomFixtureGenerator) |
43
|
|
|
{ |
44
|
|
|
$this->serializer = $serializer; |
45
|
|
|
$this->namespaceResolver = $namespaceResolver; |
46
|
|
|
$this->randomFixtureGenerator = $randomFixtureGenerator; |
47
|
|
|
parent::__construct($name); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function configure() |
51
|
|
|
{ |
52
|
|
|
$this |
53
|
|
|
->setDescription('Generates an random fixture of a smartesb entity') |
54
|
|
|
->addArgument('entity', InputArgument::REQUIRED, 'Entity class') |
55
|
|
|
->addOption( |
56
|
|
|
'entity-version', |
57
|
|
|
null, |
58
|
|
|
InputOption::VALUE_REQUIRED, |
59
|
|
|
'Determine the version of entity' |
60
|
|
|
) |
61
|
|
|
->addOption( |
62
|
|
|
'entity-group', |
63
|
|
|
null, |
64
|
|
|
InputOption::VALUE_REQUIRED, |
65
|
|
|
'Determine a group of entity' |
66
|
|
|
) |
67
|
|
|
->addOption( |
68
|
|
|
'raw-output', |
69
|
|
|
null, |
70
|
|
|
InputOption::VALUE_NONE, |
71
|
|
|
'If set raw json without comments will be printed' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param InputInterface $in |
77
|
|
|
* @param OutputInterface $out |
78
|
|
|
* |
79
|
|
|
* @throws \InvalidArgumentException |
80
|
|
|
*/ |
81
|
|
|
protected function execute(InputInterface $in, OutputInterface $out) |
82
|
|
|
{ |
83
|
|
|
$this->in = $in; |
84
|
|
|
$this->out = $out; |
85
|
|
|
$serializer = $this->serializer; |
86
|
|
|
$namespaceResolver = $this->namespaceResolver; |
87
|
|
|
$entityClass = $this->in->getArgument('entity'); |
88
|
|
|
$group = $this->in->getOption('entity-group'); |
89
|
|
|
$version = $this->in->getOption('entity-version'); |
90
|
|
|
$rawOutput = $this->in->getOption('raw-output'); |
91
|
|
|
|
92
|
|
|
if (!$rawOutput) { |
93
|
|
|
$this->out->writeln('<info>####################################</info>'); |
94
|
|
|
$this->out->writeln('<info>## Random Fixture generator ##</info>'); |
95
|
|
|
$this->out->writeln('<info>####################################</info>'); |
96
|
|
|
$this->out->writeln(''); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$entityNamespace = $namespaceResolver->resolveNamespaceForClass($entityClass); |
100
|
|
|
|
101
|
|
|
$randomFixtureGenerator = $this->randomFixtureGenerator; |
102
|
|
|
$entity = $randomFixtureGenerator->generate($entityNamespace, $group, $version); |
103
|
|
|
$context = ContextFactory::createSerializationContextForFixtures($group, $version); |
104
|
|
|
|
105
|
|
|
$result = $serializer->serialize($entity, 'json', $context); |
106
|
|
|
if (!$rawOutput) { |
107
|
|
|
$this->out->writeln(''); |
108
|
|
|
$this->out->writeln('<info>Random fixture successfully generated.</info>'); |
109
|
|
|
$this->out->writeln(''); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->out->writeln($result); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|