Failed Conditions
Branch refactor/kernels (cc9370)
by Atanas
02:23
created

src/View/NameProxyViewEngine.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 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\Facades\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
	 * Array of filename_suffix=>engine_container_key bindings
27
	 *
28
	 * @var array
29
	 */
30
	protected $bindings = [];
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param array  $bindings
36
	 * @param string $default
37
	 */
38 3
	public function __construct( $bindings, $default = '' ) {
39 3
		$this->bindings = $bindings;
40
41 3
		if ( ! empty( $default ) ) {
42 1
			$this->default = $default;
43
		}
44 3
	}
45
46
	/**
47
	 * {@inheritDoc}
48
	 */
49 1
	public function exists( $view ) {
50 1
		$engine_key = $this->getBindingForFile( $view );
51 1
		$engine = Application::resolve( $engine_key );
0 ignored issues
show
The method resolve() does not exist on WPEmerge\Facades\Application. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
		/** @scrutinizer ignore-call */ 
52
  $engine = Application::resolve( $engine_key );
Loading history...
52 1
		return $engine->exists( $view );
53
	}
54
55
	/**
56
	 * {@inheritDoc}
57
	 */
58 1
	public function canonical( $view ) {
59 1
		$engine_key = $this->getBindingForFile( $view );
60 1
		$engine = Application::resolve( $engine_key );
61 1
		return $engine->canonical( $view );
62
	}
63
64
	/**
65
	 * {@inheritDoc}
66
	 * @throws ViewNotFoundException
67
	 */
68 2
	public function make( $views ) {
69 2
		foreach ( $views as $view ) {
70 2
			if ( $this->exists( $view ) ) {
71 1
				$engine_key = $this->getBindingForFile( $view );
72 1
				$engine = Application::resolve( $engine_key );
73 2
				return $engine->make( [$view] );
74
			}
75
		}
76
77 1
		throw new ViewNotFoundException( 'View not found for "' . implode( ', ', $views ) . '"' );
78
	}
79
80
	/**
81
	 * Get the default binding
82
	 *
83
	 * @return string $binding
84
	 */
85 2
	public function getDefaultBinding() {
86 2
		return $this->default;
87
	}
88
89
	/**
90
	 * Get all bindings
91
	 *
92
	 * @return array  $bindings
93
	 */
94 1
	public function getBindings() {
95 1
		return $this->bindings;
96
	}
97
98
	/**
99
	 * Get the engine key binding for a specific file
100
	 *
101
	 * @param  string $file
102
	 * @return string
103
	 */
104 1
	public function getBindingForFile( $file ) {
105 1
		$engine_key = $this->default;
106
107 1
		foreach ( $this->bindings as $suffix => $engine ) {
108 1
			if ( substr( $file, -strlen( $suffix ) ) === $suffix ) {
109 1
				$engine_key = $engine;
110 1
				break;
111
			}
112
		}
113
114 1
		return $engine_key;
115
	}
116
}
117