Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add a way to load fixtures from a specific bundle #130

Merged
merged 5 commits into from

4 participants

@Palleas

Today I wanted to load a set of fixtures from a specific bundle. With this PR you'll be able to do :

$ app/console propel:fixtures:load @MySuperBundle

What do you think?

@havvg
Collaborator

I like the idea, but don't like the implementation, could you re-factor it into using the FileLocator?

@willdurand
Owner

:+1:

@hhamon

I don't like the @ before the name of the template. Can we just tell the bundle name instead?

@willdurand
Owner

@hhamon isn't a kind of convention used in other bundles?

@havvg
Collaborator

Is the actual path fixed, or do we want to specify a complete path to the fixtures directory.
Something like $ app/console propel:fixtures:load @AcmeBlogBundle/Resources/fixtures/demo? This would also allow to alter the fixtures of a bundle using bundle inheritance. The default fixtures directory will be unchanged, meaning app/propel/fixtures.

@hhamon

I have never seen the @ notation from a CLI command in Symfony...

@havvg
Collaborator

BehatBundle for example does it, to run features from a specific bundle.

@Palleas

Yep, the cool thing with Behat commands is running very specifics scenarios with @Bundle/featurefile:line (for example) but I'm not sure we need that.
I'm ok to use the file locator (even if I'm not sure why, because I'm only retrieving a path so it works just like if we'd used the -d option), but @havvg can you tell me more about what you don't like in this implementation?
I wanted to keep it very simple and directly look for Resources/fixtures folder, "convention over configuration" or something like that ;-)

@willdurand
Owner

Yeah, I like the @ notation, just update your PR using the FileLocator as @havvg said, and it will be ok.

@Palleas

I'm sorry but I'm not sure I see what you mean when you want me to use the FileLocator, as all I'm doing is retrieving a path. You want to get rid of the absoluteFixturesPath property ?

@havvg
Collaborator

There is a 'file_locator' service, which should be used to retrieve files/directories with a notation like you suggested, but the complete path. For example $this->container->get('file_locator')->locate('@AcmeBlogBundle/Resources/fixtures/demo/posts.yml'); which will yield the filename with bundle inheritance and other changes/customizations in mind. See https://github.com/propelorm/PropelBundle/blob/master/Command/AbstractPropelCommand.php#L234 for an example.

@hhamon

For your information, the doctrine:generate:entities command does not take the @ notation for specifying a bundle name.

@Palleas

I can remove it, I only had Behat in mind when I wrote this so...
I'll update this PR this afternoon, it's almost done :)

Romain Pouclet added some commits
Romain Pouclet Change 'getFileLocator' method's visibility to protected as it's need…
…ed in the FixtureLoadCommand class
41e147b
Romain Pouclet Use FileLocator 994f599
@Palleas

Updated :)

