Пример #1
0
 /**
  * Invoke the given action using the given Request
  * 
  * @param Request $request
  * @param string  $action
  */
 public function invokeAction(Request $request, $action, $params = array())
 {
     $controllers = $this->getControllers($action, $params, array_merge($request->query->all(), $request->request->all()));
     foreach ($controllers as $controller) {
         $this->handler->render($controller);
     }
 }
 public function testUnknownFragmentRenderer()
 {
     $context = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')->disableOriginalConstructor()->getMock();
     $renderer = new FragmentHandler($context);
     $this->setExpectedException('InvalidArgumentException', 'The "inline" renderer does not exist.');
     $renderer->render('/foo');
 }
 public function esi(ControllerReference $controller)
 {
     $controller->attributes = array_map(function ($value) {
         return $this->getRefId($value, true);
     }, $controller->attributes);
     return $this->fragmentHandler->render($controller, 'esi', ['standalone' => true]);
 }
Пример #4
0
 /**
  * @param string $place
  * @param array $attributes
  * @param array $options
  *
  * @return string
  */
 public function widgets($place, array $attributes = [], array $options = [])
 {
     $result = '';
     foreach ($this->widgets->getWidgetsForPlace($place) as $controller) {
         $result .= $this->handler->render(new ControllerReference($controller, $attributes, []), 'hinclude', $options);
     }
     return $result;
 }
 protected function getFragmentHandler($return)
 {
     $strategy = $this->getMock('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface');
     $strategy->expects($this->once())->method('getName')->will($this->returnValue('inline'));
     $strategy->expects($this->once())->method('render')->will($return);
     $renderer = new FragmentHandler(array($strategy));
     $renderer->setRequest(Request::create('/'));
     return $renderer;
 }
 protected function getFragmentHandler($return)
 {
     $strategy = $this->getMock('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface');
     $strategy->expects($this->once())->method('getName')->will($this->returnValue('inline'));
     $strategy->expects($this->once())->method('render')->will($return);
     // simulate a master request
     $event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')->disableOriginalConstructor()->getMock();
     $event->expects($this->once())->method('getRequest')->will($this->returnValue(Request::create('/')));
     $renderer = new FragmentHandler(array($strategy));
     $renderer->onKernelRequest($event);
     return $renderer;
 }
 protected function getHandler($returnValue, $arguments = array())
 {
     $renderer = $this->getMock('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface');
     $renderer->expects($this->any())->method('getName')->will($this->returnValue('foo'));
     $e = $renderer->expects($this->any())->method('render')->will($returnValue);
     if ($arguments) {
         call_user_func_array(array($e, 'with'), $arguments);
     }
     $handler = new FragmentHandler(array(), null, $this->requestStack);
     $handler->addRenderer($renderer);
     return $handler;
 }
