|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WPEmerge\View; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use View as ViewService; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Render view files with php. |
|
10
|
|
|
*/ |
|
11
|
|
|
class PhpViewEngine implements ViewEngineInterface { |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritDoc} |
|
14
|
|
|
*/ |
|
15
|
1 |
|
public function exists( $view ) { |
|
16
|
1 |
|
$file = $this->resolveFilepath( $view ); |
|
17
|
1 |
|
return strlen( $file ) > 0; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritDoc} |
|
22
|
|
|
*/ |
|
23
|
3 |
|
public function canonical( $view ) { |
|
24
|
3 |
|
$file = $this->resolveFilepath( $view ); |
|
25
|
3 |
|
return $file; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function make( $views, $context = [] ) { |
|
32
|
6 |
|
foreach ( $views as $view ) { |
|
33
|
6 |
|
if ( $this->exists( $view ) ) { |
|
34
|
5 |
|
$filepath = $this->resolveFilepath( $view ); |
|
35
|
5 |
|
return $this->makeView( $view, $filepath, $context ); |
|
36
|
|
|
} |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
throw new Exception( 'View not found for "' . implode( ', ', $views ) . '"' ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create a view instance. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $name |
|
46
|
|
|
* @param string $filepath |
|
47
|
|
|
* @param array $context |
|
48
|
|
|
* @return string |
|
|
|
|
|
|
49
|
|
|
*/ |
|
50
|
5 |
|
protected function makeView( $name, $filepath, $context = [] ) { |
|
51
|
5 |
|
return (new PhpView()) |
|
52
|
5 |
|
->setName( $name ) |
|
53
|
5 |
|
->setFilepath( $filepath ) |
|
54
|
5 |
|
->with( $context ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Resolve a view or a view array to an absolute filepath |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $view |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
3 |
|
protected function resolveFilepath( $view ) { |
|
64
|
3 |
|
$file = locate_template( $view, false ); |
|
65
|
|
|
|
|
66
|
3 |
|
if ( ! $file ) { |
|
67
|
|
|
// locate_template failed to find the view - try adding a .php extension |
|
68
|
3 |
|
$file = locate_template( $view . '.php', false ); |
|
69
|
3 |
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
if ( ! $file ) { |
|
72
|
|
|
// locate_template failed to find the view - test if a valid absolute path was passed |
|
73
|
3 |
|
$file = $this->resolveFilepathFromFilesystem( $view ); |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
if ( $file ) { |
|
77
|
1 |
|
$file = realpath( $file ); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
return $file; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Resolve the view if it exists on the filesystem |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $view |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
3 |
|
protected function resolveFilepathFromFilesystem( $view ) { |
|
90
|
3 |
|
return file_exists( $view ) ? $view : ''; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: