Skip to content

Commit

Permalink
Add suppor for symfony's 3.3 service locator
Browse files Browse the repository at this point in the history
  • Loading branch information
mvrhov committed Jan 18, 2018
1 parent da2304e commit 5ccad45
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
33 changes: 28 additions & 5 deletions sources/lib/DependencyInjection/Compiler/ModelPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ private function addTagged(DI\ContainerBuilder $container, $tag, $defaultService
/** @var DI\Definition[] $definitions */
$definitions = [];

$poolerToService = [];
$gteSf33 = version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '3.3', '>=');

// find all service IDs with the appropriate tag
$taggedServices = $container->findTaggedServiceIds($tag);

Expand All @@ -49,28 +52,48 @@ private function addTagged(DI\ContainerBuilder $container, $tag, $defaultService
if (!in_array($interface, class_implements($definitions[$serviceId]->getClass()), true)) {
throw new \RuntimeException(sprintf('Your pooler should implement %s.', $interface));
}

$poolerToService[$serviceId] = [];
} else {
throw new \RuntimeException(sprintf('There is no pooler service with id %s.', $serviceId));
}
}

$definitions[$serviceId]->addMethodCall('addModelToServiceMapping', [$class, $id . '.pomm.inner']);
$innerId = $id . '.pomm.inner';
$definitions[$serviceId]->addMethodCall('addModelToServiceMapping', [$class, $innerId]);
$poolerToService[$serviceId][$innerId] = new DI\Reference($innerId);

$old = $container->getDefinition($id);
$old->setPublic(true);
$container->removeDefinition($id);
$container->addDefinitions([$id . '.pomm.inner' => $old]);
$container->addDefinitions([$innerId => $old]);

$service = $container->register($id, $old->getClass())
->setFactory([new DI\Reference($sessionId), $method])
->addArgument($old->getClass())
;

if (version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '3.3', '<')) {
$service->addAutowiringType($old->getClass());
if ($gteSf33) {
$service->setPublic($old->isPublic());
//there is no need for inner service to be public anymore, because only service locator uses it.
$old->setPublic(false);
//add alias with full class to be compatible with sf 4.0 autowiring
if ($id !== $class) {
$container->setAlias($class, $id);
}
} else {
$service->setAutowiringTypes([$old->getClass()]); //set this one as a default for autowire
}
}

$container->setAlias($class, $id);
if ($gteSf33) {
foreach ($poolerToService as $service => $references) {
$container->getDefinition($service)
->removeMethodCall('setContaner')
->addMethodCall('setContainer', [
DI\Compiler\ServiceLocatorTagPass::register($container, $references)
]);
}
}
}
}
23 changes: 18 additions & 5 deletions sources/lib/Model/ContainerModelLayerPooler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

use PommProject\Foundation\Client\ClientPooler;
use PommProject\ModelManager\ModelLayer\ModelLayerPooler;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;

/**
* ModelLayerPooler
Expand All @@ -25,11 +25,11 @@
* @license X11 {@link http://opensource.org/licenses/mit-license.php}
* @see ClientPooler
*/
class ContainerModelLayerPooler extends ModelLayerPooler implements ContainerAwareInterface, ServiceMapInterface
class ContainerModelLayerPooler extends ModelLayerPooler implements ServiceMapInterface
{
use ContainerAwareTrait;

private $serviceMap = [];
/** @var PsrContainerInterface|ContainerInterface */
private $container;

/**
* {@inheritdoc}
Expand All @@ -50,4 +50,17 @@ protected function createClient($class)

return parent::createClient($class);
}

/**
* @param PsrContainerInterface|ContainerInterface $container
*/
public function setContainer($container)
{
if (!($container instanceof PsrContainerInterface) && !($container instanceof ContainerInterface)) {
throw new \InvalidArgumentException(
'$contaner should be instance of Symfony\Component\DependencyInjection\ContainerInterface or Psr\Container\ContainerInterface'
);
}
$this->container = $container;
}
}
23 changes: 18 additions & 5 deletions sources/lib/Model/ContainerModelPooler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace PommProject\PommBundle\Model;

use PommProject\ModelManager\Model\ModelPooler;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;

/**
* PommExtension
Expand All @@ -24,11 +24,11 @@
* @license X11 {@link http://opensource.org/licenses/mit-license.php}
* @see Extension
*/
class ContainerModelPooler extends ModelPooler implements ContainerAwareInterface, ServiceMapInterface
class ContainerModelPooler extends ModelPooler implements ServiceMapInterface
{
use ContainerAwareTrait;

private $serviceMap = [];
/** @var PsrContainerInterface|ContainerInterface */
private $container;

/**
* {@inheritdoc}
Expand All @@ -49,4 +49,17 @@ protected function createClient($class)

return parent::createClient($class);
}

/**
* @param PsrContainerInterface|ContainerInterface $container
*/
public function setContainer($container)
{
if (!($container instanceof PsrContainerInterface) && !($container instanceof ContainerInterface)) {
throw new \InvalidArgumentException(
'$contaner should be instance of Symfony\Component\DependencyInjection\ContainerInterface or Psr\Container\ContainerInterface'
);
}
$this->container = $container;
}
}

0 comments on commit 5ccad45

Please sign in to comment.