1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2017-2019 Atanas Angelov |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
7
|
|
|
* @link https://wpemerge.com/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WPEmerge\View; |
11
|
|
|
|
12
|
|
|
use WPEmerge\Helpers\MixedType; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Render view files with php. |
16
|
|
|
*/ |
17
|
|
|
class PhpViewFilesystemFinder implements ViewFinderInterface { |
18
|
|
|
/** |
19
|
|
|
* Custom views directories to check first. |
20
|
|
|
* |
21
|
|
|
* @var string[] |
22
|
|
|
*/ |
23
|
|
|
protected $directories = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @codeCoverageIgnore |
29
|
|
|
* @param string[] $directories |
30
|
|
|
*/ |
31
|
|
|
public function __construct( $directories = [] ) { |
32
|
|
|
$this->setDirectories( $directories ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the custom views directories. |
37
|
|
|
* |
38
|
|
|
* @codeCoverageIgnore |
39
|
|
|
* @return string[] |
40
|
|
|
*/ |
41
|
|
|
public function getDirectories() { |
42
|
|
|
return $this->directories; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set the custom views directories. |
47
|
|
|
* |
48
|
|
|
* @codeCoverageIgnore |
49
|
|
|
* @param string[] $directories |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function setDirectories( $directories ) { |
53
|
|
|
$this->directories = array_filter( array_map( [MixedType::class, 'removeTrailingSlash'], $directories ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function exists( $view ) { |
60
|
1 |
|
return ! empty( $this->resolveFilepath( $view ) ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
1 |
|
public function canonical( $view ) { |
67
|
1 |
|
return $this->resolveFilepath( $view ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Resolve a view to an absolute filepath. |
72
|
|
|
* |
73
|
|
|
* @param string $view |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
2 |
|
public function resolveFilepath( $view ) { |
77
|
2 |
|
$file = $this->resolveFromAbsoluteFilepath( $view ); |
78
|
|
|
|
79
|
2 |
|
if ( ! $file ) { |
80
|
2 |
|
$file = $this->resolveFromCustomDirectories( $view ); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
return $file; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Resolve a view if it is a valid absolute filepath. |
88
|
|
|
* |
89
|
|
|
* @param string $view |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
1 |
|
protected function resolveFromAbsoluteFilepath( $view ) { |
93
|
1 |
|
$path = realpath( MixedType::normalizePath( $view ) ); |
94
|
|
|
|
95
|
1 |
|
if ( ! empty( $path ) && ! is_file( $path ) ) { |
96
|
1 |
|
$path = ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $path ? $path : ''; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Resolve a view if it exists in the custom views directories. |
104
|
|
|
* |
105
|
|
|
* @param string $view |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
1 |
|
protected function resolveFromCustomDirectories( $view ) { |
109
|
1 |
|
$directories = $this->getDirectories(); |
110
|
|
|
|
111
|
1 |
|
foreach ( $directories as $directory ) { |
112
|
1 |
|
$file = MixedType::normalizePath( $directory . DIRECTORY_SEPARATOR . $view ); |
113
|
|
|
|
114
|
1 |
|
if ( ! is_file( $file ) ) { |
115
|
|
|
// Try adding a .php extension. |
116
|
1 |
|
$file .= '.php'; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$file = realpath( $file ); |
120
|
|
|
|
121
|
1 |
|
if ( $file && is_file( $file ) ) { |
122
|
1 |
|
return $file; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return ''; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|