1
|
|
|
<?php namespace Limoncello\Core\Routing\Traits; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use LogicException; |
20
|
|
|
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @package Limoncello\Core |
24
|
|
|
* |
25
|
|
|
* @method string getCallableToCacheMessage(); |
26
|
|
|
*/ |
27
|
|
|
trait HasConfiguratorsTrait |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var callable[] |
31
|
|
|
*/ |
32
|
|
|
private $configurators = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param callable[] $configurators |
36
|
|
|
* |
37
|
|
|
* @return self |
38
|
|
|
*/ |
39
|
21 |
|
public function setConfigurators(array $configurators): self |
40
|
|
|
{ |
41
|
21 |
|
foreach ($configurators as $configurator) { |
42
|
18 |
|
$isValid = $this->checkPublicStaticCallable($configurator, [LimoncelloContainerInterface::class]); |
|
|
|
|
43
|
18 |
|
if ($isValid === false) { |
44
|
18 |
|
throw new LogicException($this->getCallableToCacheMessage()); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
19 |
|
$this->configurators = $configurators; |
49
|
|
|
|
50
|
19 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param callable[] $configurators |
55
|
|
|
* |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
|
|
public function addConfigurators(array $configurators): self |
59
|
|
|
{ |
60
|
|
|
return $this->setConfigurators(array_merge($this->configurators, $configurators)); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
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.