Skip to content

Commit

Permalink
Add a Loader for Symfony DependencyInjection from Yadif to allow a si…
Browse files Browse the repository at this point in the history
…mple conversion of Yadif to Symfony containers.
  • Loading branch information
beberlei committed Jan 30, 2011
1 parent 6b6c1ce commit d874042
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 19 deletions.
127 changes: 127 additions & 0 deletions src/Yadif/Loader/YadifLoader.php
@@ -0,0 +1,127 @@
<?php

/*
* Yadif
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/

use Symfony\Component\DependencyInjection\Loader\Loader;
use Symfony\Component\DependencyInjection\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class Yadif_Loader_YadifLoader extends Loader
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container;

/**
* @var string
*/
protected $namespace = '';

public function setNamespace($namespace)
{
$this->namespace = $namespace;
}

function load($yadif)
{
if (!($yadif instanceof Yadif_Container)) {
throw new Yadif_Exception("Cannot run Symfony Dependency Injenction YadifLoader without passing an instance of Yadif_Container");
}

$definitions = $yadif->getContainer();
$params = $yadif->getParameters();
foreach ($params AS $param => $value) {
$param = strtolower(substr($param, 1)); // remove the starting :
$this->container->setParameter($this->namespace . $param, $value);
}

foreach ($definitions AS $serviceName => $def) {
$serviceId = $this->convertServiceName($serviceName);
foreach ($def[Yadif_Container::CONFIG_PARAMETERS] AS $param => $value) {
$param = strtolower(substr($param, 1)); // remove the starting :
$this->container->setParameter($serviceId . "." . $param, $value);
}

$arguments = array();
foreach ($def[Yadif_Container::CONFIG_ARGUMENTS] AS $arg) {
if (strpos($arg, ":") === 0) {
$param = strtolower(substr($arg, 1));
if ($this->container->hasParameter($serviceId.".".$param)) {
$arguments[] = '%'.$serviceId.".".$param.'%';
} else {
$arguments[] = '%'.$this->namespace.$param.'%';
}
} else {
$arguments[] = new Reference($this->convertServiceName($arg));
}
}

$this->container->setParameter($serviceId.'.class', $def[Yadif_Container::CONFIG_CLASS]);
$definition = new Definition('%' .$serviceId.'.class%', $arguments);
if ($def[Yadif_Container::CONFIG_SCOPE] == Yadif_Container::SCOPE_PROTOTYPE) {
$definition->setScope('prototype');
}

foreach ($def[Yadif_Container::CONFIG_METHODS] AS $method) {
$methodName = $method[Yadif_Container::CONFIG_METHOD];
foreach ($method[Yadif_Container::CONFIG_PARAMETERS] AS $param => $value) {
$param = strtolower(substr($param, 1)); // remove the starting :
$this->container->setParameter($serviceId . ".".strtolower($methodName).".".$param, $value);
}

$callArguments = array();
foreach ($def[Yadif_Container::CONFIG_ARGUMENTS] AS $arg) {
if (strpos($arg, ":") === 0) {
$param = strtolower(substr($arg, 1));
if ($this->container->hasParameter($serviceId . ".".strtolower($methodName).".".$param)) {
$callArguments[] = '%'.$serviceId . ".".strtolower($methodName).".".$param.'%';
} else if ($this->container->hasParameter($serviceId.".".$param)) {
$callArguments[] = '%'.$serviceId.".".$param.'%';
} else {
$callArguments[] = '%'.$this->namespace.$param.'%';
}
} else {
$callArguments[] = new Reference($this->convertServiceName($arg));
}
}
$definition->addMethodCall($methodName, $callArguments);
}

if (isset($def[Yadif_Container::CONFIG_FACTORY])) {
$definition->setClass($def[Yadif_Container::CONFIG_FACTORY][0]);
$definition->setFactoryMethod($def[Yadif_Container::CONFIG_FACTORY][1]);
}

$this->container->setDefinition($this->convertServiceName($serviceName), $definition);
}
}

private function convertServiceName($yadifService)
{
return $this->namespace . str_replace(array("_", "\\"), ".", strtolower($yadifService));
}

/**
* Returns true if this class supports the given resource.
*
* @param mixed $resource A resource
*
* @return Boolean true if this class supports the given resource, false otherwise
*/
function supports($resource)
{
return false;
}
}
17 changes: 13 additions & 4 deletions tests/Yadif/AllTests.php
@@ -1,17 +1,25 @@
<?php

