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\Application\Application; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Render view files with different engines depending on their filename |
16
|
|
|
*/ |
17
|
|
|
class NameProxyViewEngine implements ViewEngineInterface { |
18
|
|
|
/** |
19
|
|
|
* Container key of default engine to use |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $default = WPEMERGE_VIEW_PHP_VIEW_ENGINE_KEY; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Application. |
27
|
|
|
* |
28
|
|
|
* @var Application |
29
|
|
|
*/ |
30
|
|
|
protected $app = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Array of filename_suffix=>engine_container_key bindings |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $bindings = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor |
41
|
|
|
* |
42
|
|
|
* @param Application $app |
43
|
|
|
* @param array $bindings |
44
|
|
|
* @param string $default |
45
|
|
|
*/ |
46
|
3 |
|
public function __construct( Application $app, $bindings, $default = '' ) { |
47
|
3 |
|
$this->app = $app; |
48
|
3 |
|
$this->bindings = $bindings; |
49
|
|
|
|
50
|
3 |
|
if ( ! empty( $default ) ) { |
51
|
1 |
|
$this->default = $default; |
52
|
|
|
} |
53
|
3 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
1 |
|
public function exists( $view ) { |
59
|
1 |
|
$engine_key = $this->getBindingForFile( $view ); |
60
|
1 |
|
$engine = $this->app->resolve( $engine_key ); |
61
|
1 |
|
return $engine->exists( $view ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function canonical( $view ) { |
68
|
1 |
|
$engine_key = $this->getBindingForFile( $view ); |
69
|
1 |
|
$engine = $this->app->resolve( $engine_key ); |
70
|
1 |
|
return $engine->canonical( $view ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
* @throws ViewNotFoundException |
76
|
|
|
*/ |
77
|
2 |
|
public function make( $views ) { |
78
|
2 |
|
foreach ( $views as $view ) { |
79
|
2 |
|
if ( $this->exists( $view ) ) { |
80
|
1 |
|
$engine_key = $this->getBindingForFile( $view ); |
81
|
1 |
|
$engine = $this->app->resolve( $engine_key ); |
82
|
1 |
|
return $engine->make( [$view] ); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
throw new ViewNotFoundException( 'View not found for "' . implode( ', ', $views ) . '"' ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the default binding |
91
|
|
|
* |
92
|
|
|
* @return string $binding |
93
|
|
|
*/ |
94
|
2 |
|
public function getDefaultBinding() { |
95
|
2 |
|
return $this->default; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get all bindings |
100
|
|
|
* |
101
|
|
|
* @return array $bindings |
102
|
|
|
*/ |
103
|
1 |
|
public function getBindings() { |
104
|
1 |
|
return $this->bindings; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the engine key binding for a specific file |
109
|
|
|
* |
110
|
|
|
* @param string $file |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function getBindingForFile( $file ) { |
114
|
1 |
|
$engine_key = $this->default; |
115
|
|
|
|
116
|
1 |
|
foreach ( $this->bindings as $suffix => $engine ) { |
117
|
1 |
|
if ( substr( $file, -strlen( $suffix ) ) === $suffix ) { |
118
|
1 |
|
$engine_key = $engine; |
119
|
1 |
|
break; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return $engine_key; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|