Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

New "generate" command #13

Open
wants to merge 3 commits into from

4 participants

@weaverryan

Hey guys!

This is a basic generate command for generating a new fixture class. It's basic and has really just one option: whether or not it should be container aware.

Also, small updates for the other task.

Thanks!

@fspillner

Here, I'd expect, you call a static method, but renderFile is an instance method, hm?

@fspillner

I'd rename the filename as "fixturesClass.php.twig".

@fspillner

Well done, Ryan!

@weaverryan

Thanks Fabian! I've made your suggested changes and re-tested the command.

@stof
Collaborator

I'm going through the pending PRs of this repo (and I'm amazed about how old they can be). As I'm considering dropping the bundle for 2.1 (see my comment on #26), I think this command should probably be added in SensioGeneratorBundle instead

@lavoiesl
Collaborator

Wow, it is indeed very old… Moving it to SensioGeneratorBundle would be nice indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
113 Command/GenerateFixturesCommand.php
@@ -0,0 +1,113 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\DoctrineFixturesBundle\Command;
+
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Output\Output;
+use Symfony\Bundle\DoctrineBundle\Command\DoctrineCommand;
+use InvalidArgumentException;
+use Symfony\Component\HttpKernel\Bundle\BundleInterface;
+
+/**
+ * Generates a new fixtures class.
+ *
+ * @author Ryan Weaver <ryan@knplabs.com>
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class GenerateFixturesCommand extends DoctrineCommand
+{
+ protected function configure()
+ {
+ $this
+ ->setName('doctrine:fixtures:generate')
+ ->setDescription('Generates a new fixtures file.')
+ ->addOption('bundle', null, InputOption::VALUE_REQUIRED, 'The name of the bundle into which the fixture class will go.')
+ ->addOption('name', null, InputOption::VALUE_REQUIRED, 'The name for the fixture class (e.g. "LoadProducts").')
+ ->addOption('container-aware', null, InputOption::VALUE_NONE, 'Whether or not the fixture needs access to the container')
+ ->setHelp(<<<EOT
+The <info>doctrine:fixtures:generate</info> command generates a new fixture class:
+
+ <info>php app/console doctrine:fixtures:generate</info>
+
+By default, a wizard asks you about the fixture class that you'd like to create.
+You can also pass in the options manually:
+
+ <info>php app/console doctrine:fixtures:generate --bundle=AcmeStoreBundle --name=LoadProducts</info>
+EOT
+ );
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $dialog = $this->getDialogHelper();
+
+ $bundleName = $dialog->ask($output, '<comment>What bundle should the fixture be placed into?</comment> ', $input->getOption('bundle'));
+ $bundle = $this->getKernel()->getBundle($bundleName);
+
+ $name = $dialog->ask($output, '<comment>What should the class be named (e.g. LoadProducts)?</comment> ', $input->getOption('name'));
+ $containerAware = $dialog->askConfirmation($output, '<comment>Do you need access to the service container? (y/n)</comment> ', $input->getOption('container-aware'));
+
+ $target = $bundle->getPath().'/DataFixtures/ORM'.'/'.$name.'.php';
+ if (file_exists($target)) {
+ throw new InvalidArgumentException(sprintf('File already exists at "%s"', $target));
+ }
+
+ self::renderFile(
+ 'fixturesClass.php.twig',
+ $target,
+ array(
+ 'bundleNamespace' => $bundle->getNamespace(),
+ 'className' => $name,
+ 'containerAware' => $containerAware,
+ )
+ );
+
+ $output->writeln(sprintf('New fixture class written to <info>%s</info>', $target));
+ }
+
+ /**
+ * @return \Symfony\Component\Console\Helper\DialogHelper
+ */
+ private function getDialogHelper()
+ {
+ return $this->getHelperSet()->get('dialog');
+ }
+
+ /**
+ * @return \Symfony\Component\HttpKernel\KernelInterface
+ */
+ private function getKernel()
+ {
+ return $this->getApplication()->getKernel();
+ }
+
+ static protected function renderFile($template, $target, $parameters)
+ {
+ if (!is_dir(dirname($target))) {
+ mkdir(dirname($target), 0777, true);
+ }
+
+ $skeletonDir = __DIR__.'/../Resources/skeleton';
+
+ $twig = new \Twig_Environment(new \Twig_Loader_Filesystem($skeletonDir), array(
+ 'debug' => true,
+ 'cache' => false,
+ 'strict_variables' => true,
+ 'autoescape' => false,
+ ));
+
+ file_put_contents($target, $twig->render($template, $parameters));
+ }
+}
View
7 Command/LoadDataFixturesDoctrineCommand.php
@@ -17,7 +17,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Finder\Finder;
-use Symfony\Bundle\FrameworkBundle\Util\Filesystem;
use Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader as DataFixturesLoader;
use Symfony\Bundle\DoctrineBundle\Command\DoctrineCommand;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
@@ -46,15 +45,15 @@ protected function configure()
->setHelp(<<<EOT
The <info>doctrine:fixtures:load</info> command loads data fixtures from your bundles:
- <info>./app/console doctrine:fixtures:load</info>
+ <info>php app/console doctrine:fixtures:load</info>
You can also optionally specify the path to fixtures with the <info>--fixtures</info> option:
- <info>./app/console doctrine:fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2</info>
+ <info>php app/console doctrine:fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2</info>
If you want to append the fixtures instead of flushing the database first you can use the <info>--append</info> option:
- <info>./app/console doctrine:fixtures:load --append</info>
+ <info>php app/console doctrine:fixtures:load --append</info>
EOT
);
}
View
34 Resources/skeleton/fixturesClass.php.twig
@@ -0,0 +1,34 @@
+<?php
+
+namespace {{ bundleNamespace }}\DataFixtures\ORM;
+
+use Doctrine\Common\DataFixtures\FixtureInterface;
+{% if containerAware %}
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+{% endif %}
+
+class {{ className }} implements FixtureInterface{{ containerAware ? ', ContainerAwareInterface' : '' }}
+{
+{% if containerAware %}
+ /**
+ * @var ContainerInterface
+ */
+ private $container;
+
+ public function setContainer(ContainerInterface $container = null)
+ {
+ $this->container = $container;
+ }
+{% endif %}
+
+ public function load($manager)
+ {
+ // Example fixture code: replace with your own
+ // $product = new Product();
+ // $product->setName(\'test product\');
+ // $manager->persist($product);
+
+ // $manager->flush();
+ }
+}
Something went wrong with that request. Please try again.