Пример #8
0
 /**
  * @param FragmentHandler|ContainerInterface $handler
  *
  * @deprecated Passing a ContainerInterface as a first argument is deprecated since 2.7 and will be removed in 3.0.
  */
 public function __construct($handler)
 {
     if ($handler instanceof FragmentHandler) {
         $this->handler = $handler;
     } elseif ($handler instanceof ContainerInterface) {
         @trigger_error('The ability to pass a ContainerInterface instance as a first argument to ' . __METHOD__ . ' method is deprecated since version 2.7 and will be removed in 3.0. Pass a FragmentHandler instance instead.', E_USER_DEPRECATED);
         $this->handler = $handler->get('fragment.handler');
     } else {
         throw new \BadFunctionCallException(sprintf('%s takes a FragmentHandler or a ContainerInterface object as its first argument.', __METHOD__));
     }
     $this->handler = $handler;
 }
 /**
  * {@inheritdoc}
  */
 public function execute(BlockContextInterface $blockContext, Response $response = null)
 {
     /** @var $block ActionBlock */
     $block = $blockContext->getBlock();
     if (!$block->getActionName()) {
         throw new \RuntimeException(sprintf('ActionBlock with id "%s" does not have an action name defined, implement a default or persist it in the document.', $block->getId()));
     }
     if (!$block->getEnabled()) {
         return new Response();
     }
     $requestParams = $block->resolveRequestParams($this->request, $blockContext);
     return new Response($this->renderer->render(new ControllerReference($block->getActionName(), $requestParams)));
 }
 public function testExecutionOfEnabledBlock()
 {
     $actionBlock = new ActionBlock();
     $actionBlock->setEnabled(true);
     $actionBlock->setActionName('CmfBlockBundle:Test:test');
     $content = "Rendered Action Block.";
     $request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request');
     $this->kernel->expects($this->once())->method('render')->will($this->returnValue($content));
     $actionBlockService = new ActionBlockService('test-service', $this->templating, $this->kernel);
     $actionBlockService->setRequest($request);
     $response = $actionBlockService->execute(new BlockContext($actionBlock));
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
     $this->assertEquals($content, $response->getContent());
 }
 protected function getHandler($returnValue, $arguments = array())
 {
     $renderer = $this->getMock('Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface');
     $renderer->expects($this->any())->method('getName')->will($this->returnValue('foo'));
     $e = $renderer->expects($this->any())->method('render')->will($returnValue);
     if ($arguments) {
         call_user_func_array(array($e, 'with'), $arguments);
     }
     $handler = new FragmentHandler();
     $handler->addRenderer($renderer);
     $event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')->disableOriginalConstructor()->getMock();
     $event->expects($this->once())->method('getRequest')->will($this->returnValue(Request::create('/')));
     $handler->onKernelRequest($event);
     return $handler;
 }
 /**
  * Get the view for the placeholder
  *
  * @param array  $context Passed automatically, when needs_context is set to TRUE
  * @param string $key
  *
  * @return string
  */
 public function getPlaceholder($context, $key)
 {
     if (!array_key_exists('layout', $context)) {
         return;
     }
     $layouts = $context['layout']->getLayoutsAt($key);
     if (!$layouts) {
         return;
     }
     $content = '';
     foreach ($layouts as $sublayout) {
         $context['layout'] = $sublayout;
         // If the sublayout has content, replace the context's content with
         // the sublayout's content
         if ($sublayout->getContent()) {
             $layoutContent = $this->contentManager->getRepository()->find($sublayout->getContent());
             $context['content'] = $layoutContent;
         }
         // If the sublayout has parameters, set the parameter data to the context
         if ($sublayout->getParameters()) {
             $context['parameters'] = $sublayout->getParameters();
         }
         // If the sublayout has an action, call the controller action before rendering.
         // Else, just render the template directly
         if ($sublayout->getAction()) {
             $action = new ControllerReference($sublayout->getAction(), $context, []);
             $content .= $this->fragmentHandler->render($action);
         } else {
             $content .= $this->twig->render($sublayout->getFilename(), $context);
         }
     }
     return $content;
 }
Пример #13
0
 /**
  * {@inheritdoc}
  */
 public function render($uri, $renderer = 'inline', array $options = array())
 {
     if (isset($this->rendererIds[$renderer])) {
         $this->addRenderer($this->container->get($this->rendererIds[$renderer]));
         unset($this->rendererIds[$renderer]);
     }
     return parent::render($uri, $renderer, $options);
 }
