You are here

class HIncludeFragmentRenderer in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/http-kernel/Fragment/HIncludeFragmentRenderer.php \Symfony\Component\HttpKernel\Fragment\HIncludeFragmentRenderer

Implements the Hinclude rendering strategy.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of HIncludeFragmentRenderer

1 file declares its use of HIncludeFragmentRenderer
HIncludeFragmentRendererTest.php in vendor/symfony/http-kernel/Tests/Fragment/HIncludeFragmentRendererTest.php

File

vendor/symfony/http-kernel/Fragment/HIncludeFragmentRenderer.php, line 25

Namespace

Symfony\Component\HttpKernel\Fragment
View source
class HIncludeFragmentRenderer extends RoutableFragmentRenderer {
  private $globalDefaultTemplate;
  private $signer;
  private $templating;
  private $charset;

  /**
   * Constructor.
   *
   * @param EngineInterface|\Twig_Environment $templating            An EngineInterface or a \Twig_Environment instance
   * @param UriSigner                         $signer                A UriSigner instance
   * @param string                            $globalDefaultTemplate The global default content (it can be a template name or the content)
   * @param string                            $charset
   */
  public function __construct($templating = null, UriSigner $signer = null, $globalDefaultTemplate = null, $charset = 'utf-8') {
    $this
      ->setTemplating($templating);
    $this->globalDefaultTemplate = $globalDefaultTemplate;
    $this->signer = $signer;
    $this->charset = $charset;
  }

  /**
   * Sets the templating engine to use to render the default content.
   *
   * @param EngineInterface|\Twig_Environment|null $templating An EngineInterface or a \Twig_Environment instance
   *
   * @throws \InvalidArgumentException
   */
  public function setTemplating($templating) {
    if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof \Twig_Environment) {
      throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of \\Twig_Environment or Symfony\\Component\\Templating\\EngineInterface');
    }
    $this->templating = $templating;
  }

  /**
   * Checks if a templating engine has been set.
   *
   * @return bool true if the templating engine has been set, false otherwise
   */
  public function hasTemplating() {
    return null !== $this->templating;
  }

  /**
   * {@inheritdoc}
   *
   * Additional available options:
   *
   *  * default:    The default content (it can be a template name or the content)
   *  * id:         An optional hx:include tag id attribute
   *  * attributes: An optional array of hx:include tag attributes
   */
  public function render($uri, Request $request, array $options = array()) {
    if ($uri instanceof ControllerReference) {
      if (null === $this->signer) {
        throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
      }

      // we need to sign the absolute URI, but want to return the path only.
      $uri = substr($this->signer
        ->sign($this
        ->generateFragmentUri($uri, $request, true)), strlen($request
        ->getSchemeAndHttpHost()));
    }

    // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
    $uri = str_replace('&', '&amp;', $uri);
    $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
    if (null !== $this->templating && $template && $this
      ->templateExists($template)) {
      $content = $this->templating
        ->render($template);
    }
    else {
      $content = $template;
    }
    $attributes = isset($options['attributes']) && is_array($options['attributes']) ? $options['attributes'] : array();
    if (isset($options['id']) && $options['id']) {
      $attributes['id'] = $options['id'];
    }
    $renderedAttributes = '';
    if (count($attributes) > 0) {
      if (PHP_VERSION_ID >= 50400) {
        $flags = ENT_QUOTES | ENT_SUBSTITUTE;
      }
      else {
        $flags = ENT_QUOTES;
      }
      foreach ($attributes as $attribute => $value) {
        $renderedAttributes .= sprintf(' %s="%s"', htmlspecialchars($attribute, $flags, $this->charset, false), htmlspecialchars($value, $flags, $this->charset, false));
      }
    }
    return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
  }

  /**
   * @param string $template
   *
   * @return bool
   */
  private function templateExists($template) {
    if ($this->templating instanceof EngineInterface) {
      try {
        return $this->templating
          ->exists($template);
      } catch (\InvalidArgumentException $e) {
        return false;
      }
    }
    $loader = $this->templating
      ->getLoader();
    if ($loader instanceof \Twig_ExistsLoaderInterface) {
      return $loader
        ->exists($template);
    }
    try {
      $loader
        ->getSource($template);
      return true;
    } catch (\Twig_Error_Loader $e) {
    }
    return false;
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return 'hinclude';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HIncludeFragmentRenderer::$charset private property
HIncludeFragmentRenderer::$globalDefaultTemplate private property
HIncludeFragmentRenderer::$signer private property
HIncludeFragmentRenderer::$templating private property
HIncludeFragmentRenderer::getName public function Gets the name of the strategy. Overrides FragmentRendererInterface::getName
HIncludeFragmentRenderer::hasTemplating public function Checks if a templating engine has been set.
HIncludeFragmentRenderer::render public function Additional available options: Overrides FragmentRendererInterface::render
HIncludeFragmentRenderer::setTemplating public function Sets the templating engine to use to render the default content.
HIncludeFragmentRenderer::templateExists private function
HIncludeFragmentRenderer::__construct public function Constructor.
RoutableFragmentRenderer::$fragmentPath private property
RoutableFragmentRenderer::checkNonScalar private function
RoutableFragmentRenderer::generateFragmentUri protected function Generates a fragment URI for a given controller.
RoutableFragmentRenderer::setFragmentPath public function Sets the fragment path that triggers the fragment listener.