|
58
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\DoctrineFixturesBundle\Command; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Console\Input\InputArgument; |
|
|
15 |
use Symfony\Component\Console\Input\InputOption; |
|
|
16 |
use Symfony\Component\Console\Input\InputInterface; |
|
|
17 |
use Symfony\Component\Console\Output\OutputInterface; |
|
|
18 |
use Symfony\Component\Console\Output\Output; |
|
|
19 |
use Symfony\Component\Finder\Finder; |
|
|
20 |
use Symfony\Bundle\FrameworkBundle\Util\Filesystem; |
|
|
21 |
use Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader as DataFixturesLoader; |
|
|
22 |
use Symfony\Bundle\DoctrineBundle\Command\DoctrineCommand; |
|
|
23 |
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
|
|
24 |
use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
|
|
25 |
use Doctrine\ORM\EntityManager; |
|
|
26 |
use Doctrine\ORM\Internal\CommitOrderCalculator; |
|
|
27 |
use Doctrine\ORM\Mapping\ClassMetadata; |
|
|
28 |
use InvalidArgumentException; |
|
|
29 |
|
|
|
30 |
/** |
|
|
31 |
* Load data fixtures from bundles. |
|
|
32 |
* |
|
|
33 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
34 |
* @author Jonathan H. Wage <jonwage@gmail.com> |
|
|
35 |
*/ |
|
|
36 |
class LoadDataFixturesDoctrineCommand extends DoctrineCommand |
|
|
37 |
{ |
|
|
38 |
protected function configure() |
|
|
39 |
{ |
|
|
40 |
$this |
|
|
41 |
->setName('doctrine:fixtures:load') |
|
|
42 |
->setDescription('Load data fixtures to your database.') |
|
|
43 |
->addOption('fixtures', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The directory or file to load data fixtures from.') |
|
|
44 |
->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of deleting all data from the database first.') |
|
|
45 |
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') |
|
|
46 |
->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Purge data by using a database-level TRUNCATE statement') |
|
|
47 |
->setHelp(<<<EOT |
|
|
48 |
The <info>doctrine:fixtures:load</info> command loads data fixtures from your bundles: |
|
|
49 |
|
|
|
50 |
<info>./app/console doctrine:fixtures:load</info> |
|
|
51 |
|
|
|
52 |
You can also optionally specify the path to fixtures with the <info>--fixtures</info> option: |
|
|
53 |
|
|
|
54 |
<info>./app/console doctrine:fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2</info> |
|
|
55 |
|
|
|
56 |
If you want to append the fixtures instead of flushing the database first you can use the <info>--append</info> option: |
|
|
57 |
|
|
|
58 |
<info>./app/console doctrine:fixtures:load --append</info> |
|
|
59 |
|
|
|
60 |
By default Doctrine Data Fixtures uses DELETE statements to drop the existing rows from |
|
|
61 |
the database. If you want to use a TRUNCATE statement instead you can use the <info>--purge-with-truncate</info> flag: |
|
|
62 |
|
|
|
63 |
<info>./app/console doctrine:fixtures:load --purge-with-truncate</info> |
|
|
64 |
EOT |
|
|
65 |
); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
69 |
{ |
|
|
70 |
$emName = $input->getOption('em'); |
|
|
71 |
$emName = $emName ? $emName : 'default'; |
|
|
72 |
$emServiceName = sprintf('doctrine.orm.%s_entity_manager', $emName); |
|
|
73 |
|
|
|
74 |
if (!$this->getContainer()->has($emServiceName)) { |
|
|
75 |
throw new InvalidArgumentException( |
|
|
76 |
sprintf( |
|
|
77 |
'Could not find an entity manager configured with the name "%s". Check your '. |
|
|
78 |
'application configuration to configure your Doctrine entity managers.', $emName |
|
|
79 |
) |
|
|
80 |
); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
$em = $this->getContainer()->get($emServiceName); |
|
|
84 |
$dirOrFile = $input->getOption('fixtures'); |
|
|
85 |
if ($dirOrFile) { |
|
|
86 |
$paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); |
|
|
87 |
} else { |
|
|
88 |
$paths = array(); |
|
|
89 |
foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { |
|
|
90 |
$paths[] = $bundle->getPath().'/DataFixtures/ORM'; |
|
|
91 |
} |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
$loader = new DataFixturesLoader($this->getContainer()); |
|
|
95 |
foreach ($paths as $path) { |
|
|
96 |
if (is_dir($path)) { |
|
|
97 |
$loader->loadFromDirectory($path); |
|
|
98 |
} |
|
|
99 |
} |
|
|
100 |
$fixtures = $loader->getFixtures(); |
|
|
101 |
if (!$fixtures) { |
|
|
102 |
throw new InvalidArgumentException( |
|
|
103 |
sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $paths)) |
|
|
104 |
); |
|
|
105 |
} |
|
|
106 |
$purger = new ORMPurger($em); |
|
|
107 |
$purger->setPurgeMode($input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE); |
|
|
108 |
$executor = new ORMExecutor($em, $purger); |
|
|
109 |
$executor->setLogger(function($message) use ($output) { |
|
|
110 |
$output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); |
|
|
111 |
}); |
|
|
112 |
$executor->execute($fixtures, $input->getOption('append')); |
|
|
113 |
} |
|
|
114 |
} |