Пример #14
0
 /**
  * {@inheritdoc}
  */
 public function get(array $keys)
 {
     $this->validateKeys($keys);
     $keys['_token'] = $this->computeHash($keys);
     $controllerReference = new ControllerReference('cmf.block.cache.varnish:cacheAction', $keys);
     $content = $this->fragmentHandler->render($controllerReference, 'esi');
     return new CacheElement($keys, new Response($content));
 }
 public function testInitCache()
 {
     $router = $this->getMock('Symfony\\Component\\Routing\\RouterInterface');
     $router->expects($this->any())->method('generate')->will($this->returnValue('http://cmf.symfony.com/symfony-cmf/block/cache/varnish/XXX/%2Fcms%2Fcontent%2Fhome%2FadditionalInfoBlock?updated_at=as'));
     $blockRenderer = $this->getMock('Sonata\\BlockBundle\\Block\\BlockRendererInterface');
     $blockLoader = $this->getMock('Sonata\\BlockBundle\\Block\\BlockLoaderInterface');
     $blockContextManager = $this->getMock('Sonata\\BlockBundle\\Block\\BlockContextManagerInterface');
     $content = '<esi:include src="http://cmf.symfony.com/symfony-cmf/block/cache/varnish/XXX/%2Fcms%2Fcontent%2Fhome%2FadditionalInfoBlock?updated_at=as';
     $this->fragmentHandler->expects($this->once())->method('render')->will($this->returnValue($content));
     $cache = new BlockVarnishCache('My Token', $router, $blockRenderer, $blockLoader, $blockContextManager, $this->fragmentHandler, array(), 'ban');
     $this->assertTrue($cache->flush(array()));
     $this->assertTrue($cache->flushAll());
     $keys = array('block_id' => '/cms/content/home/additionalInfoBlock', 'updated_at' => 'as');
     $cacheElement = $cache->set($keys, 'data');
     $this->assertInstanceOf('Sonata\\Cache\\CacheElement', $cacheElement);
     $this->assertTrue($cache->has(array('id' => 7)));
     $cacheElement = $cache->get($keys);
     $this->assertInstanceOf('Sonata\\Cache\\CacheElement', $cacheElement);
     $this->assertEquals($content, $cacheElement->getData()->getContent());
 }
Пример #16
0
 /**
  * Process embed tags for a single tag type (embed or embed-inline)
  * @param \DOMDocument $xmlDoc
  * @param $tagName string name of the tag to extract
  */
 protected function processTag(DOMDocument $xmlDoc, $tagName)
 {
     /** @var $embed \DOMElement */
     foreach ($xmlDoc->getElementsByTagName($tagName) as $embed) {
         if (!($view = $embed->getAttribute("view"))) {
             $view = $tagName;
         }
         $embedContent = null;
         $parameters = $this->getParameters($embed);
         if ($contentId = $embed->getAttribute("object_id")) {
             try {
                 /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
                 $content = $this->repository->sudo(function (Repository $repository) use($contentId) {
                     return $repository->getContentService()->loadContent($contentId);
                 });
                 if (!$this->repository->canUser('content', 'read', $content) && !$this->repository->canUser('content', 'view_embed', $content)) {
                     throw new UnauthorizedException('content', 'read', array('contentId' => $contentId));
                 }
                 // Check published status of the Content
                 if ($content->getVersionInfo()->status !== APIVersionInfo::STATUS_PUBLISHED && !$this->repository->canUser('content', 'versionread', $content)) {
                     throw new UnauthorizedException('content', 'versionread', array('contentId' => $contentId));
                 }
                 $embedContent = $this->fragmentHandler->render(new ControllerReference('ez_content:embedContent', array('contentId' => $contentId, 'viewType' => $view, 'layout' => false, 'params' => $parameters)));
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->error("While generating embed for xmltext, could not locate " . "Content object with ID " . $contentId);
                 }
             }
         } else {
             if ($locationId = $embed->getAttribute("node_id")) {
                 try {
                     /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
                     $location = $this->repository->sudo(function (Repository $repository) use($locationId) {
                         return $repository->getLocationService()->loadLocation($locationId);
                     });
                     if (!$this->repository->canUser('content', 'read', $location->getContentInfo(), $location) && !$this->repository->canUser('content', 'view_embed', $location->getContentInfo(), $location)) {
                         throw new UnauthorizedException('content', 'read', array('locationId' => $location->id));
                     }
                     $embedContent = $this->fragmentHandler->render(new ControllerReference('ez_content:embedLocation', array('locationId' => $locationId, 'viewType' => $view, 'layout' => false, 'params' => $parameters)));
                 } catch (APINotFoundException $e) {
                     if ($this->logger) {
                         $this->logger->error("While generating embed for xmltext, could not locate " . "Location with ID " . $locationId);
                     }
                 }
             }
         }
         if ($embedContent === null) {
             // Remove empty embed
             $embed->parentNode->removeChild($embed);
         } else {
             $embed->appendChild($xmlDoc->createCDATASection($embedContent));
         }
     }
 }
