From 9aca0bfaa84363e34b566389eafebc299148cc13 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Tue, 19 Apr 2011 14:18:42 +0200 Subject: [PATCH] Added new command: load-fixtures --- Command/LoadFixturesCommand.php | 108 ++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Command/LoadFixturesCommand.php diff --git a/Command/LoadFixturesCommand.php b/Command/LoadFixturesCommand.php new file mode 100644 index 0000000..ad508fe --- /dev/null +++ b/Command/LoadFixturesCommand.php @@ -0,0 +1,108 @@ + + */ +class LoadFixturesCommand extends PhingCommand +{ + /** + * Default fixtures directory. + */ + private $defaultFixturesDir = 'propel/fixtures'; + + /** + * @see Command + */ + protected function configure() + { + $this + ->setDescription('Load XML fixtures') + ->addOption('dir', 'd', InputOption::VALUE_REQUIRED, 'The directory where XML fixtures files are located') + ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use') + ->setHelp(<<propel:load-fixtures loads XML fixtures. + + php app/console propel:load-fixtures + +The --connection parameter allows you to change the connection to use. +The default connection is the active connection (propel.dbal.default_connection). + +The --dir parameter allows you to change the directory that contains XML fixtures files (default: app/propel/fixtures). + +XML fixtures files are the same XML files you can get with the command propel:data-dump: + + + + + + +EOT + ) + ->setName('propel:load-fixtures') + ; + } + + /** + * @see Command + * + * @throws \InvalidArgumentException When the target directory does not exist + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $defaultConfig = $this->getConnection($input, $output); + + $finder = new Finder(); + $filesystem = new Filesystem(); + $dir = $input->getOption('dir') ?: $this->defaultFixturesDir; + $fixturesDir = $this->getApplication()->getKernel()->getRootDir() . '/' . $dir; + + // Create a "datadb.map" file + $datadbContent = ''; + $datas = $finder->name('*.xml')->in($fixturesDir); + foreach($datas as $data) { + $output->writeln(sprintf('Loaded fixtures from "%s".', $data)); + $datadbContent .= $data->getFilename() . '=default' . "\n"; + } + + $datadbFile = $fixturesDir . '/datadb.map'; + file_put_contents($datadbFile, $datadbContent); + + $dest = $this->getApplication()->getKernel()->getRootDir() . '/propel/sql/'; + $this->callPhing('datasql', array( + 'propel.sql.dir' => $dest, + 'propel.schema.dir' => $fixturesDir, + )); + + // Insert SQL + $insertCommand = new InsertSqlCommand(); + $insertCommand->setApplication($this->getApplication()); + + // By-pass the '--force' required option + $this->addOption('--force', '', InputOption::VALUE_NONE, ''); + $input->setOption('force', true); + + $insertCommand->execute($input, $output); + + // Delete temporary files + $finder = new Finder(); + $datas = $finder->name('*_schema.xml')->name('build*')->in($fixturesDir); + foreach($datas as $data) { + $filesystem->remove($data); + } + + $filesystem->remove($datadbFile); + + $output->writeln('Fixtures successfully loaded.'); + } +}