From e8c3aa69441a6dc265543bd5a1c978d19d25dda3 Mon Sep 17 00:00:00 2001 From: Glynn Forrest Date: Tue, 21 Jul 2015 12:45:45 +0100 Subject: [PATCH] Adding database:fixtures:run command. --- .../Command/DatabaseFixturesRunCommand.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/Neptune/Command/DatabaseFixturesRunCommand.php diff --git a/src/Neptune/Command/DatabaseFixturesRunCommand.php b/src/Neptune/Command/DatabaseFixturesRunCommand.php new file mode 100644 index 0000000..e1920c5 --- /dev/null +++ b/src/Neptune/Command/DatabaseFixturesRunCommand.php @@ -0,0 +1,90 @@ + + **/ +class DatabaseFixturesRunCommand extends DatabaseMigrateListCommand +{ + protected $name = 'database:fixtures:run'; + protected $description = 'Run fixtures'; + + protected function configure() + { + parent::configure(); + $this->addOption( + 'module', + 'm', + InputOption::VALUE_REQUIRED, + 'Only run fixtures for a certain module' + )->addOption( + 'append', + '', + InputOption::VALUE_NONE, + 'Don\'t empty database tables' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $db = $this->neptune['db']; + $loader = new FixtureLoader(); + $loader->setLogger(new ConsoleLogger($output)); + + $fixtures = $this->getAllFixtures(); + $count = 0; + foreach ($fixtures as $fixture) { + ++$count; + $loader->addFixture($fixture); + } + + $loader->run($db, $input->getOption('append')); + $inflection = $count === 1 ? 'fixture' : 'fixtures'; + $output->writeln(sprintf('Ran %s %s.', $count, $inflection)); + } + + protected function getModuleFixtures(AbstractModule $module) + { + $namespace = $module->getNamespace().'\\Fixtures\\'; + $directory = $module->getDirectory().'Fixtures/'; + if (!is_dir($directory)) { + return []; + } + + $fixtures = []; + $files = new \DirectoryIterator($directory); + foreach ($files as $file) { + if (!$file->isFile() || substr($file->getFilename(), -4) !== '.php') { + continue; + } + $class = $namespace.$file->getBasename('.php'); + $r = new \ReflectionClass($class); + if (!$r->isSubclassOf('ActiveDoctrine\\Fixture\\FixtureInterface') || $r->isAbstract()) { + continue; + } + $fixtures[] = $r->newInstance(); + } + + return $fixtures; + } + + protected function getAllFixtures() + { + $fixtures = []; + foreach ($this->neptune->getModules() as $module) { + $fixtures = array_merge($fixtures, $this->getModuleFixtures($module)); + } + + return $fixtures; + } +}