1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bright Nucleus View Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\View |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\View\Engine; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\View\Exception\FailedToInstantiateEngineException; |
15
|
|
|
use BrightNucleus\View\Support\AbstractFinder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class EngineFinder. |
19
|
|
|
* |
20
|
|
|
* @since 0.1.0 |
21
|
|
|
* |
22
|
|
|
* @package BrightNucleus\View\Engine |
23
|
|
|
* @author Alain Schlesser <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class EngineFinder extends AbstractFinder |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Find a result based on a specific criteria. |
30
|
|
|
* |
31
|
|
|
* @since 0.1.0 |
32
|
|
|
* |
33
|
|
|
* @param array $criteria Criteria to search for. |
34
|
|
|
* |
35
|
|
|
* @return EngineInterface Result of the search. |
|
|
|
|
36
|
|
|
*/ |
37
|
17 |
|
public function find(array $criteria) |
38
|
|
|
{ |
39
|
17 |
|
$this->initializeEngines(); |
40
|
|
|
|
41
|
17 |
|
foreach ($criteria as $entry) { |
42
|
17 |
|
foreach ($this->findables as $engine) { |
43
|
17 |
|
if ($engine->canHandle($entry)) { |
44
|
17 |
|
return $engine; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->getNullObject(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize the engines that can be iterated. |
54
|
|
|
* |
55
|
|
|
* @since 0.1.0 |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
17 |
|
protected function initializeEngines() |
59
|
|
|
{ |
60
|
17 |
|
foreach ($this->findables as &$engine) { |
61
|
17 |
|
$engine = $this->initializeEngine($engine); |
62
|
|
|
} |
63
|
17 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Initialize a single engine by instantiating class name strings and calling closures. |
67
|
|
|
* |
68
|
|
|
* @since 0.1.0 |
69
|
|
|
* |
70
|
|
|
* @param mixed $engine Engine to instantiate. |
71
|
|
|
* |
72
|
|
|
* @return EngineInterface Instantiated engine. |
73
|
|
|
* @throws FailedToInstantiateEngineException If the engine could not be instantiated. |
74
|
|
|
*/ |
75
|
17 |
|
protected function initializeEngine($engine) |
76
|
|
|
{ |
77
|
17 |
|
if (is_string($engine)) { |
78
|
7 |
|
$engine = new $engine(); |
79
|
|
|
} |
80
|
|
|
|
81
|
17 |
|
if (is_callable($engine)) { |
82
|
|
|
$engine = $engine(); |
83
|
|
|
} |
84
|
|
|
|
85
|
17 |
|
if (! $engine instanceof EngineInterface) { |
86
|
|
|
throw new FailedToInstantiateEngineException( |
87
|
|
|
sprintf( |
88
|
|
|
_('Could not instantiate engine "%s".'), |
89
|
|
|
serialize($engine) |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
17 |
|
return $engine; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the config key for the Findables definitions. |
99
|
|
|
* |
100
|
|
|
* @since 0.1.0 |
101
|
|
|
* |
102
|
|
|
* @return string Config key use to define the Findables. |
103
|
|
|
*/ |
104
|
7 |
|
protected function getFindablesConfigKey() |
105
|
|
|
{ |
106
|
7 |
|
return 'Engines'; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.