From b3a13a08105e626b3392562b9cae6231f47a06f5 Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Tue, 3 Apr 2012 17:39:57 +0200 Subject: [PATCH 1/3] added commandline to cli users --- .../Common/DataFixtures/Command/Add.php | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 lib/Doctrine/Common/DataFixtures/Command/Add.php diff --git a/lib/Doctrine/Common/DataFixtures/Command/Add.php b/lib/Doctrine/Common/DataFixtures/Command/Add.php new file mode 100644 index 00000000..7827e95c --- /dev/null +++ b/lib/Doctrine/Common/DataFixtures/Command/Add.php @@ -0,0 +1,80 @@ +setName('fixtures:add') + ->setDescription('Adding fixtures to your database') + ->setDefinition(array( + new InputOption( + 'directory', 'd', InputOption::VALUE_REQUIRED, + 'Directory with your fixtures - Its relative to your Entities\Proxies path' + ), + new InputOption( + 'append', 'a', InputOption::VALUE_NONE, + 'If you want to append your fixtures instead' + ), + new InputOption( + 'dump-fixtures', null, InputOption::VALUE_NONE, + 'Vieweing the fixtures instead of importing them' + ), + )); + } + + protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) + { + /** + * @todo find a better way to get the path? + */ + $em = $this->getHelper('em')->getEntityManager(); /** @var $em \Doctrine\ORM\EntityManager */ + $dir = realpath($em->getConfiguration()->getProxyDir()) . '/'; + + if (!($input->getOption('directory'))) { + $output->write('You need to input the directory' . PHP_EOL); + } else { + $dir = realpath($dir . $input->getOption('directory') . '/'); + if (! is_dir($dir)) { + $output->write('The inputted "' . $dir . '" is not a directory.' . PHP_EOL); + } else { + $loader = new Loader(); + $loader->loadFromDirectory($dir); + $fixtures = $loader->getFixtures(); + + if ($input->getOption('dump-fixtures') === true) { + $output->writeln($fixtures . PHP_EOL); + } elseif ($input->getOption('append') === true) { + $purger = new ORMPurger(); + $executor = new ORMExecutor($em, $purger); + $executor->execute($loader->getFixtures(), TRUE); + } else { + $purger = new ORMPurger(); + $executor = new ORMExecutor($em, $purger); + $executor->execute($loader->getFixtures()); + } + + } + } + } + +} + From f9b09cfb5bbde736318935bf16abddd84e82cb5e Mon Sep 17 00:00:00 2001 From: Martin Aarhof Date: Tue, 3 Apr 2012 17:59:18 +0200 Subject: [PATCH 2/3] changed a bit in the cli --- CLI-README.md | 25 +++++++++++++++++++ .../Common/DataFixtures/Command/Add.php | 7 +++--- 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 CLI-README.md diff --git a/CLI-README.md b/CLI-README.md new file mode 100644 index 00000000..2e420d2f --- /dev/null +++ b/CLI-README.md @@ -0,0 +1,25 @@ +# Command line interface (CLI) + +You will need to edit a Doctrine file for this to work. +Goto +Doctrine/ORM/Tools/Console/ConsoleRunner.php + +Add this + + // Fixtures + new \Doctrine\Common\DataFixtures\Command\Add(), + +after + + new \Doctrine\ORM\Tools\Console\Command\InfoCommand(), + +Now you can use + + ./doctrine fixtures:add + +## Options to fixtures:add + + --directory (-d) Directory with your fixtures - Its relative to your Entities\Proxies path *REQUIRED* + --append (-a) If you want to append your fixtures instead + --dump-fixtures Vieweing the fixtures instead of importing them + diff --git a/lib/Doctrine/Common/DataFixtures/Command/Add.php b/lib/Doctrine/Common/DataFixtures/Command/Add.php index 7827e95c..5c3bb713 100644 --- a/lib/Doctrine/Common/DataFixtures/Command/Add.php +++ b/lib/Doctrine/Common/DataFixtures/Command/Add.php @@ -50,18 +50,19 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O $dir = realpath($em->getConfiguration()->getProxyDir()) . '/'; if (!($input->getOption('directory'))) { - $output->write('You need to input the directory' . PHP_EOL); + $output->write($this->getSynopsis() . PHP_EOL); + throw new \Exception('You need to input the directory'); } else { $dir = realpath($dir . $input->getOption('directory') . '/'); if (! is_dir($dir)) { - $output->write('The inputted "' . $dir . '" is not a directory.' . PHP_EOL); + throw new \Exception(sprintf('The inputted "%s" is not a directory', $dir)); } else { $loader = new Loader(); $loader->loadFromDirectory($dir); $fixtures = $loader->getFixtures(); if ($input->getOption('dump-fixtures') === true) { - $output->writeln($fixtures . PHP_EOL); + throw new \Exception('Dumping fixtures is not implemented yet'); } elseif ($input->getOption('append') === true) { $purger = new ORMPurger(); $executor = new ORMExecutor($em, $purger); From f1a1dc5174f000b33bf2713c3527a2160227437f Mon Sep 17 00:00:00 2001 From: Damian Boune Date: Sat, 2 Jun 2012 00:58:08 -0700 Subject: [PATCH 3/3] Implement requested changes --- .../Common/DataFixtures/Command/Add.php | 136 +++++++++--------- 1 file changed, 71 insertions(+), 65 deletions(-) diff --git a/lib/Doctrine/Common/DataFixtures/Command/Add.php b/lib/Doctrine/Common/DataFixtures/Command/Add.php index 5c3bb713..490d6756 100644 --- a/lib/Doctrine/Common/DataFixtures/Command/Add.php +++ b/lib/Doctrine/Common/DataFixtures/Command/Add.php @@ -1,81 +1,87 @@ . */ namespace Doctrine\Common\DataFixtures\Command; use Symfony\Component\Console\Input\InputArgument, - Symfony\Component\Console\Input\InputOption, - Symfony\Component\Console, - Doctrine\Common\DataFixtures\Loader, - Doctrine\Common\DataFixtures\Executor\ORMExecutor, - Doctrine\Common\DataFixtures\Purger\ORMPurger; + Symfony\Component\Console\Input\InputOption, + Symfony\Component\Console, + Doctrine\Common\DataFixtures\Loader, + Doctrine\Common\DataFixtures\Executor\ORMExecutor, + Doctrine\Common\DataFixtures\Purger\ORMPurger; -class Add - extends Console\Command\Command +class Add extends Console\Command\Command { - protected function configure() - { - $this - ->setName('fixtures:add') - ->setDescription('Adding fixtures to your database') - ->setDefinition(array( - new InputOption( - 'directory', 'd', InputOption::VALUE_REQUIRED, - 'Directory with your fixtures - Its relative to your Entities\Proxies path' - ), - new InputOption( - 'append', 'a', InputOption::VALUE_NONE, - 'If you want to append your fixtures instead' - ), - new InputOption( - 'dump-fixtures', null, InputOption::VALUE_NONE, - 'Vieweing the fixtures instead of importing them' - ), - )); - } + protected function configure() + { + $this + ->setName('fixtures:add') + ->setDescription('Adding fixtures to your database') + ->setDefinition(array( + new InputOption( + 'directory', 'd', InputOption::VALUE_REQUIRED, + 'Directory with your fixtures - Its relative to your Entities\Proxies path' + ), + new InputOption( + 'append', 'a', InputOption::VALUE_NONE, + 'If you want to append your fixtures instead' + ), + new InputOption( + 'dump-fixtures', null, InputOption::VALUE_NONE, + 'Viewing the fixtures instead of importing them' + ), + )); + } - protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) - { - /** - * @todo find a better way to get the path? - */ - $em = $this->getHelper('em')->getEntityManager(); /** @var $em \Doctrine\ORM\EntityManager */ - $dir = realpath($em->getConfiguration()->getProxyDir()) . '/'; + protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) + { + /** + * @todo find a better way to get the path? + */ + $em = $this->getHelper('em')->getEntityManager(); /** @var $em \Doctrine\ORM\EntityManager */ - if (!($input->getOption('directory'))) { - $output->write($this->getSynopsis() . PHP_EOL); - throw new \Exception('You need to input the directory'); - } else { - $dir = realpath($dir . $input->getOption('directory') . '/'); - if (! is_dir($dir)) { - throw new \Exception(sprintf('The inputted "%s" is not a directory', $dir)); - } else { - $loader = new Loader(); - $loader->loadFromDirectory($dir); - $fixtures = $loader->getFixtures(); + if (!($input->getOption('directory'))) { + $output->write($this->getSynopsis() . PHP_EOL); + throw new \InvalidArgumentException('You need to input the directory'); + } else { + $dir = realpath($em->getConfiguration()->getProxyDir() . '/' . $input->getOption('directory') . '/'); + if (! is_dir($dir)) { + throw new \InvalidArgumentException(sprintf('The inputted "%s" is not a directory', $dir)); + } else { + $loader = new Loader(); + $loader->loadFromDirectory($dir); + $fixtures = $loader->getFixtures(); - if ($input->getOption('dump-fixtures') === true) { - throw new \Exception('Dumping fixtures is not implemented yet'); - } elseif ($input->getOption('append') === true) { - $purger = new ORMPurger(); - $executor = new ORMExecutor($em, $purger); - $executor->execute($loader->getFixtures(), TRUE); - } else { - $purger = new ORMPurger(); - $executor = new ORMExecutor($em, $purger); - $executor->execute($loader->getFixtures()); - } + if ($input->getOption('append') === true) { + $purger = new ORMPurger(); + $executor = new ORMExecutor($em, $purger); + $executor->execute($loader->getFixtures(), TRUE); + } else { + $purger = new ORMPurger(); + $executor = new ORMExecutor($em, $purger); + $executor->execute($loader->getFixtures()); + } - } - } - } + } + } + } }