Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a Loader for Symfony DependencyInjection from Yadif to allow a si…

…mple conversion of Yadif to Symfony containers.
  • Loading branch information...
commit d874042a506e416a6d2a1c114f496c60e036c10f 1 parent 6b6c1ce
@beberlei authored
View
127 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;
+ }
+}
View
17 tests/Yadif/AllTests.php
@@ -1,10 +1,17 @@
<?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";
@@ -12,6 +19,7 @@
require_once "ZendApplicationComplianceTest.php";
require_once "BuilderTest.php";
require_once "ModuleTest.php";
+require_once "SymfonyYadifLoaderTest.php";
class Yadif_Tests_AllTests
{
@@ -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;
}
View
4 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()
View
3  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
View
4 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()
View
4 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()
View
28 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);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.