1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vivait\BootstrapBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class GenerateFixtureCommand extends ContainerAwareCommand |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->setName('generate:fixture') |
18
|
|
|
->setDescription('Generates a fixture') |
19
|
|
|
->addArgument('query', InputArgument::REQUIRED, 'What query do you want to create a fixture for?') |
20
|
|
|
->addOption('chain', null, InputOption::VALUE_NONE, 'If set, it will chain the method calls'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
24
|
|
|
{ |
25
|
|
|
$em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
26
|
|
|
|
27
|
|
|
$query = $em->createQuery($input->getArgument('query')); |
28
|
|
|
$chain = $input->getOption('chain'); |
|
|
|
|
29
|
|
|
$code = ''; |
30
|
|
|
|
31
|
|
|
foreach ($query->getResult() as $result) { |
32
|
|
|
$code .= $this->generateAliceFixture($result); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
file_put_contents($this->getContainer()->getParameter("kernel.cache_dir") . '/fixtures.yml', $code); |
36
|
|
|
|
37
|
|
|
$output->writeln($code); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function generateFixture($object, $chain = false) |
41
|
|
|
{ |
42
|
|
|
$reflection = new \ReflectionObject($object); |
43
|
|
|
|
44
|
|
|
$output = sprintf('$class = new \%s;%s', $reflection->getName(), PHP_EOL); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$is_first = true; |
47
|
|
|
|
48
|
|
|
foreach ($reflection->getMethods() as $method) { |
49
|
|
|
if (substr_compare($method->getName(), 'set', 0, 3) === 0) { |
|
|
|
|
50
|
|
|
$cut_name = substr($method->getName(), 3); |
|
|
|
|
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$getter = $reflection->getMethod('get' . $cut_name); |
54
|
|
|
} catch (\ReflectionException $e) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// There's a getter |
59
|
|
|
if ($getter) { |
60
|
|
|
$value = $getter->invoke($object); |
61
|
|
|
|
62
|
|
|
if ($value === null || $value === '') { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (is_object($value)) { |
67
|
|
|
$value_reflection = new \ReflectionObject($value); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$value_reflection->getMethod('__set_state'); |
71
|
|
|
} catch (\ReflectionException $e) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$output .= sprintf( |
77
|
|
|
'%s->%s(%s)%s', |
78
|
|
|
(($chain && !$is_first) ? "\t" : '$class'), |
79
|
|
|
$method->getName(), |
|
|
|
|
80
|
|
|
var_export($value, true), |
81
|
|
|
($chain ? '' : ';') . PHP_EOL |
82
|
|
|
); |
83
|
|
|
$is_first = false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($chain) { |
89
|
|
|
$output = substr($output, 0, strlen($output) - 1) . ';' . PHP_EOL; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$output .= '$manager->persist($class);' . PHP_EOL . PHP_EOL; |
93
|
|
|
|
94
|
|
|
return $output; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function generateAliceFixture($object, $name = 'district') |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$reflection = new \ReflectionObject($object); |
100
|
|
|
$array = []; |
101
|
|
|
|
102
|
|
|
$entity = $reflection->getName(); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$key = $reflection->getProperty($name); |
105
|
|
|
$key->setAccessible(true); |
106
|
|
|
$key = $key->getValue($object); |
107
|
|
|
|
108
|
|
|
foreach ($reflection->getProperties() as $property) { |
109
|
|
|
if (in_array($property->getName(), ['id'])) { |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$property->setAccessible(true); |
114
|
|
|
$value = $property->getValue($object); |
115
|
|
|
$value = $this->getYAMLValue($value); |
116
|
|
|
|
117
|
|
|
if ($value === null) { |
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$array[(string)$key][$property->getName()] = $value; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return Yaml::dump([$entity => $array], 3); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function getYAMLValue($value) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
if (is_array($value) || $value instanceOf \Traversable) { |
130
|
|
|
$new_value = []; |
131
|
|
|
foreach ($value as $foreign) { |
132
|
|
|
// TODO: Make this more flexible |
133
|
|
|
$new_value[] = $this->getYAMLValue($foreign); |
134
|
|
|
} |
135
|
|
|
return $new_value; |
136
|
|
|
} else if (is_object($value)) { |
137
|
|
|
return '@' . $value->getId(); |
138
|
|
|
} else { |
139
|
|
|
return $value; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.