AsseticBundle\Service::getStrategyForRenderer PHP Method

getStrategyForRenderer() public method

Get strategy to setup assets for given $renderer.
public getStrategyForRenderer ( Zend\View\Renderer\RendererInterface $renderer ) : AsseticBundle\View\StrategyInterface | null
$renderer Zend\View\Renderer\RendererInterface
return AsseticBundle\View\StrategyInterface | null
    public function getStrategyForRenderer(Renderer $renderer)
    {
        if (!$this->hasStrategyForRenderer($renderer)) {
            return null;
        }
        $rendererName = $this->getRendererName($renderer);
        if (!isset($this->strategy[$rendererName])) {
            $strategyClass = $this->configuration->getStrategyNameForRenderer($rendererName);
            if (!class_exists($strategyClass, true)) {
                throw new Exception\InvalidArgumentException(sprintf('strategy class "%s" dosen\'t exists', $strategyClass));
            }
            $instance = new $strategyClass();
            if (!$instance instanceof StrategyInterface) {
                throw new Exception\DomainException(sprintf('strategy class "%s" is not instanceof "AsseticBundle\\View\\StrategyInterface"', $strategyClass));
            }
            $this->strategy[$rendererName] = $instance;
        }
        /** @var $strategy \AsseticBundle\View\StrategyInterface */
        $strategy = $this->strategy[$rendererName];
        $strategy->setBaseUrl($this->configuration->getBaseUrl());
        $strategy->setBasePath($this->configuration->getBasePath());
        $strategy->setDebug($this->configuration->isDebug());
        $strategy->setCombine($this->configuration->isCombine());
        $strategy->setRenderer($renderer);
        return $strategy;
    }

Usage Example

Example #1
0
 public function testGetStrategyForRendererSuccess()
 {
     $renderer = $this->getMockBuilder('Zend\\View\\Renderer\\PhpRenderer')->disableOriginalConstructor()->getMock();
     $this->object->getConfiguration()->addRendererToStrategy(get_class($renderer), 'AsseticBundle\\View\\NoneStrategy');
     $value = $this->object->getStrategyForRenderer($renderer);
     $this->assertInstanceOf('AsseticBundle\\View\\StrategyInterface', $value);
 }