Пример #17
0
 public function testInvokeAction()
 {
     $controllerRefCount = 0;
     $controllerList = array('C1', 'B1', 'A5', 'D2');
     $action = 'awesomeAction';
     $params = array('goo' => 'boo');
     $request = new Request($query = array('foo' => 'bar', 'baz' => 'shoo'), $requestParams = array('a' => 'kaboom', 'd' => 'achoo'));
     $memberRequest = new Request(array(), array(), $attributes = array('app' => 'foobarbaz'));
     $this->sut->setRequest($memberRequest);
     $mergedParams = array_merge(array(), $params, $attributes);
     $mergedQuery = array_merge(array(), $query, $requestParams);
     $this->controllerManager->expects($this->any())->method('getControllers')->with($action)->will($this->returnValue($controllerList));
     $controllersAdded = array();
     $this->fragmentHandler->expects($this->exactly(count($controllerList)))->method('render')->with($this->logicalAnd($this->isInstanceOf('Symfony\\Component\\HttpKernel\\Controller\\ControllerReference'), $this->callback(function (ControllerReference $cr) use(&$controllersAdded, $controllerList, $mergedParams, $mergedQuery) {
         $controllersAdded[] = $cr->controller;
         // XXX PHPUnit bug prevents us from doing an order-based test here
         // TODO upgrade PHPUnit
         return in_array($cr->controller, $controllerList) && count(array_diff_assoc($mergedParams, $cr->attributes)) < 1 && count(array_diff_assoc($mergedQuery, $cr->query)) < 1;
     })));
     $this->sut->invokeAction($request, $action, $params);
     $this->assertEmpty(array_diff($controllerList, array_unique($controllersAdded)));
 }
 /**
  * @param \Twig_Environment $env
  * @param $context
  * @param TwigTemplateEvent $event
  *
  * @return string
  */
 protected function render(\Twig_Environment $env, $context, TwigTemplateEvent $event)
 {
     $codes = $event->getCodes();
     $compiled = '';
     if (count($codes)) {
         foreach ($codes as $code) {
             if ($code instanceof TwigEventInclude) {
                 $compiled .= $env->resolveTemplate($code->getTemplate())->render(array_replace_recursive($context, $code->getParameters()));
                 continue;
             }
             if ($code instanceof TwigEventString) {
                 $compiled .= $env->render($code->getTemplateString(), array_replace_recursive($context, $code->getParameters()));
                 continue;
             }
             if ($code instanceof TwigEventRender) {
                 $reference = new ControllerReference($code->getController(), $code->getAttributes(), $code->getQuery());
                 $compiled .= $this->fragment->render($reference, $code->getStrategy());
                 continue;
             }
         }
     }
     return $compiled;
 }
