|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WPEmerge\View; |
|
4
|
|
|
|
|
5
|
|
|
use View as ViewService; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Render view files with php |
|
9
|
|
|
*/ |
|
10
|
|
|
class Php implements EngineInterface { |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritDoc} |
|
13
|
|
|
*/ |
|
14
|
3 |
|
public function exists( $view ) { |
|
15
|
3 |
|
$file = $this->resolveFile( $view ); |
|
16
|
3 |
|
return strlen( $file ) > 0; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritDoc} |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public function render( $views, $context ) { |
|
23
|
6 |
|
foreach ( $views as $view ) { |
|
24
|
6 |
|
if ( $this->exists( $view ) ) { |
|
25
|
5 |
|
$file = $this->resolveFile( $view ); |
|
26
|
5 |
|
return $this->renderView( $view, $file, $context ); |
|
27
|
|
|
} |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
return ''; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Render a single view to string |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $view |
|
37
|
|
|
* @param string $file |
|
38
|
|
|
* @param array $context |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
5 |
|
protected function renderView( $view, $file, $context ) { |
|
42
|
5 |
|
$__file = $file; |
|
43
|
|
|
|
|
44
|
5 |
|
$__context = array_merge( |
|
45
|
5 |
|
['global' => ViewService::getGlobals()], |
|
46
|
5 |
|
ViewService::compose( $view ), |
|
47
|
|
|
$context |
|
48
|
5 |
|
); |
|
49
|
|
|
|
|
50
|
5 |
|
$renderer = function() use ( $__file, $__context ) { |
|
51
|
5 |
|
ob_start(); |
|
52
|
5 |
|
extract( $__context ); |
|
53
|
5 |
|
include( $__file ); |
|
54
|
5 |
|
return ob_get_clean(); |
|
55
|
5 |
|
}; |
|
56
|
|
|
|
|
57
|
5 |
|
return $renderer(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Resolve a view or a view array to an absolute filepath |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $view |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
3 |
|
protected function resolveFile( $view ) { |
|
67
|
3 |
|
$file = locate_template( $view, false ); |
|
68
|
|
|
|
|
69
|
3 |
|
if ( ! $file ) { |
|
70
|
|
|
// locate_template failed to find the view - test if a valid absolute path was passed |
|
71
|
3 |
|
$file = $this->resolveFileFromFilesystem( $view ); |
|
72
|
3 |
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return $file; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Resolve the view if it exists on the filesystem |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $view |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
3 |
|
protected function resolveFileFromFilesystem( $view ) { |
|
84
|
3 |
|
return file_exists( $view ) ? $view : ''; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|