‪TYPO3CMS  10.4
MiddlewareDispatcher.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
20 use Psr\Container\ContainerInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Server\MiddlewareInterface;
24 use Psr\Http\Server\RequestHandlerInterface;
26 
34 class ‪MiddlewareDispatcher implements RequestHandlerInterface
35 {
41  protected ‪$tip;
42 
46  protected ‪$container;
47 
53  public function ‪__construct(
54  RequestHandlerInterface $kernel,
55  iterable $middlewares = [],
56  ContainerInterface ‪$container = null
57  ) {
58  $this->container = ‪$container;
59  $this->‪seedMiddlewareStack($kernel);
60 
61  foreach ($middlewares as $middleware) {
62  if (is_string($middleware)) {
63  $this->‪lazy($middleware);
64  } else {
65  $this->‪add($middleware);
66  }
67  }
68  }
69 
76  public function ‪handle(ServerRequestInterface $request): ResponseInterface
77  {
78  return $this->tip->handle($request);
79  }
80 
86  protected function ‪seedMiddlewareStack(RequestHandlerInterface $kernel)
87  {
88  $this->tip = $kernel;
89  }
90 
100  public function ‪add(MiddlewareInterface $middleware)
101  {
102  $next = ‪$this->tip;
103  $this->tip = new class($middleware, $next) implements RequestHandlerInterface {
107  private $middleware;
111  private $next;
112 
113  public function ‪__construct(MiddlewareInterface $middleware, RequestHandlerInterface $next)
114  {
115  $this->middleware = $middleware;
116  $this->next = $next;
117  }
118 
119  public function ‪handle(ServerRequestInterface $request): ResponseInterface
120  {
121  return $this->middleware->process($request, $this->next);
122  }
123  };
124  }
125 
135  public function ‪lazy(string $middleware): void
136  {
137  $next = ‪$this->tip;
138  $this->tip = new class($middleware, $next, ‪$this->container) implements RequestHandlerInterface {
142  private $middleware;
143 
147  private $next;
148 
152  private ‪$container;
153 
154  public function ‪__construct(string $middleware, RequestHandlerInterface $next, ContainerInterface ‪$container = null)
155  {
156  $this->middleware = $middleware;
157  $this->next = $next;
158  $this->container = ‪$container;
159  }
160 
161  public function ‪handle(ServerRequestInterface $request): ResponseInterface
162  {
163  if ($this->container !== null && $this->container->has($this->middleware)) {
164  $middleware = $this->container->get($this->middleware);
165  } else {
166  $middleware = GeneralUtility::makeInstance($this->middleware);
167  }
168 
169  if (!$middleware instanceof MiddlewareInterface) {
170  throw new \InvalidArgumentException(get_class($middleware) . ' does not implement ' . MiddlewareInterface::class, 1516821342);
171  }
172  return $middleware->process($request, $this->next);
173  }
174  };
175  }
176 }
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\add
‪add(MiddlewareInterface $middleware)
Definition: MiddlewareDispatcher.php:98
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\handle
‪ResponseInterface handle(ServerRequestInterface $request)
Definition: MiddlewareDispatcher.php:74
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\lazy
‪lazy(string $middleware)
Definition: MiddlewareDispatcher.php:131
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\seedMiddlewareStack
‪seedMiddlewareStack(RequestHandlerInterface $kernel)
Definition: MiddlewareDispatcher.php:84
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\$container
‪ContainerInterface $container
Definition: MiddlewareDispatcher.php:44
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\$tip
‪RequestHandlerInterface $tip
Definition: MiddlewareDispatcher.php:40
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\__construct
‪__construct(RequestHandlerInterface $kernel, iterable $middlewares=[], ContainerInterface $container=null)
Definition: MiddlewareDispatcher.php:51
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher
Definition: MiddlewareDispatcher.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18