1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WPEmerge\View; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use View as ViewService; |
7
|
|
|
use WPEmerge\Helpers\Path; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Render view files with php |
11
|
|
|
*/ |
12
|
|
|
class Php implements EngineInterface { |
13
|
|
|
/** |
14
|
|
|
* {@inheritDoc} |
15
|
|
|
*/ |
16
|
1 |
|
public function exists( $view ) { |
17
|
1 |
|
$file = $this->resolveFile( $view ); |
18
|
1 |
|
return strlen( $file ) > 0; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
6 |
|
public function render( $views, $context ) { |
25
|
6 |
|
$view_file = $this->resolveViewAndFile( $views ); |
26
|
|
|
|
27
|
6 |
|
if ( $view_file === null ) { |
28
|
1 |
|
return ''; |
29
|
|
|
} |
30
|
|
|
|
31
|
5 |
|
$__view = $view_file['file']; |
32
|
|
|
|
33
|
5 |
|
$__context = array_merge( |
34
|
5 |
|
['global' => ViewService::getGlobals()], |
35
|
5 |
|
ViewService::compose( $view_file['view'] ), |
36
|
|
|
$context |
37
|
5 |
|
); |
38
|
|
|
|
39
|
5 |
|
$renderer = function() use ( $__view, $__context ) { |
40
|
5 |
|
ob_start(); |
41
|
5 |
|
extract( $__context ); |
42
|
5 |
|
include( $__view ); |
43
|
5 |
|
return ob_get_clean(); |
44
|
5 |
|
}; |
45
|
|
|
|
46
|
5 |
|
return $renderer(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Resolve a view or a view array to an absolute filepath |
51
|
|
|
* |
52
|
|
|
* @param string $view |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
1 |
|
protected function resolveFile( $view ) { |
56
|
1 |
|
$file = locate_template( $view, false ); |
57
|
|
|
|
58
|
1 |
|
if ( ! $file ) { |
59
|
|
|
// locate_template failed to find the view - test if a valid absolute path was passed |
60
|
1 |
|
$file = $this->resolveFileFromFilesystem( $view ); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
return $file; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Resolve the view if it exists on the filesystem |
68
|
|
|
* |
69
|
|
|
* @param string $view |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
1 |
|
protected function resolveFileFromFilesystem( $view ) { |
73
|
1 |
|
return file_exists( $view ) ? $view : ''; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Resolve an array of views to the first existing view and it's filepath |
78
|
|
|
* |
79
|
|
|
* @param string[] $views |
80
|
|
|
* @return array|null |
|
|
|
|
81
|
|
|
*/ |
82
|
2 |
|
protected function resolveViewAndFile( $views ) { |
83
|
2 |
|
$view = ''; |
84
|
2 |
|
$file = ''; |
85
|
|
|
|
86
|
2 |
|
foreach ( $views as $current_view ) { |
87
|
2 |
|
if ( $this->exists( $current_view ) ) { |
88
|
1 |
|
$view = $current_view; |
89
|
1 |
|
$file = $this->resolveFile( $view ); |
90
|
1 |
|
break; |
91
|
|
|
} |
92
|
2 |
|
} |
93
|
|
|
|
94
|
2 |
|
if ( ! $file ) { |
95
|
1 |
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return [ |
99
|
1 |
|
'view' => $view, |
100
|
1 |
|
'file' => $file, |
101
|
1 |
|
]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.