Skip to content

Commit

Permalink
Leverage Symfony 3.3+ Autowiring
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude committed Feb 4, 2020
1 parent 77e944b commit 2cb5457
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 66 deletions.
3 changes: 2 additions & 1 deletion DependencyInjection/Compiler/CollectFilterPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Webfactory\Bundle\LegacyIntegrationBundle\EventListener\LegacyApplicationDispatchingEventListener;

class CollectFilterPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$kernelEventListenerDefinition = $container->getDefinition('webfactory_legacy_integration.kernel_event_listener');
$kernelEventListenerDefinition = $container->getDefinition(LegacyApplicationDispatchingEventListener::class);

foreach ($container->findTaggedServiceIds('webfactory_legacy_integration.filter') as $id => $tags) {
$kernelEventListenerDefinition->addMethodCall('addFilter', [
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/WebfactoryLegacyIntegrationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public function load(array $configs, ContainerBuilder $container)

switch (@$config['parsingMode']) {
case 'html5':
$container->setParameter('webfactory_legacy_integration.parser_class', 'Webfactory\Dom\PolyglotHTML5ParsingHelper');
$container->setParameter('webfactory_legacy_integration.parser_class', Webfactory\Dom\PolyglotHTML5ParsingHelper::class);
break;
case 'xhtml10':
$container->setParameter('webfactory_legacy_integration.parser_class', 'Webfactory\Dom\XHTML10ParsingHelper');
$container->setParameter('webfactory_legacy_integration.parser_class', Webfactory\Dom\XHTML10ParsingHelper::class);
break;
}
}
Expand Down
24 changes: 10 additions & 14 deletions EventListener/LegacyApplicationDispatchingEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@
namespace Webfactory\Bundle\LegacyIntegrationBundle\EventListener;

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Annotation\Dispatch;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\LegacyApplication;

class LegacyApplicationDispatchingEventListener
{
protected $container;
/**
* @var LegacyApplication
*/
private $legacyApplication;

protected $reader;

protected $stopwatch;

/**
* @var Filter[]
*/
protected $filters = [];

public function __construct(ContainerInterface $container, Reader $reader)
public function __construct(LegacyApplication $legacyApplication, Reader $reader)
{
$this->container = $container;
$this->legacyApplication = $legacyApplication;
$this->reader = $reader;
}

Expand All @@ -55,7 +59,7 @@ public function onKernelController(FilterControllerEvent $event)
}

if ($dispatch) {
$response = $this->getLegacyApplication()->handle($event->getRequest(), $event->getRequestType(), false);
$response = $this->legacyApplication->handle($event->getRequest(), $event->getRequestType(), false);

foreach ($this->filters as $filter) {
$filter->filter($event, $response);
Expand All @@ -65,12 +69,4 @@ public function onKernelController(FilterControllerEvent $event)
}
}
}

/**
* @return HttpKernelInterface
*/
protected function getLegacyApplication()
{
return $this->container->get('webfactory_legacy_integration.legacy_application');
}
}
4 changes: 2 additions & 2 deletions Integration/Annotation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Webfactory\Bundle\LegacyIntegrationBundle\Integration\Annotation;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter as FilterInterface;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\Factory;

Expand All @@ -33,7 +33,7 @@ public function __construct($values)
}
}

