‪TYPO3CMS  ‪main
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 
48  public function ‪__construct(
49  RequestHandlerInterface $kernel,
50  iterable $middlewares = [],
51  ContainerInterface ‪$container = null
52  ) {
53  $this->container = ‪$container;
54  $this->‪seedMiddlewareStack($kernel);
55 
56  foreach ($middlewares as $middleware) {
57  if (is_string($middleware)) {
58  $this->‪lazy($middleware);
59  } else {
60  $this->‪add($middleware);
61  }
62  }
63  }
64 
68  public function ‪handle(ServerRequestInterface $request): ResponseInterface
69  {
70  return $this->tip->handle($request);
71  }
72 
76  protected function ‪seedMiddlewareStack(RequestHandlerInterface $kernel)
77  {
78  $this->tip = $kernel;
79  }
80 
90  public function ‪add(MiddlewareInterface $middleware)
91  {
92  $next = ‪$this->tip;
93  $this->tip = new class ($middleware, $next) implements RequestHandlerInterface {
97  private $middleware;
101  private $next;
102 
103  public function ‪__construct(MiddlewareInterface $middleware, RequestHandlerInterface $next)
104  {
105  $this->middleware = $middleware;
106  $this->next = $next;
107  }
108 
109  public function ‪handle(ServerRequestInterface $request): ResponseInterface
110  {
111  return $this->middleware->process($request, $this->next);
112  }
113  };
114  }
115 
125  public function ‪lazy(string $middleware): void
126  {
127  $next = ‪$this->tip;
128  $this->tip = new class ($middleware, $next, ‪$this->container) implements RequestHandlerInterface {
132  private $middleware;
133 
137  private $next;
138 
142  private ‪$container;
143 
144  public function ‪__construct(string $middleware, RequestHandlerInterface $next, ContainerInterface ‪$container = null)
145  {
146  $this->middleware = $middleware;
147  $this->next = $next;
148  $this->container = ‪$container;
149  }
150 
151  public function ‪handle(ServerRequestInterface $request): ResponseInterface
152  {
153  if ($this->container !== null && $this->container->has($this->middleware)) {
154  $middleware = $this->container->get($this->middleware);
155  } else {
156  $middleware = GeneralUtility::makeInstance($this->middleware);
157  }
158 
159  if (!$middleware instanceof MiddlewareInterface) {
160  throw new \InvalidArgumentException(get_class($middleware) . ' does not implement ' . MiddlewareInterface::class, 1516821342);
161  }
162  return $middleware->process($request, $this->next);
163  }
164  };
165  }
166 }
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\add
‪add(MiddlewareInterface $middleware)
Definition: MiddlewareDispatcher.php:88
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\lazy
‪lazy(string $middleware)
Definition: MiddlewareDispatcher.php:121
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\seedMiddlewareStack
‪seedMiddlewareStack(RequestHandlerInterface $kernel)
Definition: MiddlewareDispatcher.php:74
‪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:46
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher
Definition: MiddlewareDispatcher.php:35
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\$container
‪ContainerInterface null $container
Definition: MiddlewareDispatcher.php:44
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\handle
‪handle(ServerRequestInterface $request)
Definition: MiddlewareDispatcher.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18