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