Avoid injecting the container in the YamlDataLoader

This commit is contained in:
Kévin Gomez 2014-04-26 18:01:25 +01:00
parent c1eebcf61b
commit d1ef814d57
3 changed files with 20 additions and 20 deletions

View file

@ -10,6 +10,7 @@
namespace Propel\PropelBundle\DataFixtures\Loader;
use Faker\Generator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\ParseException;
use Symfony\Component\Yaml\Yaml;
@ -22,18 +23,18 @@ use Symfony\Component\Yaml\Yaml;
class YamlDataLoader extends AbstractDataLoader
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
* @var \Faker\Generator
*/
private $container;
private $faker;
/**
* {@inheritdoc}
*/
public function __construct($rootDir, ContainerInterface $container)
public function __construct($rootDir, array $datasources, Generator $faker = null)
{
parent::__construct($rootDir, $container->getParameter('propel.configuration'));
parent::__construct($rootDir, $datasources);
$this->container = $container;
$this->faker = $faker;
}
/**
@ -46,8 +47,8 @@ class YamlDataLoader extends AbstractDataLoader
throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $file));
}
if (null !== $this->container && $this->container->has('faker.generator')) {
$generator = $this->container->get('faker.generator');
if (null !== $this->faker) {
$generator = $this->faker;
$faker = function ($type) use ($generator) {
$args = func_get_args();
array_shift($args);

View file

@ -52,7 +52,8 @@
<service id="propel.loader.yaml" class="%propel.loader.yaml.class%">
<argument>%kernel.root_dir%</argument>
<argument type="service" id="service_container" />
<argument>%propel.configuration%</argument>
<argument type="service" id="faker.generator" on-invalid="null" />
</service>
<service id="propel.loader.xml" class="%propel.loader.xml.class%">

View file

@ -37,7 +37,7 @@ class YamlDataLoaderTest extends TestCase
YAML;
$filename = $this->getTempFile($fixtures);
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con);
@ -61,7 +61,7 @@ Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor:
YAML;
$filename = $this->getTempFile($fixtures);
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con);
@ -85,7 +85,7 @@ Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor:
YAML;
$filename = $this->getTempFile($fixtures);
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con);
@ -109,7 +109,7 @@ Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookAuthor:
YAML;
$filename = $this->getTempFile($fixtures);
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con);
@ -137,10 +137,8 @@ Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBook:
YAML;
$filename = $this->getTempFile($fixtures);
$container = $this->getContainer();
$container->set('faker.generator', \Faker\Factory::create());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $container);
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array(), \Faker\Factory::create());
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\CoolBookQuery::create()->find($this->con);
@ -215,7 +213,7 @@ YAML;
$con = $builder->build();
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyBookQuery::create()->find($con);
@ -303,7 +301,7 @@ YAML;
$builder->setSchema($schema);
$con = $builder->build();
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename1, $filename2), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlManyToManyMultipleFilesBookQuery::create()->find($con);
@ -372,7 +370,7 @@ YAML;
$builder->setSchema($schema);
$con = $builder->build();
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename), 'default');
$books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlInheritedRelationshipBookQuery::create()->find($con);
@ -407,7 +405,7 @@ YAML;
$builder->setSchema($schema);
$con = $builder->build();
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename), 'default');
$book = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlBookWithObjectQuery::create(null)->findOne($con);
@ -457,7 +455,7 @@ YAML;
$builder->setSchema($schema);
$con = $builder->build();
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $this->getContainer());
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', array());
$loader->load(array($filename), 'default');
$authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\YamlDelegateOnPrimaryKeyAuthorQuery::create()->find($con);