Skip to content
This repository has been archived by the owner on Sep 25, 2022. It is now read-only.

Commit

Permalink
Always use the current services
Browse files Browse the repository at this point in the history
  • Loading branch information
GeLoLabs committed Jul 12, 2016
1 parent 18f1404 commit 34204e6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 99 deletions.
79 changes: 27 additions & 52 deletions src/Context/Doctrine/AbstractAliceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,11 @@ abstract class AbstractAliceContext implements KernelAwareContext, AliceContextI
*/
protected $basePath;

/**
* @var string[]
*/
protected $classes;

/**
* @var FixturesFinderInterface
*/
protected $fixturesFinder;

/**
* @var KernelInterface
*/
protected $kernel;

/**
* @var LoaderInterface
*/
protected $loader;

/**
* @var PersisterInterface
*/
protected $persister;

/**
* @param string|null $basePath
*/
Expand All @@ -68,36 +48,13 @@ public function __construct($basePath = null)
}

/**
* @param KernelInterface $kernel
* @param FixturesFinderInterface $fixturesFinder
* @param LoaderInterface $loader
* @param PersisterInterface|ObjectManager $persister
* @param string $basePath
* {@inheritdoc}
*/
final public function init(
KernelInterface $kernel,
FixturesFinderInterface $fixturesFinder,
LoaderInterface $loader,
PersisterInterface $persister,
$basePath = null
) {
public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
$this->fixturesFinder = $fixturesFinder;
$this->loader = $loader;
$this->persister = $persister;

if (null !== $basePath) {
$this->basePath = $basePath;
}
}

