Request::setArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Extbase\Mvc;
19
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Http\Message\StreamInterface;
22
use Psr\Http\Message\UriInterface;
23
use TYPO3\CMS\Core\Http\ApplicationType;
24
use TYPO3\CMS\Core\Http\NormalizedParams;
25
use TYPO3\CMS\Core\Http\ServerRequest;
26
use TYPO3\CMS\Extbase\Error\Result;
27
28
/**
29
 * The extbase request.
30
 *
31
 * This is a decorator: The core PSR-7 request is hand over as constructor
32
 * argument, this class implements ServerRequestInterface, too.
33
 * Additionally, the extbase request details are attached as 'extbase'
34
 * attribute to the PSR-7 request and this class implements extbase RequestInterface.
35
 * This class has no state except the PSR-7 request, all operations are
36
 * hand down to the PSR-7 request.
37
 */
38
class Request implements ServerRequestInterface, RequestInterface
39
{
40
    protected ServerRequestInterface $request;
41
42
    /**
43
     * @todo v12: final public function __construct(ServerRequestInterface $request)
44
     */
45
    public function __construct($request = null)
46
    {
47
        if (is_string($request) && !empty($request)) {
48
            // Deprecation layer for old extbase Request __construct(string $controllerClassName = '')
49
            $controllerClassName = $request;
50
            /** @var ServerRequestInterface $request */
51
            $request = $GLOBALS['TYPO3_REQUEST'] ?? new ServerRequest();
52
            $attribute = new ExtbaseRequestParameters($controllerClassName);
53
            $request = $request->withAttribute('extbase', $attribute);
54
        } elseif ($request === null) {
55
            // Deprecation layer when ServerRequestInterface is not given yet
56
            /** @var ServerRequestInterface $request */
57
            // Fallback "new ServerRequest()" currently used in install tool.
58
            $request = $GLOBALS['TYPO3_REQUEST'] ?? new ServerRequest();
59
            $attribute = new ExtbaseRequestParameters('');
60
            $request = $request->withAttribute('extbase', $attribute);
61
        }
62
        if (!$request instanceof ServerRequestInterface) {
63
            throw new \InvalidArgumentException(
64
                'Request must implement PSR-7 ServerRequestInterface',
65
                1624452071
66
            );
67
        }
68
        if (!$request->getAttribute('extbase') instanceof ExtbaseRequestParameters) {
69
            throw new \InvalidArgumentException(
70
                'Given request must have an attribute "extbase" of type ExtbaseAttribute',
71
                1624452070
72
            );
73
        }
74
        $this->request = $request;
75
    }
76
77
    /**
78
     * ExtbaseAttribute attached as attribute 'extbase' to $request carries extbase
79
     * specific request values. This helper method type hints this attribute.
80
     */
81
    protected function getExtbaseAttribute(): ExtbaseRequestParameters
82
    {
83
        return $this->request->getAttribute('extbase');
84
    }
85
86
    public function getServerRequest(): ServerRequestInterface
87
    {
88
        return $this->request;
89
    }
90
91
    /**
92
     * Methods implementing extbase RequestInterface
93
     */
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function getControllerObjectName(): string
99
    {
100
        return $this->getExtbaseAttribute()->getControllerObjectName();
101
    }
102
103
    /**
104
     * Return an instance with the specified controller object name set.
105
     */
106
    public function withControllerObjectName(string $controllerObjectName): self
107
    {
108
        $attribute = $this->getExtbaseAttribute()->setControllerObjectName($controllerObjectName);
109
        $request = $this->request->withAttribute('extbase', $attribute);
110
        return new static($request);
111
    }
112
113
    /**
114
     * Returns the plugin key.
115
     *
116
     * @todo: Should be "public function getPluginName(): string", blocked by testing-framework
117
     */
118
    public function getPluginName()
119
    {
120
        return $this->getExtbaseAttribute()->getPluginName();
121
    }
122
123
    /**
124
     * Return an instance with the specified plugin name set.
125
     */
126
    public function withPluginName($pluginName = null): self
127
    {
128
        $attribute = $this->getExtbaseAttribute()->setPluginName($pluginName);
129
        $request = $this->request->withAttribute('extbase', $attribute);
130
        return new static($request);
131
    }
132
133
    /**
134
     * Returns the extension name of the specified controller.
135
     *
136
     * @todo: Should be "public function getControllerExtensionName(): string", blocked by testing-framework
137
     */
138
    public function getControllerExtensionName()
139
    {
140
        return $this->getExtbaseAttribute()->getControllerExtensionName();
141
    }
142
143
    /**
144
     * Return an instance with the specified controller extension name set.
145
     *
146
     * @param string|null $controllerExtensionName Extension name
147
     * @return self
148
     */
149
    public function withControllerExtensionName($controllerExtensionName): self
150
    {
151
        $attribute = $this->getExtbaseAttribute()->setControllerExtensionName($controllerExtensionName);
152
        $request = $this->request->withAttribute('extbase', $attribute);
153
        return new static($request);
154
    }
155
156
    /**
157
     * Returns the extension key of the specified controller.
158
     *
159
     * @return string The extension key
160
     */
161
    public function getControllerExtensionKey(): string
162
    {
163
        return $this->getExtbaseAttribute()->getControllerExtensionKey();
164
    }
165
166
    /**
167
     * Returns the controller name supposed to handle this request, if one
168
     * was set already (if not, the name of the default controller is returned)
169
     *
170
     * @todo: Should be "public function getControllerName(): string", blocked by testing-framework
171
     */
172
    public function getControllerName()
173
    {
174
        return (string)$this->getExtbaseAttribute()->getControllerName();
175
    }
176
177
    /**
178
     * Return an instance with the specified controller name set.
179
     * Note: This is not the object name of the controller!
180
     *
181
     * @param string|null $controllerName Controller name
182
     * @return self
183
     */
184
    public function withControllerName($controllerName): self
185
    {
186
        $attribute = $this->getExtbaseAttribute()->setControllerName($controllerName);
187
        $request = $this->request->withAttribute('extbase', $attribute);
188
        return new static($request);
189
    }
190
191
    /**
192
     * Returns the name of the action the controller is supposed to execute.
193
     *
194
     * @todo: Should be "public function getControllerActionName(): string", blocked by testing-framework
195
     */
196
    public function getControllerActionName()
197
    {
198
        return $this->getExtbaseAttribute()->getControllerActionName();
199
    }
200
201
    /**
202
     * Return an instance with the specified controller action name set.
203
     *
204
     * Note that the action name must start with a lower case letter and is case sensitive.
205
     *
206
     * @param string|null $actionName Action name
207
     * @return self
208
     */
209
    public function withControllerActionName($actionName): self
210
    {
211
        $attribute = $this->getExtbaseAttribute()->setControllerActionName($actionName);
212
        $request = $this->request->withAttribute('extbase', $attribute);
213
        return new static($request);
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219
    public function getArguments(): array
220
    {
221
        return $this->getExtbaseAttribute()->getArguments();
222
    }
223
224
    /**
225
     * Return an instance with the specified extbase arguments, replacing
226
     * any arguments which existed before.
227
     */
228
    public function withArguments(array $arguments): self
229
    {
230
        $attribute = $this->getExtbaseAttribute()->setArguments($arguments);
231
        $request = $this->request->withAttribute('extbase', $attribute);
232
        return new static($request);
233
    }
234
235
    /**
236
     * @inheritdoc
237
     */
238
    public function getArgument($argumentName)
239
    {
240
        return $this->getExtbaseAttribute()->getArgument($argumentName);
241
    }
242
243
    /**
244
     * @inheritDoc
245
     */
246
    public function hasArgument($argumentName): bool
247
    {
248
        return $this->getExtbaseAttribute()->hasArgument($argumentName);
249
    }
250
251
    /**
252
     * Return an instance with the specified argument set.
253
     *
254
     * @param string $argumentName Name of the argument to set
255
     * @param mixed $value The new value
256
     * @return self
257
     */
258
    public function withArgument(string $argumentName, $value): self
259
    {
260
        $attribute = $this->getExtbaseAttribute()->setArgument($argumentName, $value);
261
        $request = $this->request->withAttribute('extbase', $attribute);
262
        return new static($request);
263
    }
264
265
    /**
266
     * Returns the requested representation format, something
267
     * like "html", "xml", "png", "json" or the like.
268
     */
269
    public function getFormat(): string
270
    {
271
        return $this->getExtbaseAttribute()->getFormat();
272
    }
273
274
    /**
275
     * Return an instance with the specified derived request attribute.
276
     *
277
     * This method allows setting a single derived request attribute as
278
     * described in getFormat().
279
     */
280
    public function withFormat(string $format): self
281
    {
282
        $attribute = $this->getExtbaseAttribute()->setFormat($format);
283
        $request = $this->request->withAttribute('extbase', $attribute);
284
        return new static($request);
285
    }
286
287
    /**
288
     * Extbase @internal methods, not part of extbase RequestInterface. Should vanish as soon as unused.
289
     */
290
291
    /**
292
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
293
     */
294
    public function setControllerObjectName($controllerObjectName)
295
    {
296
        $this->getExtbaseAttribute()->setControllerObjectName($controllerObjectName);
297
    }
298
299
    /**
300
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
301
     */
302
    public function setPluginName($pluginName = null)
303
    {
304
        $this->getExtbaseAttribute()->setPluginName($pluginName);
305
    }
306
307
    /**
308
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
309
     */
310
    public function setControllerExtensionName($controllerExtensionName)
311
    {
312
        $this->getExtbaseAttribute()->setControllerExtensionName($controllerExtensionName);
313
    }
314
315
    /**
316
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
317
     */
318
    public function setControllerAliasToClassNameMapping(array $controllerAliasToClassNameMapping)
319
    {
320
        // this is only needed as long as forwarded requests are altered and unless there
321
        // is no new request object created by the request builder.
322
        $this->getExtbaseAttribute()->setControllerAliasToClassNameMapping($controllerAliasToClassNameMapping);
323
    }
324
325
    /**
326
     * @internal only to be used within Extbase, not part of TYPO3 Core API.
327
     */
328
    public function withControllerAliasToClassNameMapping(array $controllerAliasToClassNameMapping): self
329
    {
330
        // this is only needed as long as forwarded requests are altered and unless there
331
        // is no new request object created by the request builder.
332
        $attribute = $this->getExtbaseAttribute()->setControllerAliasToClassNameMapping($controllerAliasToClassNameMapping);
333
        $request = $this->request->withAttribute('extbase', $attribute);
334
        return new static($request);
335
    }
336
337
    /**
338
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
339
     */
340
    public function setControllerName($controllerName)
341
    {
342
        $this->getExtbaseAttribute()->setControllerName($controllerName);
343
    }
344
345
    /**
346
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
347
     */
348
    public function setControllerActionName($actionName)
349
    {
350
        $this->getExtbaseAttribute()->setControllerActionName($actionName);
351
    }
352
353
    /**
354
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
355
     */
356
    public function setArgument($argumentName, $value)
357
    {
358
        $this->getExtbaseAttribute()->setArgument($argumentName, $value);
359
    }
360
361
    /**
362
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
363
     */
364
    public function setArguments(array $arguments)
365
    {
366
        $this->getExtbaseAttribute()->setArguments($arguments);
367
    }
368
369
    /**
370
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
371
     */
372
    public function setFormat($format)
373
    {
374
        $this->getExtbaseAttribute()->setFormat($format);
375
    }
376
377
    /**
378
     * @internal only to be used within Extbase, not part of TYPO3 Core API.
379
     */
380
    public function getOriginalRequest(): ?Request
381
    {
382
        return $this->getExtbaseAttribute()->getOriginalRequest();
383
    }
384
385
    /**
386
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
387
     */
388
    public function setOriginalRequest(Request $originalRequest)
389
    {
390
        $this->getExtbaseAttribute()->setOriginalRequest($originalRequest);
391
    }
392
393
    /**
394
     * Get the request mapping results for the original request.
395
     *
396
     * @internal only to be used within Extbase, not part of TYPO3 Core API.
397
     */
398
    public function getOriginalRequestMappingResults(): Result
399
    {
400
        return $this->getExtbaseAttribute()->getOriginalRequestMappingResults();
401
    }
402
403
    /**
404
     * @internal only to be used within Extbase, not part of TYPO3 Core API. Violates immutability.
405
     */
406
    public function setOriginalRequestMappingResults(Result $originalRequestMappingResults)
407
    {
408
        $this->getExtbaseAttribute()->setOriginalRequestMappingResults($originalRequestMappingResults);
409
    }
410
411
    /**
412
     * @internal only to be used within Extbase, not part of TYPO3 Core API.
413
     */
414
    public function getInternalArguments(): array
415
    {
416
        return $this->getExtbaseAttribute()->getInternalArguments();
417
    }
418
419
    /**
420
     * @internal only to be used within Extbase, not part of TYPO3 Core API.
421
     */
422
    public function getInternalArgument($argumentName)
423
    {
424
        return $this->getExtbaseAttribute()->getInternalArgument($argumentName);
425
    }
426
427
    /**
428
     * Deprecated methods of extbase Request for v11 compat.
429
     */
430
431
    /**
432
     * @deprecated since v11, will be removed in v12. Violates immutability.
433
     */
434
    public function setDispatched($flag)
435
    {
436
        $this->getExtbaseAttribute()->setDispatched($flag);
437
    }
438
439
    /**
440
     * @deprecated since v11, will be removed in v12.
441
     */
442
    public function isDispatched()
443
    {
444
        return $this->getExtbaseAttribute()->isDispatched();
445
    }
446
447
    /**
448
     * @deprecated since v11, will be removed in v12.
449
     */
450
    public function getRequestUri()
451
    {
452
        trigger_error('Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 12.0', E_USER_DEPRECATED);
453
        /** @var NormalizedParams $normalizedParams */
454
        $normalizedParams = $this->getAttribute('normalizedParams');
455
        return $normalizedParams->getRequestUrl();
456
    }
457
458
    /**
459
     * @deprecated since v11, will be removed in v12.
460
     */
461
    public function getBaseUri()
462
    {
463
        trigger_error('Method ' . __METHOD__ . ' is deprecated and will be removed in TYPO3 12.0', E_USER_DEPRECATED);
464
        /** @var NormalizedParams $normalizedParams */
465
        $normalizedParams = $this->getAttribute('normalizedParams');
466
        $baseUri = $normalizedParams->getSiteUrl();
467
        if (ApplicationType::fromRequest($this)->isBackend()) {
468
            $baseUri .= TYPO3_mainDir;
469
        }
470
        return $baseUri;
471
    }
472
473
    /**
474
     * Methods implementing ServerRequestInterface
475
     */
476
477
    /**
478
     * @inheritdoc
479
     */
480
    public function getServerParams(): array
481
    {
482
        return $this->request->getServerParams();
483
    }
484
485
    /**
486
     * @inheritdoc
487
     */
488
    public function getCookieParams(): array
489
    {
490
        return $this->request->getCookieParams();
491
    }
492
493
    /**
494
     * @inheritdoc
495
     */
496
    public function withCookieParams(array $cookies): self
497
    {
498
        $request = $this->request->withCookieParams($cookies);
499
        return new static($request);
500
    }
501
502
    /**
503
     * @inheritdoc
504
     */
505
    public function getQueryParams(): array
506
    {
507
        return $this->request->getQueryParams();
508
    }
509
510
    /**
511
     * @inheritdoc
512
     */
513
    public function withQueryParams(array $query): self
514
    {
515
        $request = $this->request->withQueryParams($query);
516
        return new static($request);
517
    }
518
519
    /**
520
     * @inheritdoc
521
     */
522
    public function getUploadedFiles(): array
523
    {
524
        return $this->request->getUploadedFiles();
525
    }
526
527
    /**
528
     * @inheritdoc
529
     */
530
    public function withUploadedFiles(array $uploadedFiles): self
531
    {
532
        $request = $this->request->withUploadedFiles($uploadedFiles);
533
        return new static($request);
534
    }
535
536
    /**
537
     * @inheritdoc
538
     */
539
    public function getParsedBody()
540
    {
541
        return $this->request->getParsedBody();
542
    }
543
544
    /**
545
     * @inheritdoc
546
     */
547
    public function withParsedBody($data): self
548
    {
549
        $request = $this->request->withParsedBody($data);
550
        return new static($request);
551
    }
552
553
    /**
554
     * @inheritdoc
555
     */
556
    public function getAttributes(): array
557
    {
558
        return $this->request->getAttributes();
559
    }
560
561
    /**
562
     * @inheritdoc
563
     */
564
    public function getAttribute($name, $default = null)
565
    {
566
        return $this->request->getAttribute($name, $default);
567
    }
568
569
    /**
570
     * @inheritdoc
571
     */
572
    public function withAttribute($name, $value): self
573
    {
574
        $request = $this->request->withAttribute($name, $value);
575
        return new static($request);
576
    }
577
578
    /**
579
     * @inheritdoc
580
     */
581
    public function withoutAttribute($name): self
582
    {
583
        $request = $this->request->withoutAttribute($name);
584
        return new static($request);
585
    }
586
587
    /**
588
     * Methods implementing RequestInterface
589
     */
590
591
    /**
592
     * @inheritdoc
593
     */
594
    public function getRequestTarget(): string
595
    {
596
        return $this->request->getRequestTarget();
597
    }
598
599
    /**
600
     * @inheritdoc
601
     */
602
    public function withRequestTarget($requestTarget): self
603
    {
604
        $request = $this->request->withRequestTarget($requestTarget);
605
        return new static($request);
606
    }
607
608
    /**
609
     * @inheritdoc
610
     */
611
    public function getMethod(): string
612
    {
613
        return $this->request->getMethod();
614
    }
615
616
    /**
617
     * @inheritdoc
618
     */
619
    public function withMethod($method): self
620
    {
621
        $request = $this->request->withMethod($method);
622
        return new static($request);
623
    }
624
625
    /**
626
     * @inheritdoc
627
     */
628
    public function getUri(): UriInterface
629
    {
630
        return $this->request->getUri();
631
    }
632
633
    /**
634
     * @inheritdoc
635
     */
636
    public function withUri(UriInterface $uri, $preserveHost = false): self
637
    {
638
        $request = $this->request->withUri($uri, $preserveHost);
639
        return new static($request);
640
    }
641
642
    /**
643
     * Methods implementing MessageInterface
644
     */
645
646
    /**
647
     * @inheritdoc
648
     */
649
    public function getProtocolVersion(): string
650
    {
651
        return $this->request->getProtocolVersion();
652
    }
653
654
    /**
655
     * @inheritdoc
656
     */
657
    public function withProtocolVersion($version): self
658
    {
659
        $request = $this->request->withProtocolVersion($version);
660
        return new static($request);
661
    }
662
663
    /**
664
     * @inheritdoc
665
     */
666
    public function getHeaders(): array
667
    {
668
        return $this->request->getHeaders();
669
    }
670
671
    /**
672
     * @inheritdoc
673
     */
674
    public function hasHeader($name): bool
675
    {
676
        return $this->request->hasHeader($name);
677
    }
678
679
    /**
680
     * @inheritdoc
681
     */
682
    public function getHeader($name): array
683
    {
684
        return $this->request->getHeader($name);
685
    }
686
687
    /**
688
     * @inheritdoc
689
     */
690
    public function getHeaderLine($name): string
691
    {
692
        return $this->request->getHeaderLine($name);
693
    }
694
695
    /**
696
     * @inheritdoc
697
     */
698
    public function withHeader($name, $value): self
699
    {
700
        $request = $this->request->withHeader($name, $value);
701
        return new static($request);
702
    }
703
704
    /**
705
     * @inheritdoc
706
     */
707
    public function withAddedHeader($name, $value): self
708
    {
709
        $request = $this->request->withAddedHeader($name, $value);
710
        return new static($request);
711
    }
712
713
    /**
714
     * @inheritdoc
715
     */
716
    public function withoutHeader($name): self
717
    {
718
        $request = $this->request->withoutHeader($name);
719
        return new static($request);
720
    }
721
722
    /**
723
     * @inheritdoc
724
     */
725
    public function getBody(): StreamInterface
726
    {
727
        return $this->request->getBody();
728
    }
729
730
    /**
731
     * @inheritdoc
732
     */
733
    public function withBody(StreamInterface $body): self
734
    {
735
        $request = $this->request->withBody($body);
736
        return new static($request);
737
    }
738
}
739