Skip to content

Commit

Permalink
Merge pull request #130 from Palleas/load_from_bundle
Browse files Browse the repository at this point in the history
Add a way to load fixtures from a specific bundle
  • Loading branch information
willdurand committed Apr 6, 2012
2 parents e34f2a0 + 0f78f2b commit cfb7873
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Command/AbstractPropelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
45 changes: 42 additions & 3 deletions Command/FixturesLoadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,14 +44,19 @@ 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
*/
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',
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
}
}

0 comments on commit cfb7873

Please sign in to comment.