1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Core\Routing\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Contracts\Core\SapiInterface; |
22
|
|
|
use Limoncello\Core\Application\Application; |
23
|
|
|
use LogicException; |
24
|
|
|
use Psr\Container\ContainerInterface; |
25
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @package Limoncello\Core |
29
|
|
|
* |
30
|
|
|
* @method string getCallableToCacheMessage(); |
31
|
|
|
*/ |
32
|
|
|
trait HasRequestFactoryTrait |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var callable|false|null |
36
|
|
|
*/ |
37
|
|
|
private $requestFactory = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param callable|null $requestFactory |
41
|
|
|
* |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
22 |
|
public function setRequestFactory(callable $requestFactory = null): self |
45
|
|
|
{ |
46
|
22 |
|
$parameters = [SapiInterface::class, ContainerInterface::class]; |
47
|
22 |
|
if ($requestFactory !== null && |
48
|
22 |
|
$this->checkPublicStaticCallable($requestFactory, $parameters, ServerRequestInterface::class) === false |
|
|
|
|
49
|
|
|
) { |
50
|
2 |
|
throw new LogicException($this->getCallableToCacheMessage()); |
51
|
|
|
} |
52
|
20 |
|
$this->requestFactory = $requestFactory; |
53
|
|
|
|
54
|
20 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
17 |
|
protected function isRequestFactorySet() |
61
|
|
|
{ |
62
|
17 |
|
return $this->requestFactory !== false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return callable |
|
|
|
|
67
|
|
|
* |
68
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
69
|
|
|
*/ |
70
|
8 |
|
protected function getDefaultRequestFactory(): callable |
71
|
|
|
{ |
72
|
8 |
|
return Application::getDefaultRequestFactory(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.