Command/FixturesLoadCommand.php
@@ -298,6 +312,21 @@ protected function getFixtureFiles($type = 'sql', $in = null)
$finder = new Finder();
$finder->sortByName()->name('*.' . $type);
- return $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
+ $files = $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
+
+ if (null === $this->bundle) {
+
@willdurand Owner

You should remove this blank line

@Palleas
Palleas added a note

I thought it was in the Symfony2's CS?

@willdurand Owner

yes it is, but just if you have code before I think..

@willdurand Owner

See the first code example: http://symfony.com/doc/current/contributing/code/standards.html the first return in the transform method doesn't have a blank line before.

@Palleas
Palleas added a note

Indeed, fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Command/FixturesLoadCommand.php
@@ -298,6 +312,21 @@ protected function getFixtureFiles($type = 'sql', $in = null)
$finder = new Finder();
$finder->sortByName()->name('*.' . $type);
- return $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
+ $files = $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
+
+ if (null === $this->bundle) {
+
+ return $files;
+ }
+
+ $finalFixtureFiles = array();
+
+ foreach ($files as $file) {
+ $fixtureFilePath = str_replace($this->bundle->getPath(). DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR, '', $file->getRealPath());
@willdurand Owner

You could probably avoid to write the same path twice (see line 131)

@willdurand Owner

perfect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Romain Pouclet added some commits
@willdurand willdurand merged commit cfb7873 into propelorm:master
@willdurand
Owner

Thanks!

@Palleas

Hiii, my first contribution to Propel \o/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 4, 2012
  1. Add a way to load fixtures from a specific bundle

    Romain Pouclet authored
Commits on Apr 6, 2012
  1. Change 'getFileLocator' method's visibility to protected as it's need…

    Romain Pouclet authored
    …ed in the FixtureLoadCommand class
  2. Use FileLocator

    Romain Pouclet authored
  3. Remove blank line

    Romain Pouclet authored
  4. Avoid duplicating fixtures path

    Romain Pouclet authored
This page is out of date. Refresh to see the latest.
View
2  Command/AbstractPropelCommand.php
@@ -505,7 +505,7 @@ private function transformToLogicalName(\SplFileInfo $schema, BundleInterface $b
return sprintf('@%s/Resources/config/%s', $bundle->getName(), $schemaPath);
}
- private function getFileLocator()
+ protected function getFileLocator()
{
return $this->getContainer()->get('file_locator');
}
View
45 Command/FixturesLoadCommand.php
@@ -16,6 +16,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
+use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Propel\PropelBundle\Command\AbstractPropelCommand;
use Propel\PropelBundle\DataFixtures\Loader\YamlDataLoader;
@@ -43,7 +44,11 @@ class FixturesLoadCommand extends AbstractPropelCommand
* @var \Symfony\Component\Filesystem\Filesystem
*/
private $filesystem = null;
-
+ /**
+ * Bundle the fixtures are being loaded from
+ * @var Symfony\Component\HttpKernel\Bundle\BundleInterface
+ */
+ private $bundle;
/**
* @see Command
*/
@@ -51,6 +56,7 @@ protected function configure()
{
$this
->setDescription('Load XML, SQL and/or YAML fixtures')
+ ->addArgument('bundle', InputArgument::OPTIONAL, 'The bundle to load fixtures from')
->addOption(
'dir', 'd', InputOption::VALUE_OPTIONAL,
'The directory where XML, SQL and/or YAML fixtures files are located',
@@ -117,7 +123,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->writeSection($output, '[Propel] You are running the command: propel:fixtures:load');
$this->filesystem = new Filesystem();
- $this->absoluteFixturesPath = realpath($this->getApplication()->getKernel()->getRootDir() . '/../' . $input->getOption('dir'));
+
+ if ('@' === substr($input->getArgument('bundle'), 0, 1)) {
+ $this->bundle = $this
+ ->getContainer()
+ ->get('kernel')
+ ->getBundle(substr($input->getArgument('bundle'), 1));
+ $this->absoluteFixturesPath = $this->getFixturesPath($this->bundle);
+ } else {
+ $this->absoluteFixturesPath = realpath($this->getApplication()->getKernel()->getRootDir() . '/../' . $input->getOption('dir'));
+ }
if ($input->getOption('verbose')) {
$this->additionalPhingArgs[] = 'verbose';
@@ -298,6 +313,30 @@ protected function getFixtureFiles($type = 'sql', $in = null)
$finder = new Finder();
$finder->sortByName()->name('*.' . $type);
- return $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
+ $files = $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
+
+ if (null === $this->bundle) {
+ return $files;
+ }
+
+ $finalFixtureFiles = array();
+
+ foreach ($files as $file) {
+ $fixtureFilePath = str_replace($this->getFixturesPath($this->bundle) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
+ $logicalName = sprintf('@%s/Resources/fixtures/%s', $this->bundle->getName(), $fixtureFilePath);
+ $finalFixtureFiles[] = new \SplFileInfo($this->getFileLocator()->locate($logicalName));
+ }
+
+ return new \ArrayIterator($finalFixtureFiles);
}
+
+ /**
+ * Returns the path the command will look into to find fixture files
+ *
+ * @return String
+ */
+ protected function getFixturesPath(BundleInterface $bundle)
+ {
+ return $bundle->getPath().DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'fixtures';
+ }
}
Something went wrong with that request. Please try again.