ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
HandlerService.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
32
37{
39 private array $handlers = [];
40
41 public function __construct(
42 private RequestBuilder $request_builder,
43 private Context $context,
44 private array $handler_instances
45 ) {
46 $this->response_factory = new Factory($context);
47 // check handlers
48 foreach ($handler_instances as $handler_instance) {
49 if (!$handler_instance instanceof Handler) {
50 throw new \InvalidArgumentException(
51 'Handler instances must implement the Handler interface'
52 );
53 }
54 }
55 }
56
57 public function initHandler(): void
58 {
59 foreach ($this->handler_instances as $handler) {
60 if (isset($this->handlers[$handler->getNamespace()])) {
61 throw new \LogicException("Namespace-Collision detected: " . $handler->getNamespace());
62 }
63 $this->handlers[$handler->getNamespace()] = $handler;
64 if ($handler instanceof AliasedHandler) {
65 foreach ($handler->getNamespaceAliasses() as $namespace_aliass) {
66 if (isset($this->handlers[$namespace_aliass])) {
67 throw new \LogicException("Namespace-Collision detected: " . $namespace_aliass);
68 }
69 $this->handlers[$namespace_aliass] = $handler;
70 }
71 }
72 }
73 }
74
78 public function performRedirect(URI $base_uri): void
79 {
80 if (empty($this->handlers)) {
81 $this->initHandler();
82 }
83
84 $http = $this->context->http();
85
86 $request = $this->request_builder->buildRequest(
87 $http,
88 $this->context->refinery(),
89 $this->handlers
90 );
91 if (!$request instanceof Request) {
92 throw new \RuntimeException('No request could be built');
93 }
94
95 $handler = $this->handlers[$request->getNamespace()] ?? null;
96 if (!$handler instanceof Handler) {
97 throw new \InvalidArgumentException('No handler found for namespace ' . $request->getNamespace());
98 }
99 $response = $handler->handle($request, $this->context, $this->response_factory);
100 if (!$response->targetCanBeReached()) {
101 $http->saveResponse(
102 $http->response()->withStatus(404),
103 );
104 $http->sendResponse();
105 $http->close();
106 }
107
108 $uri_builder = new StandardURIBuilder(new StaticURLConfig());
109
110 switch (true) {
111 case $response instanceof MaybeCanHandlerAfterLogin:
112 $target = $uri_builder->buildTarget(
113 $request->getNamespace(),
114 $request->getReferenceId(),
115 $request->getAdditionalParameters()
116 );
117 $full_uri = $base_uri . "/login.php?target=";
118 $full_uri .= str_replace('/', '_', rtrim($target, '/')); // TODO: ILIAS currently need this like this
119 if (!$this->context->isUserLoggedIn()) {
120 $full_uri .= '&cmd=force_login&lang=' . $this->context->getUserLanguage();
121 }
122 $full_uri = $this->appendUnknownParameters($this->context, $full_uri); // Read the comment below
123 break;
124 case $response instanceof CannotReach:
125 $this->context->mainTemplate()->setOnScreenMessage(
126 'failure',
127 $this->context->lng()->txt('permission_denied'),
128 true
129 );
130 $full_uri = $base_uri . '/index.php';
131 break;
132 default:
133 // Perform Redirect
134 $uri_path = $response->getURIPath() ?? '';
135 $base_path = $base_uri->getPath() ?? '';
136 if ($base_path !== '' && $base_path !== '/') {
137 $uri_path = str_replace(rtrim($base_path, '/') . '/', '', $uri_path);
138 }
139
140 for ($x = 0; $x < $response->shift(); $x++) {
141 $base_uri = $base_uri->withPath(dirname((string) $base_uri->getPath()));
142 }
143
144 $full_uri = $base_uri . '/' . trim((string) $uri_path, '/');
145 break;
146 }
147
148 $http->saveResponse(
149 $http->response()->withAddedHeader('Location', $full_uri)
150 );
151 $http->sendResponse();
152 $http->close();
153 }
154
159 private function appendUnknownParameters(Context $context, string $full_uri): string
160 {
161 if ($context->http()->wrapper()->query()->has('soap_pw')) {
162 return \ilUtil::appendUrlParameterString(
163 $full_uri,
164 'soap_pw=' . $context->http()->wrapper()->query()->retrieve(
165 'soap_pw',
166 $context->refinery()->kindlyTo()->string()
167 )
168 );
169 }
170 if ($context->http()->wrapper()->query()->has('ext_uid')) {
171 return \ilUtil::appendUrlParameterString(
172 $full_uri,
173 'ext_uid=' . $context->http()->wrapper()->query()->retrieve(
174 'ext_uid',
175 $context->refinery()->kindlyTo()->string()
176 )
177 );
178 }
179
180 return $full_uri;
181 }
182}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
withPath(?string $path=null)
Get URI with modified path.
Definition: URI.php:283
appendUnknownParameters(Context $context, string $full_uri)
__construct(private RequestBuilder $request_builder, private Context $context, private array $handler_instances)
$http
Definition: deliver.php:30
$handler
Definition: oai.php:29
$context
Definition: webdav.php:31
$response
Definition: xapitoken.php:90