Added some basic CLI #49

Open
wants to merge 4 commits into
from

Projects

None yet

7 participants

@lsv
lsv commented Apr 3, 2012

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
Martin Aarhof added some commits Apr 3, 2012
@stof stof commented on an outdated diff Apr 20, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
@@ -0,0 +1,81 @@
+<?php
+/**
+ * Created by JetBrains PhpStorm.
+ * User: lsv
+ * Date: 4/3/12
+ * Time: 4:38 PM
+ * To change this template use File | Settings | File Templates.
+ */
@stof
stof Apr 20, 2012 Member

please remove this and add the license header instead

@stof stof commented on an outdated diff Apr 20, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ * Date: 4/3/12
+ * Time: 4:38 PM
+ * To change this template use File | Settings | File Templates.
+ */
+
+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;
+
+class Add
+ extends Console\Command\Command
@stof
stof Apr 20, 2012 Member

please keep it on the same line

@stof stof commented on an outdated diff Apr 20, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+
+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;
+
+class Add
+ extends Console\Command\Command
+{
+
+ protected function configure()
+ {
+ $this
+ ->setName('fixtures:add')
@stof
stof Apr 20, 2012 Member

please use 4 spaces per level for the indentation instead of tabs

@stof stof commented on an outdated diff Apr 20, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ {
+ $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'
@stof
stof Apr 20, 2012 Member

typo here

@stof stof commented on an outdated diff Apr 20, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ '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()) . '/';
@stof
stof Apr 20, 2012 Member

this variable is not used

@stof stof commented on an outdated diff Apr 20, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+
+ 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($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));
@stof
stof Apr 20, 2012 Member

you should use InvalidArgumentException

@stof stof commented on an outdated diff Apr 20, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ $dir = realpath($em->getConfiguration()->getProxyDir()) . '/';
+
+ 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('dump-fixtures') === true) {
+ throw new \Exception('Dumping fixtures is not implemented yet');
@stof
stof Apr 20, 2012 Member

if it is not implemented, why adding the option in the command ?

@stof stof commented on the diff Aug 7, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+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;
+
+class Add extends Console\Command\Command
+{
+
+ protected function configure()
+ {
+ $this
+ ->setName('fixtures:add')
@stof
stof Aug 7, 2012 Member

to be consistent with the naming used in the symfony2 bundle, it should be load, not add.

@stof stof commented on the diff Aug 7, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ ),
+ 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?
@stof
stof Aug 7, 2012 Member

which path ? you are getting the entity manager, not a path

@stof stof commented on the diff Aug 7, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ Doctrine\Common\DataFixtures\Loader,
+ Doctrine\Common\DataFixtures\Executor\ORMExecutor,
+ Doctrine\Common\DataFixtures\Purger\ORMPurger;
+
+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'
@stof
stof Aug 7, 2012 Member

why relative to the proxy path ? Fixtures have nothing to do with proxies

@stof stof commented on the diff Aug 7, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ '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 */
+
+ if (!($input->getOption('directory'))) {
+ $output->write($this->getSynopsis() . PHP_EOL);
+ throw new \InvalidArgumentException('You need to input the directory');
+ } else {
@stof
stof Aug 7, 2012 Member

no need to use else as you throw an exception.

and if it is mandatory, it should be an argument, not an option.

@stof stof commented on the diff Aug 7, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ $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('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());
@stof
stof Aug 7, 2012 Member

why duplication the same logic when all you need to have as difference is using $executor->execute($loader->getFixtures(), $input->getOption('append')); ?

@stof stof commented on the diff Aug 7, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+ ->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'
+ ),
+ ));
@stof
stof Aug 7, 2012 Member

the help of the command should be added too

@stof stof commented on the diff Aug 7, 2012
lib/Doctrine/Common/DataFixtures/Command/Add.php
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
@stof
stof Aug 7, 2012 Member

This is wrong. The library is using the MIT license.

@stof
Member
stof commented Aug 7, 2012

Thus, I think the command should provide some protected method able to be overwritten to modify it. currently, you have to duplicate it entirely if you want to provide a command for MongoDB or PHPCR, and the DoctrineFixturesBundle cannot use it either to avoid duplicating the logic.

@jmikola
Member
jmikola commented Aug 7, 2012

I believe all the bundles currently have commands, correct? Is this modeled after the ORM bundle's command? The structure certainly differs from LoadDataFixturesDoctrineODMCommand.

@stof
Member
stof commented Aug 7, 2012

@jmikola yeah, currently, they all duplicate the whole command, which is why I already thought about providing a base class in the DataFixtures library (but without time to actually do it)

@lavoiesl
Member

Shouldn’t this command be called fixtures:load to avoid confusion?

@lavoiesl lavoiesl added this to the 1.1 milestone Mar 23, 2015
@lavoiesl lavoiesl modified the milestone: 1.2, 1.1 Mar 23, 2015
This was referenced Mar 23, 2015
Closed
@L0rD59
L0rD59 commented Jun 17, 2015

👍

@Ocramius Ocramius modified the milestone: 1.2 Jun 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment