forked from SkyFoxvn/PropelBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixturesLoadCommand.php
More file actions
326 lines (269 loc) · 10.8 KB
/
Copy pathFixturesLoadCommand.php
File metadata and controls
326 lines (269 loc) · 10.8 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Bundle\PropelBundle\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
/**
* FixturesLoadCommand
*
* @author William DURAND <william.durand1@gmail.com>
*/
class FixturesLoadCommand extends AbstractCommand
{
/**
* Default fixtures directory.
*/
private string $defaultFixturesDir = 'propel/fixtures';
/**
* Absolute path for fixtures directory
*/
private string $absoluteFixturesPath = '';
/**
* Filesystem for manipulating files
*/
private ?\Symfony\Component\Filesystem\Filesystem $filesystem = null;
/**
* @see Command
*/
protected function configure(): void
{
$this
->setName('propel:fixtures:load')
->setDescription('Load XML, SQL and/or YAML fixtures')
->setHelp(<<<EOT
The <info>propel:fixtures:load</info> loads <info>XML</info>, <info>SQL</info> and/or <info>YAML</info> fixtures.
<info>php app/console propel:fixtures:load</info>
The <info>--connection</info> parameter allows you to change the connection to use.
The default connection is the active connection (propel.dbal.default_connection).
The <info>--dir</info> parameter allows you to change the directory that contains <info>XML</info> or/and <info>SQL</info> fixtures files <comment>(default: propel/fixtures)</comment>.
The <info>--xml</info> parameter allows you to load only <info>XML</info> fixtures.
The <info>--sql</info> parameter allows you to load only <info>SQL</info> fixtures.
The <info>--yml</info> parameter allows you to load only <info>YAML</info> fixtures.
You can mix <info>--xml</info>, <info>--sql</info> and <info>--yml</info> parameters to load XML, YAML and SQL fixtures at the same time.
If none of this parameter is set, all XML, YAML and SQL files in the directory will be load.
XML fixtures files are the same XML files you can get with the command <info>propel:data-dump</info>:
<comment>
<Fixtures>
<Object Namespace="Awesome">
<o1 Title="My title" MyFoo="bar" />
</Object>
<Related Namespace="Awesome">
<r1 ObjectId="o1" Description="Hello world !" />
</Related>
</Fixtures>
</comment>
YAML fixtures are:
<comment>
\Awesome\Object:
o1:
Title: My title
MyFoo: bar
\Awesome\Related:
r1:
ObjectId: o1
Description: Hello world !
</comment>
EOT
)
->addArgument('bundle', InputArgument::OPTIONAL, 'The bundle to load fixtures from')
->addOption(
'dir', 'd', InputOption::VALUE_OPTIONAL,
'The directory where XML, SQL and/or YAML fixtures files are located',
$this->defaultFixturesDir
)
->addOption('xml', '', InputOption::VALUE_NONE, 'Load XML fixtures')
->addOption('sql', '', InputOption::VALUE_NONE, 'Load SQL fixtures')
->addOption('yml', '', InputOption::VALUE_NONE, 'Load YAML fixtures')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->filesystem = new Filesystem();
if (null !== $this->bundle) {
$this->absoluteFixturesPath = $this->getFixturesPath($this->bundle);
} else {
$this->absoluteFixturesPath = realpath($this->getKernel()->getProjectDir() . '/' . $input->getOption('dir'));
}
if (!$this->absoluteFixturesPath && !file_exists($this->absoluteFixturesPath)) {
$this->writeSection($output, array(
'The fixtures directory "' . $this->absoluteFixturesPath . '" does not exist.'
), 'fg=white;bg=red');
return \Propel\Generator\Command\AbstractCommand::CODE_ERROR;
}
$noOptions = !$input->getOption('xml') && !$input->getOption('sql') && !$input->getOption('yml');
if ($input->getOption('sql') || $noOptions) {
if (-1 === $this->loadSqlFixtures($input, $output)) {
$output->writeln('No <info>SQL</info> fixtures found.');
}
}
if ($input->getOption('xml') || $noOptions) {
if (-1 === $this->loadFixtures($input, $output, 'xml')) {
$output->writeln('No <info>XML</info> fixtures found.');
}
}
if ($input->getOption('yml') || $noOptions) {
if (-1 === $this->loadFixtures($input, $output, 'yml')) {
$output->writeln('No <info>YML</info> fixtures found.');
}
}
return \Propel\Generator\Command\AbstractCommand::CODE_SUCCESS;
}
/**
* Load fixtures
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param string|null $type If specified, only fixtures with the given type will be loaded (yml, xml).
*
* @return bool|int|void
*/
protected function loadFixtures(InputInterface $input, OutputInterface $output, ?string $type = null)
{
if (null === $type) {
return;
}
$datas = $this->getFixtureFiles($type);
if (count(iterator_to_array($datas)) === 0) {
return -1;
}
$connectionName = $input->getOption('connection') ?: $this->getDefaultConnection();
if ('yml' === $type) {
$loader = $this->getContainer()->get('propel.loader.yaml');
} elseif ('xml' === $type) {
$loader = $this->getContainer()->get('propel.loader.xml');
} else {
return;
}
$nb = $loader->load(iterator_to_array($datas), $connectionName);
$output->writeln(sprintf('<comment>%s</comment> %s fixtures file%s loaded.', $nb, strtoupper($type), $nb > 1 ? 's' : ''));
return true;
}
/**
* Load SQL fixtures
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int
*/
protected function loadSqlFixtures(InputInterface $input, OutputInterface $output): int
{
$tmpdir = $this->getCacheDir();
$datas = $this->getFixtureFiles('sql');
$this->prepareCache($tmpdir);
$connectionName = $input->getOption('connection') ?: $this->getContainer()->getParameter('propel.dbal.default_connection');
// Create a "sqldb.map" file
$sqldbContent = '';
foreach ($datas as $data) {
$output->writeln(sprintf('<info>Loading SQL fixtures from</info> <comment>%s</comment>.', $data));
$sqldbContent .= $data->getFilename() . '=' . $connectionName . PHP_EOL;
$this->filesystem->copy($data, $tmpdir . '/' . $data->getFilename(), true);
}
if ('' === $sqldbContent) {
return -1;
}
$sqldbFile = $tmpdir . '/sqldb.map';
file_put_contents($sqldbFile, $sqldbContent);
if (!$this->insertSql($connectionName, $input, $output)) {
return -1;
}
$this->filesystem->remove($tmpdir);
return 0;
}
/**
* Prepare the cache directory
*
* @param string $tmpdir The temporary directory path.
*/
protected function prepareCache(string $tmpdir): void
{
// Recreate a propel directory in cache
$this->filesystem->remove($tmpdir);
$this->filesystem->mkdir($tmpdir);
}
/**
* Insert SQL
*/
protected function insertSql(string $connectionName, InputInterface $input, OutputInterface $output): bool
{
$parameters = array(
'--connection' => array($connectionName),
'--verbose' => $input->getOption('verbose'),
'--sql-dir' => $this->getCacheDir(),
'--force' => 'force'
);
// add the command's name to the parameters
array_unshift($parameters, $this->getName());
$command = $this->getApplication()->find('propel:sql:insert');
$command->setApplication($this->getApplication());
// and run the sub-command
$ret = $command->run(new ArrayInput($parameters), $output);
if ($ret === 0) {
$output->writeln('All SQL statements have been inserted.');
} else {
$this->writeTaskError($output, 'insert-sql', false);
}
return $ret === 0;
}
/**
* Returns the fixtures files to load.
*
* @param string $type The extension of the files.
* @param string|null $in The directory in which we search the files. If null,
* we'll use the absoluteFixturesPath property.
*
* @return \ArrayIterator<int, \SplFileInfo>|Finder
*/
protected function getFixtureFiles(string $type = 'sql', ?string $in = null)
{
$finder = new Finder();
$finder->sort(function ($a, $b) {
return strcmp($a->getPathname(), $b->getPathname());
})->name('*.' . $type);
$files = $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
if (null === $this->bundle) {
return $files;
}
$finalFixtureFiles = array();
foreach ($files as $file) {
$fixtureFilePath = str_replace($this->getFixturesPath($this->bundle) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
$logicalName = sprintf('@%s/Resources/fixtures/%s', $this->bundle->getName(), $fixtureFilePath);
$finalFixtureFiles[] = new \SplFileInfo($this->getFileLocator()->locate($logicalName));
}
return new \ArrayIterator($finalFixtureFiles);
}
/**
* Returns the path the command will look into to find fixture files
*
* @param BundleInterface $bundle The bundle to explore.
*
* @return String
*/
protected function getFixturesPath(BundleInterface $bundle)
{
return $bundle->getPath() . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'fixtures';
}
/**
* @return \Symfony\Component\Config\FileLocatorInterface
*/
protected function getFileLocator()
{
return $this->getContainer()->get('file_locator');
}
}