Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some basic CLI #49

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions 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

87 changes: 87 additions & 0 deletions lib/Doctrine/Common/DataFixtures/Command/Add.php
@@ -0,0 +1,87 @@
<?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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

* <http://www.doctrine-project.org>.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this and add the license header instead


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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be consistent with the naming used in the symfony2 bundle, it should be load, not 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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

),
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'
),
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the help of the command should be added too

}

protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
{
/**
* @todo find a better way to get the path?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which path ? you are getting the entity manager, not a 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to use else as you throw an exception.

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

$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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

}

}
}
}

}