|
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\View; |
|
13
|
|
|
|
|
14
|
|
|
use BrightNucleus\View\Engine\EngineInterface; |
|
15
|
|
|
use BrightNucleus\View\Exception\FailedToInstantiateViewException; |
|
16
|
|
|
use BrightNucleus\View\Support\AbstractFinder; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ViewFinder. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 0.1.0 |
|
22
|
|
|
* |
|
23
|
|
|
* @package BrightNucleus\View\View |
|
24
|
|
|
* @author Alain Schlesser <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ViewFinder extends AbstractFinder |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Find a result based on a specific criteria. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 0.1.0 |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $criteria Criteria to search for. |
|
35
|
|
|
* @param EngineInterface $engine Optional. Engine to use with the view. |
|
|
|
|
|
|
36
|
|
|
* |
|
37
|
|
|
* @return ViewInterface View that was found. |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
17 |
|
public function find(array $criteria, EngineInterface $engine = null) |
|
40
|
|
|
{ |
|
41
|
17 |
|
$uri = $criteria[0]; |
|
42
|
|
|
|
|
43
|
17 |
|
$this->initializeViews($uri, $engine); |
|
44
|
|
|
|
|
45
|
17 |
|
foreach ($criteria as $entry) { |
|
46
|
17 |
|
foreach ($this->findables as $viewObject) { |
|
47
|
17 |
|
if ($viewObject->canHandle($entry)) { |
|
48
|
17 |
|
return $viewObject; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->nullObject; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Initialize the views that can be iterated. |
|
58
|
|
|
* |
|
59
|
|
|
* @since 0.1.0 |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $uri URI to use for the view. |
|
62
|
|
|
* @param EngineInterface $engine Optional. Engine to use with the view. |
|
|
|
|
|
|
63
|
|
|
*/ |
|
64
|
17 |
View Code Duplication |
protected function initializeViews($uri, EngineInterface $engine = null) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
17 |
|
foreach ($this->findables as &$view) { |
|
67
|
17 |
|
$view = $this->initializeView($view, $uri, $engine); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
17 |
|
if (! is_object($this->nullObject)) { |
|
71
|
7 |
|
$this->nullObject = new $this->nullObject(); |
|
72
|
|
|
} |
|
73
|
17 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Initialize a single view by instantiating class name strings and calling closures. |
|
77
|
|
|
* |
|
78
|
|
|
* @since 0.1.0 |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed $view View to instantiate. |
|
81
|
|
|
* @param string $uri URI to use for the view. |
|
82
|
|
|
* @param EngineInterface $engine Optional. Engine to use with the view. |
|
|
|
|
|
|
83
|
|
|
* |
|
84
|
|
|
* @return ViewInterface Instantiated view. |
|
85
|
|
|
* @throws FailedToInstantiateViewException If the view could not be instantiated. |
|
86
|
|
|
*/ |
|
87
|
17 |
View Code Duplication |
protected function initializeView($view, $uri, EngineInterface $engine = null) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
17 |
|
if (is_string($view)) { |
|
90
|
7 |
|
$view = new $view($uri, $engine); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
17 |
|
if (is_callable($view)) { |
|
94
|
|
|
$view = $view($uri, $engine); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
17 |
|
if (! $view instanceof ViewInterface) { |
|
98
|
|
|
throw new FailedToInstantiateViewException( |
|
99
|
|
|
sprintf( |
|
100
|
|
|
_('Could not instantiate view "%s".'), |
|
101
|
|
|
serialize($view) |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
17 |
|
return $view; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get the config key for the Findables definitions. |
|
111
|
|
|
* |
|
112
|
|
|
* @since 0.1.0 |
|
113
|
|
|
* |
|
114
|
|
|
* @return string Config key use to define the Findables. |
|
115
|
|
|
*/ |
|
116
|
7 |
|
protected function getFindablesConfigKey() |
|
117
|
|
|
{ |
|
118
|
7 |
|
return 'Views'; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.