public function createFilter(Container $container)
public function createFilter(ContainerInterface $container)
{
if ($class = $this->class) {
if (!class_exists($class)) {
Expand Down
4 changes: 2 additions & 2 deletions Integration/Annotation/IgnoreHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Webfactory\Bundle\LegacyIntegrationBundle\Integration\Annotation;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\Factory;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\IgnoreHeader as IgnoreHeaderFilter;

Expand All @@ -27,7 +27,7 @@ public function __construct(array $values)
}
}

public function createFilter(Container $container)
public function createFilter(ContainerInterface $container)
{
return new IgnoreHeaderFilter($this->header);
}
Expand Down
4 changes: 2 additions & 2 deletions Integration/Annotation/IgnoreRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Webfactory\Bundle\LegacyIntegrationBundle\Integration\Annotation;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\Factory;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\IgnoreRedirect as IgnoreRedirectFilter;

Expand All @@ -17,7 +17,7 @@
*/
class IgnoreRedirect implements Factory
{
public function createFilter(Container $container)
public function createFilter(ContainerInterface $container)
{
return new IgnoreRedirectFilter();
}
Expand Down
4 changes: 2 additions & 2 deletions Integration/Annotation/Passthru.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Webfactory\Bundle\LegacyIntegrationBundle\Integration\Annotation;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\Factory;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\PassthruLegacyResponseFilter;

Expand All @@ -17,7 +17,7 @@
*/
class Passthru implements Factory
{
public function createFilter(Container $container)
public function createFilter(ContainerInterface $container)
{
return new PassthruLegacyResponseFilter();
}
Expand Down
4 changes: 2 additions & 2 deletions Integration/Filter/ControllerAnnotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter;

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter as FilterInterface;
Expand All @@ -19,7 +19,7 @@ class ControllerAnnotations implements FilterInterface
protected $reader;
protected $container;

public function __construct(Reader $reader, Container $container)
public function __construct(Reader $reader, ContainerInterface $container)
{
$this->reader = $reader;
$this->container = $container;
Expand Down
4 changes: 2 additions & 2 deletions Integration/Filter/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;

interface Factory
{
public function createFilter(Container $container);
public function createFilter(ContainerInterface $container);
}
38 changes: 14 additions & 24 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,51 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults autowire="true" autoconfigure="true" />

<service id="Webfactory\Bundle\LegacyIntegrationBundle\Integration\BootstrapFileKernelAdaptor">
<argument>%webfactory_legacy_integration.legacy_application_bootstrap_file%</argument>
</service>

<service id="webfactory_legacy_integration.legacy_kernel"
alias="Webfactory\Bundle\LegacyIntegrationBundle\Integration\BootstrapFileKernelAdaptor" />
<service id="webfactory_legacy_integration.legacy_kernel" alias="Webfactory\Bundle\LegacyIntegrationBundle\Integration\BootstrapFileKernelAdaptor" />

<service
id="webfactory_legacy_integration.legacy_application"
class="Webfactory\Bundle\LegacyIntegrationBundle\Integration\LegacyApplication"
public="true">
<service id="Webfactory\Bundle\LegacyIntegrationBundle\Integration\LegacyApplication">
<call method="setLegacyKernel">
<argument type="service" id="webfactory_legacy_integration.legacy_kernel" />
</call>
</service>
<service id="webfactory_legacy_integration.legacy_application" alias="Webfactory\Bundle\LegacyIntegrationBundle\Integration\LegacyApplication" public="true" />

<service id="webfactory_legacy_integration.kernel_event_listener"
class="Webfactory\Bundle\LegacyIntegrationBundle\EventListener\LegacyApplicationDispatchingEventListener">
<argument type="service" id="service_container"/>
<argument type="service" id="annotation_reader"/>
<service id="Webfactory\Bundle\LegacyIntegrationBundle\EventListener\LegacyApplicationDispatchingEventListener">
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" priority="-210" />
</service>

<service id="webfactory_legacy_integration.controller_annotations_filter" class="Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\ControllerAnnotations">
<argument type="service" id="annotation_reader"/>
<argument type="service" id="service_container"/>
<service id="Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\ControllerAnnotations">
<tag name="webfactory_legacy_integration.filter"/>
</service>

<service id="webfactory_legacy_integration.keep_cookies_filter" class="Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\KeepCookiesAndHeadersFilter">
<argument type="service" id="annotation_reader"/>
<service id="Webfactory\Bundle\LegacyIntegrationBundle\Integration\Filter\KeepCookiesAndHeadersFilter">
<tag name="webfactory_legacy_integration.filter"/>
<tag name="kernel.event_listener" event="kernel.response"/>
</service>

<service id="webfactory_legacy_integration.twig_extension" class="Webfactory\Bundle\LegacyIntegrationBundle\Twig\Extension">
<argument type="service" id="service_container"/>
<service id="Webfactory\Bundle\LegacyIntegrationBundle\Twig\Extension">
<tag name="twig.extension"/>
<tag name="container.service_subscriber" />
</service>

<service id="webfactory_legacy_integration.xpath_helper_factory"
class="Webfactory\Bundle\LegacyIntegrationBundle\Integration\XPathHelperFactory">
<service id="Webfactory\Bundle\LegacyIntegrationBundle\Integration\XPathHelperFactory">
<argument type="service">
<service class="%webfactory_legacy_integration.parser_class%"/>
</argument>
<argument type="service" id="webfactory_legacy_integration.legacy_application"/>
<argument type="service" id="Webfactory\Bundle\LegacyIntegrationBundle\Integration\LegacyApplication"/>
<argument type="service" id="logger" on-invalid="null" />
<tag name="monolog.logger" channel="webfactory_legacy_integration" />
</service>

<service id="webfactory_legacy_integration.xpath_helper" public="true"
class="Webfactory\Bundle\LegacyIntegrationBundle\Integration\XPathHelper">
<factory service="webfactory_legacy_integration.xpath_helper_factory" method="createHelper" />
<service id="Webfactory\Bundle\LegacyIntegrationBundle\Integration\XPathHelper">
<factory service="Webfactory\Bundle\LegacyIntegrationBundle\Integration\XPathHelperFactory" method="createHelper" />
</service>

<service id="webfactory_legacy_integration.xpath_helper" alias="Webfactory\Bundle\LegacyIntegrationBundle\Integration\XPathHelper" public="true" />
</services>
</container>
32 changes: 24 additions & 8 deletions Twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,33 @@

namespace Webfactory\Bundle\LegacyIntegrationBundle\Twig;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\TwigFunction;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\LegacyApplication;
use Webfactory\Bundle\LegacyIntegrationBundle\Integration\XPathHelper;

class Extension extends AbstractExtension implements GlobalsInterface
class Extension extends AbstractExtension implements GlobalsInterface, ServiceSubscriberInterface
{
protected $legacyApplication;
protected $container;
/**
* @var ContainerInterface
*/
private $container;

/**
* @var string
*/
private $embedResult = null;

protected $embedResult = null;
public static function getSubscribedServices()
{
return [
LegacyApplication::class,
XPathHelper::class,
];
}

public function __construct(ContainerInterface $container)
{
Expand Down Expand Up @@ -58,15 +74,15 @@ public function xpath($xpath)
return $this->getXPathHelper()->getFragment($xpath);
}

protected function getXPathHelper()
private function getXPathHelper()
{
return $this->container->get('webfactory_legacy_integration.xpath_helper');
return $this->container->get(XPathHelper::class);
}

public function embedString($needle, $content)
{
if (null === $this->embedResult) {
$legacyApp = $this->container->get('webfactory_legacy_integration.legacy_application');
$legacyApp = $this->container->get(LegacyApplication::class);
$this->embedResult = $legacyApp->getResponse()->getContent();
}

Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

"require": {
"doctrine/annotations": "~1.0",
"psr/container": "^1.0",
"psr/log": "^1.0",
"symfony/config": "2.8.*|3.4.*|^4.0",
"symfony/dependency-injection": "2.8.*|3.4.*|^4.0",
"symfony/framework-bundle": "2.8.*|3.4.*|^4.0",
"symfony/http-foundation": "2.8.*|3.4.*|^4.0",
"symfony/http-kernel": "2.8.*|3.4.*|^4.0",
"symfony/config": "3.4.*|^4.0",
"symfony/dependency-injection": "3.4.*|^4.0",
"symfony/framework-bundle": "3.4.*|^4.0",
"symfony/http-foundation": "3.4.*|^4.0",
"symfony/http-kernel": "3.4.*|^4.0",
"twig/twig": "^1.34|^2.0",
"webfactory/dom": "~1.0, >= 1.0.15"
}
Expand Down

0 comments on commit 2cb5457

Please sign in to comment.