diff --git a/composer.json b/composer.json index 5d70c913..0ebe5a20 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "doctrine/data-fixtures": "~1.0", "behat/mink-extension": "~2.0", "fzaninotto/faker": "~1.4", - "nelmio/alice": "~2.0|~3.0", + "nelmio/alice": "~2.0", "guzzle/guzzle": "~3.7" }, "require-dev": { diff --git a/src/Knp/FriendlyContexts/Alice/Fixtures/Loader.php b/src/Knp/FriendlyContexts/Alice/Fixtures/Loader.php index 2fc4598d..74e8beb3 100644 --- a/src/Knp/FriendlyContexts/Alice/Fixtures/Loader.php +++ b/src/Knp/FriendlyContexts/Alice/Fixtures/Loader.php @@ -3,95 +3,40 @@ namespace Knp\FriendlyContexts\Alice\Fixtures; use Knp\FriendlyContexts\Alice\ProviderResolver; -use Nelmio\Alice\Loader\NativeLoader as BaseLoader; - -use Nelmio\Alice\DataLoaderInterface; -use Nelmio\Alice\FixtureBuilderInterface; -use Nelmio\Alice\GeneratorInterface; -use Nelmio\Alice\IsAServiceTrait; -use Nelmio\Alice\ObjectSet; - -class SimpleDataLoader implements DataLoaderInterface -{ - use IsAServiceTrait; - - /** - * @var FixtureBuilderInterface - */ - private $builder; - - /** - * @var GeneratorInterface - */ - private $generator; - - public function __construct(FixtureBuilderInterface $fixtureBuilder, GeneratorInterface $generator) - { - $this->builder = $fixtureBuilder; - $this->generator = $generator; - } - - public $fixtureSet; - - /** - * @inheritdoc - */ - public function loadData(array $data, array $parameters = [], array $objects = []) : ObjectSet - { - $fixtureSet = $this->builder->build($data, $parameters, $objects); - $this->fixtureSet = $fixtureSet; - - return $this->generator->generate($fixtureSet); - } -} - +use Nelmio\Alice\Fixtures\Loader as BaseLoader; class Loader extends BaseLoader { - private $fixtureSet; - private $fixtureData; - private $loader; - private $dataLoader; + private $cache = []; public function __construct($locale, ProviderResolver $providers) { - parent::__construct(); - $this->loader = $this->createFilesLoader(); - $this->dataLoader = $this->getDataLoader(); + parent::__construct($locale, $providers->all()); } public function getCache() { - $cache = []; - foreach ($this->fixtureSet->getFixtures() as $fixtureId => $fixture) { - $spec = [$fixtureId]; - foreach ($fixture->getSpecs()->getProperties()->getIterator() as $property) { - $spec[] = $property->getValue(); - } - $cache[] = [$spec, $this->fixtureData[$fixture->getId()]]; - } - - return $cache; + return $this->cache; } public function clearCache() { - $this->fixtureSet = null; + $this->cache = []; } - public function load($filename) + /** + * {@inheritdoc} + */ + protected function instantiateFixtures(array $fixtures) { - $this->fixtureData = $this->loader->loadFiles($filename)->getObjects(); - $this->fixtureSet = $this->dataLoader->fixtureSet; + parent::instantiateFixtures($fixtures); - return $this->fixtureData; - } + foreach ($fixtures as $fixture) { + $spec = array_map(function ($property) { + return $property->getValue(); + }, $fixture->getProperties()); - protected function createDataLoader() : DataLoaderInterface - { - return new SimpleDataLoader( - $this->getFixtureBuilder(), - $this->getGenerator() - ); + $this->cache[] = [ $spec, $this->objects->get($fixture->getName()) ]; + } } } diff --git a/src/Knp/FriendlyContexts/Alice/Fixtures/SimpleDataLoader.php b/src/Knp/FriendlyContexts/Alice/Fixtures/SimpleDataLoader.php deleted file mode 100644 index 4ab8544d..00000000 --- a/src/Knp/FriendlyContexts/Alice/Fixtures/SimpleDataLoader.php +++ /dev/null @@ -1,46 +0,0 @@ -builder = $fixtureBuilder; - $this->generator = $generator; - } - - public $fixtureSet; - - /** - * @inheritdoc - */ - public function loadData(array $data, array $parameters = [], array $objects = []) : ObjectSet - { - $fixtureSet = $this->builder->build($data, $parameters, $objects); - $this->fixtureSet = $fixtureSet; - - return $this->generator->generate($fixtureSet); - } -} diff --git a/src/Knp/FriendlyContexts/Context/AliceContext.php b/src/Knp/FriendlyContexts/Context/AliceContext.php index 359b3bc1..e01a5f33 100644 --- a/src/Knp/FriendlyContexts/Context/AliceContext.php +++ b/src/Knp/FriendlyContexts/Context/AliceContext.php @@ -35,19 +35,24 @@ public function loadAlice($event) protected function loadFixtures($loader, $fixtures, $files) { $persistable = $this->getPersistableClasses(); - foreach ($loader->load($fixtures) as $object) { - if (in_array(get_class($object), $persistable)) { - $this->getEntityManager()->persist($object); + + foreach ($fixtures as $id => $fixture) { + if (in_array($id, $files)) { + foreach ($loader->load($fixture) as $object) { + if (in_array(get_class($object), $persistable)) { + $this->getEntityManager()->persist($object); + } + } + + $this->getEntityManager()->flush(); } } - - $this->getEntityManager()->flush(); } private function getPersistableClasses() { $persistable = array(); - $metadatas = $this->getEntityManager()->getMetadataFactory()->getAllMetadata(); + $metadatas = $this->getEntityManager()->getMetadataFactory()->getAllMetadata(); foreach ($metadatas as $metadata) { if (isset($metadata->isEmbeddedClass) && $metadata->isEmbeddedClass) { @@ -69,7 +74,8 @@ protected function registerCache($loader) $this ->getRecordBag() ->getCollection($reflection->getName()) - ->attach($entity, $values); + ->attach($entity, $values) + ; $reflection = $reflection->getParentClass(); } while (false !== $reflection); }