Permalink
Browse files

Fix InactiveScopeException in CLI mode

  • Loading branch information...
1 parent cab5925 commit 73e78070ca7340c9774c19dde3c8460435b0e284 suncat2000 committed Aug 29, 2012
@@ -0,0 +1,25 @@
+<?php
+
+namespace SunCat\MobileDetectBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+/**
+ * CLI listener
+ */
+class CLIListenerPass implements CompilerPassInterface
+{
+ /**
+ * If CLI, when remove setMobileDetector method from definition mobile_detect.twig.extension
+ * @param ContainerBuilder $container
+ */
+ public function process(ContainerBuilder $container)
+ {
+ if (php_sapi_name() == "cli") {
+ $definition = $container->getDefinition('mobile_detect.twig.extension');
+ $definition->removeMethodCall('setMobileDetector');
+ }
+ }
+}
@@ -3,10 +3,22 @@
namespace SunCat\MobileDetectBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use SunCat\MobileDetectBundle\DependencyInjection\Compiler\CLIListenerPass;
/**
* MobileDetectBundle
*/
class MobileDetectBundle extends Bundle
{
+ /**
+ * Build method
+ * @param ContainerBuilder $container
+ */
+ public function build(ContainerBuilder $container)
+ {
+ parent::build($container);
+
+ $container->addCompilerPass(new CLIListenerPass());
+ }
}
@@ -11,14 +11,14 @@ services:
arguments: [ '@request=' ]
mobile_detect.request_listener:
class: %mobile_detect.request_listener.class%
- arguments: [ '@mobile_detect.mobile_detector', '@request', %mobile_detect.redirect% ]
- scope: request
+ arguments: [ '@mobile_detect.mobile_detector', '@request=', %mobile_detect.redirect% ]
tags:
- { name: kernel.event_listener, event: kernel.request, method: handleRequest }
- { name: kernel.event_listener, event: kernel.response, method: handleResponse }
mobile_detect.twig.extension:
class: %mobile_detect.twig.extension.class%
- arguments: [ "@mobile_detect.mobile_detector" ]
+ calls:
+ - [ setMobileDetector, [ "@mobile_detect.mobile_detector"] ]
strict: false
tags:
- { name: twig.extension }
@@ -15,10 +15,10 @@ class MobileDetectExtension extends Twig_Extension
private $mobileDetector;
/**
- * Constructor
- * @param \SunCat\MobileDetectBundle\DeviceDetector\MobileDetector $mobileDetector
+ * Set mobile detector
+ * @param MobileDetector $mobileDetector
*/
- public function __construct(MobileDetector $mobileDetector)
+ public function setMobileDetector(MobileDetector $mobileDetector)
{
$this->mobileDetector = $mobileDetector;
}

0 comments on commit 73e7807

Please sign in to comment.