Пример #19
0
 /**
  * Process embed tags for a single tag type (embed or embed-inline)
  * @param \DOMDocument $xmlDoc
  * @param $tagName string name of the tag to extract
  */
 protected function processTag(DOMDocument $xmlDoc, $tagName)
 {
     /** @var $embed \DOMElement */
     foreach ($xmlDoc->getElementsByTagName($tagName) as $embed) {
         if (!($view = $embed->getAttribute("view"))) {
             $view = $tagName;
         }
         $embedContent = null;
         $parameters = array("noLayout" => true, "objectParameters" => array());
         foreach ($embed->attributes as $attribute) {
             // We only consider tags in the custom namespace, and skip disallowed names
             if (!isset($this->excludedAttributes[$attribute->localName])) {
                 $parameters["objectParameters"][$attribute->localName] = $attribute->nodeValue;
             }
         }
         if ($contentId = $embed->getAttribute("object_id")) {
             try {
                 /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */
                 $content = $this->repository->sudo(function (Repository $repository) use($contentId) {
                     return $repository->getContentService()->loadContent($contentId);
                 });
                 if (!$this->repository->canUser('content', 'read', $content) && !$this->repository->canUser('content', 'view_embed', $content)) {
                     throw new UnauthorizedException('content', 'read', array('contentId' => $contentId));
                 }
                 // Check published status of the Content
                 if ($content->getVersionInfo()->status !== APIVersionInfo::STATUS_PUBLISHED && !$this->repository->canUser('content', 'versionread', $content)) {
                     throw new UnauthorizedException('content', 'versionread', array('contentId' => $contentId));
                 }
                 $embedContent = $this->fragmentHandler->render(new ControllerReference('ez_content:embedContent', array('contentId' => $contentId, 'viewType' => $view, 'layout' => false, 'params' => $parameters)));
             } catch (APINotFoundException $e) {
                 if ($this->logger) {
                     $this->logger->error("While generating embed for xmltext, could not locate " . "Content object with ID " . $contentId);
                 }
             }
         } else {
             if ($locationId = $embed->getAttribute("node_id")) {
                 try {
                     /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
                     $location = $this->repository->sudo(function (Repository $repository) use($locationId) {
                         return $repository->getLocationService()->loadLocation($locationId);
                     });
                     if (!$this->repository->canUser('content', 'read', $location->getContentInfo(), $location) && !$this->repository->canUser('content', 'view_embed', $location->getContentInfo(), $location)) {
                         throw new UnauthorizedException('content', 'read', array('locationId' => $location->id));
                     }
                     $embedContent = $this->fragmentHandler->render(new ControllerReference('ez_content:embedLocation', array('locationId' => $locationId, 'viewType' => $view, 'layout' => false, 'params' => $parameters)));
                 } catch (APINotFoundException $e) {
                     if ($this->logger) {
                         $this->logger->error("While generating embed for xmltext, could not locate " . "Location with ID " . $locationId);
                     }
                 }
             }
         }
         if ($embedContent === null) {
             // Remove tmp paragraph
             if ($embed->parentNode->lookupNamespaceUri('tmp') !== null) {
                 $embed->parentNode->parentNode->removeChild($embed->parentNode);
             } else {
                 if ($embed->parentNode->localName === "link" && $embed->parentNode->childNodes->length === 1) {
                     // Remove paragraph with empty link
                     if ($embed->parentNode->parentNode->localName === "paragraph" && $embed->parentNode->parentNode->childNodes->length === 1) {
                         $embed->parentNode->parentNode->parentNode->removeChild($embed->parentNode->parentNode);
                     } else {
                         $embed->parentNode->parentNode->removeChild($embed->parentNode);
                     }
                 } else {
                     $embed->parentNode->removeChild($embed);
                 }
             }
         } else {
             $embed->appendChild($xmlDoc->createCDATASection($embedContent));
         }
     }
 }
Пример #20
0
 /**
  * Renders an action
  *
  * @param string|ControllerReference $uri
  *
  * @return string
  */
 public function render($uri)
 {
     return $this->handler->render($uri);
 }
Пример #21
0
 /**
  * @param BlockInterface $block
  *
  * @return null|string
  */
 public function renderEsi(BlockInterface $block)
 {
     $reference = new ControllerReference('OpiferContentBundle:Frontend/Block:view', ['id' => $block->getId()], $this->getRequest()->query->all());
     return $this->fragmentHandler->render($reference, 'esi');
 }