‪TYPO3CMS  9.5
MiddlewareDispatcher.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 namespace ‪TYPO3\CMS\Core\Http;
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 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use Psr\Http\Server\MiddlewareInterface;
21 use Psr\Http\Server\RequestHandlerInterface;
23 
32 {
38  protected ‪$tip;
39 
44  public function ‪__construct(
46  array $middlewares = []
47  ) {
48  $this->‪seedMiddlewareStack($kernel);
49 
50  foreach ($middlewares as $middleware) {
51  if (is_string($middleware)) {
52  $this->‪lazy($middleware);
53  } else {
54  $this->‪add($middleware);
55  }
56  }
57  }
58 
65  public function ‪handle(ServerRequestInterface $request): ResponseInterface
66  {
67  return $this->tip->handle($request);
68  }
69 
75  protected function ‪seedMiddlewareStack(‪RequestHandlerInterface $kernel)
76  {
77  $this->tip = $kernel;
78  }
79 
89  public function ‪add(MiddlewareInterface $middleware)
90  {
91  $next = ‪$this->tip;
92  $this->tip = new class($middleware, $next) implements ‪RequestHandlerInterface {
93  private $middleware;
94  private $next;
95 
96  public function ‪__construct(MiddlewareInterface $middleware, ‪RequestHandlerInterface $next)
97  {
98  $this->middleware = $middleware;
99  $this->next = $next;
100  }
101 
102  public function ‪handle(ServerRequestInterface $request): ResponseInterface
103  {
104  return $this->middleware->process($request, $this->next);
105  }
106  };
107  }
108 
118  public function ‪lazy(string $middleware)
119  {
120  $next = ‪$this->tip;
121  $this->tip = new class($middleware, $next) implements ‪RequestHandlerInterface {
122  private $middleware;
123  private $next;
124 
125  public function ‪__construct(string $middleware, ‪RequestHandlerInterface $next)
126  {
127  $this->middleware = $middleware;
128  $this->next = $next;
129  }
130 
131  public function ‪handle(ServerRequestInterface $request): ResponseInterface
132  {
133  $middleware = GeneralUtility::makeInstance($this->middleware);
134 
135  if (!$middleware instanceof MiddlewareInterface) {
136  throw new \InvalidArgumentException(get_class($middleware) . ' does not implement ' . MiddlewareInterface::class, 1516821342);
137  }
138  return $middleware->process($request, $this->next);
139  }
140  };
141  }
142 }
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\add
‪add(MiddlewareInterface $middleware)
Definition: MiddlewareDispatcher.php:88
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\handle
‪ResponseInterface handle(ServerRequestInterface $request)
Definition: MiddlewareDispatcher.php:64
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\lazy
‪lazy(string $middleware)
Definition: MiddlewareDispatcher.php:117
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\seedMiddlewareStack
‪seedMiddlewareStack(RequestHandlerInterface $kernel)
Definition: MiddlewareDispatcher.php:74
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\$tip
‪RequestHandlerInterface $tip
Definition: MiddlewareDispatcher.php:37
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher\__construct
‪__construct(RequestHandlerInterface $kernel, array $middlewares=[])
Definition: MiddlewareDispatcher.php:43
‪TYPO3\CMS\Core\Http\MiddlewareDispatcher
Definition: MiddlewareDispatcher.php:32
‪TYPO3\CMS\Core\Http\RequestHandlerInterface
Definition: RequestHandlerInterface.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:3