/**
* {@inheritdoc}
*
* @return $this
*/
abstract public function setKernel(KernelInterface $kernel);

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -167,7 +124,7 @@ public function thereAreSeveralFixtures(TableNode $fixturesFileRows, $persister
private function loadFixtures($fixturesFiles, $persister = null)
{
if (null === $persister) {
$persister = $this->persister;
$persister = $this->getPersister();
}

if (true === is_string($persister)) {
Expand Down Expand Up @@ -202,20 +159,20 @@ private function loadFixtures($fixturesFiles, $persister = null)
if (false === empty($fixtureBundles)) {
$fixturesFiles = array_merge(
$fixturesFiles,
$this->fixturesFinder->getFixtures($this->kernel, $fixtureBundles, $this->kernel->getEnvironment())
$this->getFixturesFinder()->getFixtures($this->kernel, $fixtureBundles, $this->kernel->getEnvironment())
);
}

if (false === empty($fixtureDirectories)) {
$fixturesFiles = array_merge(
$fixturesFiles,
$this->fixturesFinder->getFixturesFromDirectory($fixtureDirectories)
$this->getFixturesFinder()->getFixturesFromDirectory($fixtureDirectories)
);
}

$this->loader->load(
$this->getLoader()->load(
$persister,
$this->fixturesFinder->resolveFixtures($this->kernel, $fixturesFiles)
$this->getFixturesFinder()->resolveFixtures($this->kernel, $fixturesFiles)
);
}

Expand All @@ -229,7 +186,7 @@ private function loadFixtures($fixturesFiles, $persister = null)
final protected function resolvePersister($persister)
{
if (null === $persister) {
return $this->persister;
return $this->getPersister();
}

switch (true) {
Expand All @@ -245,4 +202,22 @@ final protected function resolvePersister($persister)
));
}
}

/**
* @return LoaderInterface
*/
protected function getLoader()
{
return $this->kernel->getContainer()->get('hautelook_alice.fixtures.loader');
}

/**
* @return PersisterInterface
*/
abstract protected function getPersister();

/**
* @return FixturesFinderInterface
*/
abstract protected function getFixturesFinder();
}
58 changes: 36 additions & 22 deletions src/Context/Doctrine/AliceODMContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Fidry\AliceBundleExtension\Context\Doctrine;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\SchemaManager;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Context to load fixtures files with Alice loader.
Expand All @@ -22,51 +22,65 @@
class AliceODMContext extends AbstractAliceContext
{
/**
* @var SchemaManager
* {@inheritdoc}
*/
private $schemaManager;
public function createSchema()
{
$schemaManager = $this->getSchemaManager();

$schemaManager->createCollections();
$schemaManager->ensureIndexes();
}

/**
* {@inheritdoc}
*/
public function setKernel(KernelInterface $kernel)
public function dropSchema()
{
$this->init(
$kernel,
$kernel->getContainer()->get('hautelook_alice.doctrine.mongodb.fixtures_finder'),
$kernel->getContainer()->get('hautelook_alice.fixtures.loader'),
$this->resolvePersister($kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager'))
);

$this->schemaManager = $kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager')->getSchemaManager();
$schemaManager = $this->getSchemaManager();

return $this;
$schemaManager->deleteIndexes();
$schemaManager->dropCollections();
}

/**
* {@inheritdoc}
*/
public function createSchema()
public function emptyDatabase()
{
$this->schemaManager->createCollections();
$this->schemaManager->ensureIndexes();
$this->dropSchema();
$this->createSchema();
}

/**
* {@inheritdoc}
*/
public function dropSchema()
protected function getPersister()
{
$this->schemaManager->deleteIndexes();
$this->schemaManager->dropCollections();
return $this->resolvePersister($this->getDocumentManager());
}

/**
* {@inheritdoc}
*/
public function emptyDatabase()
protected function getFixturesFinder()
{
$this->dropSchema();
$this->createSchema();
return $this->kernel->getContainer()->get('hautelook_alice.doctrine.mongodb.fixtures_finder');
}

/**
* @return SchemaManager
*/
private function getSchemaManager()
{
return $this->getDocumentManager()->getSchemaManager();
}

/**
* @return DocumentManager
*/
private function getDocumentManager()
{
return $this->kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager');
}
}
65 changes: 40 additions & 25 deletions src/Context/Doctrine/AliceORMContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

namespace Fidry\AliceBundleExtension\Context\Doctrine;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Context to load fixtures files with Alice loader.
Expand All @@ -22,53 +23,67 @@
class AliceORMContext extends AbstractAliceContext
{
/**
* @var SchemaTool
* {@inheritdoc}
*/
private $schemaTool;
public function createSchema()
{
$this->getSchemaTool()->createSchema($this->getAllMetadatas());
}

/**
* {@inheritdoc}
*/
public function setKernel(KernelInterface $kernel)
public function dropSchema()
{
$this->init(
$kernel,
$kernel->getContainer()->get('hautelook_alice.doctrine.orm.fixtures_finder'),
$kernel->getContainer()->get('hautelook_alice.fixtures.loader'),
$this->resolvePersister($kernel->getContainer()->get('doctrine.orm.entity_manager'))
);

/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $kernel->getContainer()->get('doctrine.orm.default_entity_manager');

$this->schemaTool = new SchemaTool($entityManager);
$this->classes = $entityManager->getMetadataFactory()->getAllMetadata();

return $this;
$this->getSchemaTool()->dropSchema($this->getAllMetadatas());
}

/**
* {@inheritdoc}
*/
public function createSchema()
public function emptyDatabase()
{
$this->schemaTool->createSchema($this->classes);
$this->dropSchema();
$this->createSchema();
}

/**
* {@inheritdoc}
*/
public function dropSchema()
protected function getPersister()
{
$this->schemaTool->dropSchema($this->classes);
return $this->resolvePersister($this->getEntityManager());
}

/**
* {@inheritdoc}
*/
public function emptyDatabase()
protected function getFixturesFinder()
{
$this->dropSchema();
$this->createSchema();
return $this->kernel->getContainer()->get('hautelook_alice.doctrine.orm.fixtures_finder');
}

/**
* @return SchemaTool
*/
private function getSchemaTool()
{
return new SchemaTool($this->getEntityManager());
}

/**
* @return ClassMetadata[]
*/
private function getAllMetadatas()
{
return $this->getEntityManager()->getMetadataFactory()->getAllMetadata();
}

/**
* @return EntityManagerInterface
*/
private function getEntityManager()
{
return $this->kernel->getContainer()->get('doctrine.orm.entity_manager');
}
}

0 comments on commit 34204e6

Please sign in to comment.