Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
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 |
FragmentHandler.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 use Symfony\Component\HttpFoundation\Request;
015 use Symfony\Component\HttpFoundation\Response;
016 use Symfony\Component\HttpFoundation\StreamedResponse;
017 use Symfony\Component\HttpFoundation\RequestStack;
018 use Symfony\Component\HttpKernel\Controller\ControllerReference;
019
020 /**
021 * Renders a URI that represents a resource fragment.
022 *
023 * This class handles the rendering of resource fragments that are included into
024 * a main resource. The handling of the rendering is managed by specialized renderers.
025 *
026 * This listener works in 2 modes:
027 *
028 * * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
029 * * 2.4+ mode where you must pass a RequestStack instance in the constructor.
030 *
031 * @author Fabien Potencier <fabien@symfony.com>
032 *
033 * @see FragmentRendererInterface
034 */
035 class FragmentHandler
036 {
037 private $debug;
038 private $renderers = array();
039 private $request;
040 private $requestStack;
041
042 /**
043 * Constructor.
044 *
045 * RequestStack will become required in 3.0.
046 *
047 * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
048 * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
049 * @param bool $debug Whether the debug mode is enabled or not
050 */
051 public function __construct($requestStack = null, $renderers = array(), $debug = false)
052 {
053 if (is_array($requestStack)) {
054 $tmp = $debug;
055 $debug = func_num_args() < 2 ? false : $renderers;
056 $renderers = $requestStack;
057 $requestStack = func_num_args() < 3 ? null : $tmp;
058
059 @trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as first argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
060 } elseif (!$requestStack instanceof RequestStack) {
061 @trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
062 }
063
064 if (null !== $requestStack && !$requestStack instanceof RequestStack) {
065 throw new \InvalidArgumentException('RequestStack instance expected.');
066 }
067 if (!is_array($renderers)) {
068 throw new \InvalidArgumentException('Renderers must be an array.');
069 }
070
071 $this->requestStack = $requestStack;
072 foreach ($renderers as $renderer) {
073 $this->addRenderer($renderer);
074 }
075 $this->debug = $debug;
076 }
077
078 /**
079 * Adds a renderer.
080 *
081 * @param FragmentRendererInterface $renderer A FragmentRendererInterface instance
082 */
083 public function addRenderer(FragmentRendererInterface $renderer)
084 {
085 $this->renderers[$renderer->getName()] = $renderer;
086 }
087
088 /**
089 * Sets the current Request.
090 *
091 * This method was used to synchronize the Request, but as the HttpKernel
092 * is doing that automatically now, you should never call it directly.
093 * It is kept public for BC with the 2.3 version.
094 *
095 * @param Request|null $request A Request instance
096 *
097 * @deprecated since version 2.4, to be removed in 3.0.
098 */
099 public function setRequest(Request $request = null)
100 {
101 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be removed in 3.0.', E_USER_DEPRECATED);
102
103 $this->request = $request;
104 }
105
106 /**
107 * Renders a URI and returns the Response content.
108 *
109 * Available options:
110 *
111 * * ignore_errors: true to return an empty string in case of an error
112 *
113 * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
114 * @param string $renderer The renderer name
115 * @param array $options An array of options
116 *
117 * @return string|null The Response content or null when the Response is streamed
118 *
119 * @throws \InvalidArgumentException when the renderer does not exist
120 * @throws \LogicException when no master request is being handled
121 */
122 public function render($uri, $renderer = 'inline', array $options = array())
123 {
124 if (!isset($options['ignore_errors'])) {
125 $options['ignore_errors'] = !$this->debug;
126 }
127
128 if (!isset($this->renderers[$renderer])) {
129 throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
130 }
131
132 if (!$request = $this->getRequest()) {
133 throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
134 }
135
136 return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
137 }
138
139 /**
140 * Delivers the Response as a string.
141 *
142 * When the Response is a StreamedResponse, the content is streamed immediately
143 * instead of being returned.
144 *
145 * @param Response $response A Response instance
146 *
147 * @return string|null The Response content or null when the Response is streamed
148 *
149 * @throws \RuntimeException when the Response is not successful
150 */
151 protected function deliver(Response $response)
152 {
153 if (!$response->isSuccessful()) {
154 throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->getRequest()->getUri(), $response->getStatusCode()));
155 }
156
157 if (!$response instanceof StreamedResponse) {
158 return $response->getContent();
159 }
160
161 $response->sendContent();
162 }
163
164 private function getRequest()
165 {
166 return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request;
167 }
168 }
169