Skip to content

Commit

Permalink
Use nelmio/alice 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexislefebvre committed Aug 28, 2020
1 parent 97cc93a commit 0bfa11a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 125 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
87 changes: 16 additions & 71 deletions src/Knp/FriendlyContexts/Alice/Fixtures/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) ];
}
}
}
46 changes: 0 additions & 46 deletions src/Knp/FriendlyContexts/Alice/Fixtures/SimpleDataLoader.php

This file was deleted.

20 changes: 13 additions & 7 deletions src/Knp/FriendlyContexts/Context/AliceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit 0bfa11a

Please sign in to comment.