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 LogicException; |
22
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
23
|
|
|
use function array_merge; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Limoncello\Core |
27
|
|
|
* |
28
|
|
|
* @method string getCallableToCacheMessage(); |
29
|
|
|
*/ |
30
|
|
|
trait HasConfiguratorsTrait |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var callable[] |
34
|
|
|
*/ |
35
|
|
|
private $configurators = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param callable[] $configurators |
39
|
|
|
* |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
21 |
|
public function setConfigurators(array $configurators): self |
43
|
|
|
{ |
44
|
21 |
|
foreach ($configurators as $configurator) { |
45
|
18 |
|
$isValid = $this->checkPublicStaticCallable($configurator, [LimoncelloContainerInterface::class]); |
|
|
|
|
46
|
18 |
|
if ($isValid === false) { |
47
|
18 |
|
throw new LogicException($this->getCallableToCacheMessage()); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
19 |
|
$this->configurators = $configurators; |
52
|
|
|
|
53
|
19 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param callable[] $configurators |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
1 |
|
public function addConfigurators(array $configurators): self |
62
|
|
|
{ |
63
|
1 |
|
return $this->setConfigurators(array_merge($this->configurators, $configurators)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
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.