require_once dirname(__FILE__)."/../Container.php";
require_once dirname(__FILE__)."/../Builder.php";
require_once dirname(__FILE__)."/../Module.php";
require_once "PHPUnit/Framework.php";
require_once __DIR__ . "/../../vendor/symfony/src/Symfony/Component/HttpFoundation/UniversalClassLoader.php";

$loader = new Symfony\Component\HttpFoundation\UniversalClassLoader();
$loader->registerNamespace('Symfony', __DIR__.'/../../vendor/symfony/src/');
$loader->register();

require_once __DIR__."/../../src/Yadif/Container.php";
require_once __DIR__."/../../src/Yadif/Builder.php";
require_once __DIR__."/../../src/Yadif/Module.php";
require_once __DIR__."/../../src/Yadif/Loader/YadifLoader.php";

require_once "Fixture.php";
require_once "ConfigComponentTest.php";
require_once "InstantiateObjectGraphTest.php";
require_once "BindParamsTest.php";
require_once "EnforceSingletonTest.php";
require_once "ZendApplicationComplianceTest.php";
require_once "BuilderTest.php";
require_once "ModuleTest.php";
require_once "SymfonyYadifLoaderTest.php";

class Yadif_Tests_AllTests
{
Expand All @@ -25,6 +33,7 @@ public static function suite()
$suite->addTestSuite('Yadif_Tests_ZendApplicationComplianceTest');
$suite->addTestSuite('Yadif_Tests_BuilderTest');
$suite->addTestSuite('Yadif_Tests_ModuleTest');
$suite->addTestSuite('Yadif_Tests_SymfonyYadifLoaderTest');

return $suite;
}
Expand Down
4 changes: 0 additions & 4 deletions tests/Yadif/BindParamsTest.php
@@ -1,9 +1,5 @@
<?php

require_once dirname(__FILE__)."/../Container.php";
require_once "Fixture.php";
require_once "PHPUnit/Framework.php";

class Yadif_Tests_BindParamsTest extends PHPUnit_Framework_TestCase
{
public function testGetParamReturnsNullByDefault()
Expand Down
3 changes: 0 additions & 3 deletions tests/Yadif/ConfigComponentTest.php
@@ -1,8 +1,5 @@
<?php

require_once dirname(__FILE__)."/../Container.php";
require_once "Fixture.php";
require_once "PHPUnit/Framework.php";
require_once "Zend/Config.php";

class Yadif_Tests_ConfigComponentTest extends PHPUnit_Framework_TestCase
Expand Down
4 changes: 0 additions & 4 deletions tests/Yadif/EnforceSingletonTest.php
@@ -1,9 +1,5 @@
<?php

require_once dirname(__FILE__)."/../Container.php";
require_once "Fixture.php";
require_once "PHPUnit/Framework.php";

class Yadif_Tests_EnforceSingletonTest extends PHPUnit_Framework_TestCase
{
public function testInstantiateObjectGraphWithNestedSingletonAsDefaultBehaviour()
Expand Down
4 changes: 0 additions & 4 deletions tests/Yadif/InstantiateObjectGraphTest.php
@@ -1,9 +1,5 @@
<?php

require_once dirname(__FILE__)."/../Container.php";
require_once "Fixture.php";
require_once "PHPUnit/Framework.php";

class Yadif_Tests_InstantiateObjectGraphTest extends PHPUnit_Framework_TestCase
{
public function testInstantiateObjectWithoutDependencies()
Expand Down
28 changes: 28 additions & 0 deletions tests/Yadif/SymfonyYadifLoaderTest.php
@@ -0,0 +1,28 @@
<?php

class Yadif_Tests_SymfonyYadifLoaderTest extends PHPUnit_Framework_TestCase
{

public function testLoad()
{
$config = array(
'YadifFoo' => array(
'class' => 'YadifFoo',
'arguments' => array('YadifBaz', 'YadifBar'),
),
'YadifBar' => array(
'class' => 'YadifBar',
'arguments' => array('YadifBaz')
),
'YadifBaz' => array(
'class' => 'YadifBaz',
)
);
$yadif = new Yadif_Container($config);

$container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
$loader = new Yadif_Loader_YadifLoader($container);
$loader->load($yadif);
}

}

0 comments on commit d874042

Please sign in to comment.