Skip to content

Commit

Permalink
Merge pull request #10 from ubick/feature/support-custom-compiler-passes
Browse files Browse the repository at this point in the history
Add support for custom compiler passes
  • Loading branch information
Jon Acker committed Jun 1, 2016
2 parents 07c8ec9 + 204805e commit 39bd5f9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/code/community/Bridge/MageApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ interface MageApp
public function useCache($model);

public function getStore($id = null);

public function dispatchEvent($eventName, $args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

use ContainerTools\Configuration;
use ContainerTools\ContainerGenerator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Container;
use Bridge\MageApp;

class Inviqa_SymfonyContainer_Helper_ContainerProvider
{
const HELPER_NAME = 'inviqa_symfonyContainer/containerProvider';

/**
* @var Mage_Core_Model_App
*/
private $_mageApp;

/**
* @var Container
*/
Expand All @@ -22,14 +29,15 @@ class Inviqa_SymfonyContainer_Helper_ContainerProvider
* @var CompilerPassInterface
*/
private $_storeConfigCompilerPass;

/**
* @var CompilerPassInterface
*/
private $_injectableCompilerPass;

public function __construct(array $services = array())
{
$this->_mageApp = isset($services['app']) ? $services['app'] : Mage::app();

$this->_generatorConfig = isset($services['generatorConfig']) ?
$services['generatorConfig'] :
Mage::getModel('inviqa_symfonyContainer/configurationBuilder')->build();
Expand Down Expand Up @@ -59,6 +67,11 @@ private function _buildContainer()
$this->_generatorConfig->addCompilerPass($this->_storeConfigCompilerPass);
$this->_generatorConfig->addCompilerPass($this->_injectableCompilerPass);

$this->_mageApp->dispatchEvent(
'symfony_container_before_container_generator',
['generator_config' => $this->_generatorConfig]
);

$generator = new ContainerGenerator($this->_generatorConfig);

return $this->_container = $generator->getContainer();
Expand Down
23 changes: 20 additions & 3 deletions app/code/community/Inviqa/SymfonyContainer/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ class Inviqa_SymfonyContainer_Model_Observer
{
use Inviqa_SymfonyContainer_Helper_ServiceProvider;

const CACHE_META_SUFFIX = '.meta';
const SERVICE_INJECTOR = 'inviqa_symfonyContainer/serviceInjector';

public function onCacheRefresh(Varien_Event_Observer $event)
{
if (ConfigurationBuilder::MODEL_ALIAS === $event->getType()) {
$filePath = Mage::getBaseDir('cache') . '/' . ConfigurationBuilder::CACHED_CONTAINER;
if (file_exists($filePath)) {
unlink($filePath);
$containerFilePath = $this->containerCachePath();
$metaFilePath = $this->containerCacheMetaPath();

if (file_exists($containerFilePath)) {
unlink($containerFilePath);
}

if (file_exists($metaFilePath)) {
unlink($metaFilePath);
}
}
}
Expand All @@ -26,4 +33,14 @@ public function onPreDispatch(Varien_Event_Observer $event)
'container' => Mage::helper(ContainerProvider::HELPER_NAME)->getContainer()
])->setupDependencies($controller);
}

private function containerCachePath()
{
return Mage::getBaseDir('cache') . DIRECTORY_SEPARATOR . ConfigurationBuilder::CACHED_CONTAINER;
}

private function containerCacheMetaPath()
{
return $this->containerCachePath() . self::CACHE_META_SUFFIX;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
use Inviqa_SymfonyContainer_Model_StoreConfigCompilerPass as StoreConfigCompilerPass;
use Inviqa_SymfonyContainer_Model_InjectableCompilerPass as InjectableCompilerPass;
use Symfony\Component\DependencyInjection\Container;
use Bridge\MageApp;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class Inviqa_SymfonyContainer_Helper_ContainerProviderSpec extends ObjectBehavior
{
function let(Configuration $generatorConfig, StoreConfigCompilerPass $configCompilerPass, InjectableCompilerPass $injectableCompilerPass)
function let(
MageApp $app,
Configuration $generatorConfig,
StoreConfigCompilerPass $configCompilerPass,
InjectableCompilerPass $injectableCompilerPass
)
{
$generatorConfig->getContainerFilePath()->willReturn('container.php');
$generatorConfig->getDebug()->willReturn(true);
Expand All @@ -22,6 +28,7 @@ function let(Configuration $generatorConfig, StoreConfigCompilerPass $configComp
$generatorConfig->isTestEnvironment()->willReturn(false);

$services = [
'app' => $app,
'generatorConfig' => $generatorConfig,
'storeConfigCompilerPass' => $configCompilerPass,
'injectableCompilerPass' => $injectableCompilerPass
Expand All @@ -30,18 +37,28 @@ function let(Configuration $generatorConfig, StoreConfigCompilerPass $configComp
$this->beConstructedWith($services);
}

function it_generates_container(Configuration $generatorConfig)
function it_generates_the_container(Configuration $generatorConfig)
{
$generatorConfig->addCompilerPass(Argument::any())->shouldBeCalled();

$this->getContainer()->shouldBeAnInstanceOf(Container::class);
}

function it_memoizes_container(Configuration $generatorConfig, StoreConfigCompilerPass $configCompilerPass, InjectableCompilerPass $injectableCompilerPass)
function it_memoizes_the_container(
MageApp $app,
Configuration $generatorConfig,
StoreConfigCompilerPass $configCompilerPass,
InjectableCompilerPass $injectableCompilerPass
)
{
$generatorConfig->addCompilerPass($configCompilerPass)->shouldBeCalledTimes(1);
$generatorConfig->addCompilerPass($injectableCompilerPass)->shouldBeCalledTimes(1);

$app->dispatchEvent(
'symfony_container_before_container_generator',
['generator_config' => $generatorConfig]
)->shouldBeCalled();

$container = $this->getContainer();
$this->getContainer()->shouldBe($container);
}
Expand Down

0 comments on commit 39bd5f9

Please sign in to comment.