Skip to content

Commit

Permalink
Merge pull request #12 from jjanvier/feature/processors
Browse files Browse the repository at this point in the history
Add the ability to use Alice's processors.
  • Loading branch information
baldurrensch committed Oct 7, 2013
2 parents 4fd1388 + c371222 commit a41cff0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Alice/DataFixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Nelmio\Alice\ProcessorInterface;

/**
* Class DataFixtureLoader
Expand Down Expand Up @@ -37,6 +38,10 @@ public function load(ObjectManager $manager)
$loader->setObjectManager($manager);
$loader->setProviders(array($this));

foreach ($this->getProcessors() as $processor) {
$loader->addProcessor($processor);
}

$loader->load($this->getFixtures());
}

Expand All @@ -47,6 +52,16 @@ public function load(ObjectManager $manager)
*/
abstract protected function getFixtures();

/**
* Returns an array of ProcessorInterface to process fixtures
*
* @return ProcessorInterface[]
*/
protected function getProcessors()
{
return array();
}

/**
* Sets the Container.
*
Expand Down
42 changes: 41 additions & 1 deletion Alice/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ObjectManager;
use Nelmio\Alice\ProcessorInterface;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -17,6 +18,11 @@ class Loader
*/
private $providers;

/**
* @var ProcessorInterface[]
*/
private $processors;

/**
* @var array
*/
Expand Down Expand Up @@ -49,6 +55,7 @@ class Loader
public function __construct($loaders, LoggerInterface $logger = null)
{
$this->loaders = $loaders;
$this->processors = array();
$this->logger = $logger;
$this->references = new ArrayCollection();
}
Expand Down Expand Up @@ -88,7 +95,7 @@ public function load(array $files)
$objects = array();
foreach ($files as $file) {
$set = $loader->load($file);
$this->persister->persist($set);
$this->persist($set);

$objects = array_merge($objects, $set);
}
Expand All @@ -97,6 +104,9 @@ public function load(array $files)
$this->persister->detach($obj);
$this->references->set($name, $obj);
}

// remove processors when file is loaded
$this->processors = array();
}

/**
Expand All @@ -107,6 +117,14 @@ public function setProviders(array $providers)
$this->providers = $providers;
}

/**
* @param ProcessorInterface $processor
*/
public function addProcessor(ProcessorInterface $processor)
{
$this->processors[] = $processor;
}

/**
* @param string $key
*
Expand All @@ -132,4 +150,26 @@ protected function getLoader($key)

return $loader;
}

/**
* Persists objects with the preProcess and postProcess methods used by the processors.
*
* @param $objects
*/
private function persist($objects)
{
foreach ($this->processors as $processor) {
foreach ($objects as $obj) {
$processor->preProcess($obj);
}
}

$this->persister->persist($objects);

foreach ($this->processors as $processor) {
foreach ($objects as $obj) {
$processor->postProcess($obj);
}
}
}
}

0 comments on commit a41cff0

Please sign in to comment.