Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suppor for symfony's 3.3 service locator #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 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,29 +52,47 @@ 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
}
}

if ($class !== $id) {
$container->setAlias($class, $id);
if ($gteSf33) {
foreach ($poolerToService as $service => $references) {
$container->getDefinition($service)
->removeMethodCall('setContaner')
->addMethodCall('setContainer', [
DI\Compiler\ServiceLocatorTagPass::register($container, $references)
]);
}
}
}
Expand Down
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 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t undestand the goal of changes in this file. The ContainerAwareTrait already requires a ContainerInterface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t undestand the goal of changes in this file. The ContainerAwareTrait already requires a ContainerInterface.

Copy link
Contributor Author

@mvrhov mvrhov Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ContainerAware is not compatible with PSR container. And service locator in Sf3.3 returns PSR container interface, Previous sf version their own.


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;
}
}