Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
HIncludeFragmentRenderer.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpKernel\Fragment;
013
014 if (!defined('ENT_SUBSTITUTE')) {
015 define('ENT_SUBSTITUTE', 8);
016 }
017
018 use Symfony\Component\HttpFoundation\Request;
019 use Symfony\Component\HttpFoundation\Response;
020 use Symfony\Component\Templating\EngineInterface;
021 use Symfony\Component\HttpKernel\Controller\ControllerReference;
022 use Symfony\Component\HttpKernel\UriSigner;
023
024 /**
025 * Implements the Hinclude rendering strategy.
026 *
027 * @author Fabien Potencier <fabien@symfony.com>
028 */
029 class HIncludeFragmentRenderer extends RoutableFragmentRenderer
030 {
031 private $globalDefaultTemplate;
032 private $signer;
033 private $templating;
034 private $charset;
035
036 /**
037 * Constructor.
038 *
039 * @param EngineInterface|\Twig_Environment $templating An EngineInterface or a \Twig_Environment instance
040 * @param UriSigner $signer A UriSigner instance
041 * @param string $globalDefaultTemplate The global default content (it can be a template name or the content)
042 * @param string $charset
043 */
044 public function __construct($templating = null, UriSigner $signer = null, $globalDefaultTemplate = null, $charset = 'utf-8')
045 {
046 $this->setTemplating($templating);
047 $this->globalDefaultTemplate = $globalDefaultTemplate;
048 $this->signer = $signer;
049 $this->charset = $charset;
050 }
051
052 /**
053 * Sets the templating engine to use to render the default content.
054 *
055 * @param EngineInterface|\Twig_Environment|null $templating An EngineInterface or a \Twig_Environment instance
056 *
057 * @throws \InvalidArgumentException
058 */
059 public function setTemplating($templating)
060 {
061 if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof \Twig_Environment) {
062 throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of \Twig_Environment or Symfony\Component\Templating\EngineInterface');
063 }
064
065 $this->templating = $templating;
066 }
067
068 /**
069 * Checks if a templating engine has been set.
070 *
071 * @return bool true if the templating engine has been set, false otherwise
072 */
073 public function hasTemplating()
074 {
075 return null !== $this->templating;
076 }
077
078 /**
079 * {@inheritdoc}
080 *
081 * Additional available options:
082 *
083 * * default: The default content (it can be a template name or the content)
084 * * id: An optional hx:include tag id attribute
085 * * attributes: An optional array of hx:include tag attributes
086 */
087 public function render($uri, Request $request, array $options = array())
088 {
089 if ($uri instanceof ControllerReference) {
090 if (null === $this->signer) {
091 throw new \LogicException('You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.');
092 }
093
094 // we need to sign the absolute URI, but want to return the path only.
095 $uri = substr($this->signer->sign($this->generateFragmentUri($uri, $request, true)), strlen($request->getSchemeAndHttpHost()));
096 }
097
098 // We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
099 $uri = str_replace('&', '&', $uri);
100
101 $template = isset($options['default']) ? $options['default'] : $this->globalDefaultTemplate;
102 if (null !== $this->templating && $template && $this->templateExists($template)) {
103 $content = $this->templating->render($template);
104 } else {
105 $content = $template;
106 }
107
108 $attributes = isset($options['attributes']) && is_array($options['attributes']) ? $options['attributes'] : array();
109 if (isset($options['id']) && $options['id']) {
110 $attributes['id'] = $options['id'];
111 }
112 $renderedAttributes = '';
113 if (count($attributes) > 0) {
114 foreach ($attributes as $attribute => $value) {
115 $renderedAttributes .= sprintf(
116 ' %s="%s"',
117 htmlspecialchars($attribute, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false),
118 htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset, false)
119 );
120 }
121 }
122
123 return new Response(sprintf('<hx:include src="%s"%s>%s</hx:include>', $uri, $renderedAttributes, $content));
124 }
125
126 /**
127 * @param string $template
128 *
129 * @return bool
130 */
131 private function templateExists($template)
132 {
133 if ($this->templating instanceof EngineInterface) {
134 try {
135 return $this->templating->exists($template);
136 } catch (\InvalidArgumentException $e) {
137 return false;
138 }
139 }
140
141 $loader = $this->templating->getLoader();
142 if ($loader instanceof \Twig_ExistsLoaderInterface) {
143 return $loader->exists($template);
144 }
145
146 try {
147 $loader->getSource($template);
148
149 return true;
150 } catch (\Twig_Error_Loader $e) {
151 }
152
153 return false;
154 }
155
156 /**
157 * {@inheritdoc}
158 */
159 public function getName()
160 {
161 return 'hinclude';
162 }
163 }
164