Passed
Push — master ( 783123...126691 )
by Atanas
02:41
created

PhpViewEngine::resolveFilepath()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 8
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
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
/**
13
 * Render view files with php.
14
 */
15
class PhpViewEngine implements ViewEngineInterface {
16
	/**
17
	 * View finder.
18
	 *
19
	 * @var PhpViewFilesystemFinder
20
	 */
21
	protected $finder = [];
22
23
	/**
24
	 * Constructor.
25
	 *
26
	 * @codeCoverageIgnore
27
	 * @param PhpViewFilesystemFinder $finder
28
	 */
29
	public function __construct( PhpViewFilesystemFinder $finder ) {
30
		$this->setFinder( $finder );
31
	}
32
33
	/**
34
	 * Get the custom views directories.
35
	 *
36
	 * @codeCoverageIgnore
37
	 * @return PhpViewFilesystemFinder
38
	 */
39
	public function getFinder() {
40
		return $this->finder;
41
	}
42
43
	/**
44
	 * Set the view finder.
45
	 *
46
	 * @codeCoverageIgnore
47
	 * @param  PhpViewFilesystemFinder
48
	 * @return void
49
	 */
50
	public function setFinder( PhpViewFilesystemFinder $finder ) {
51
		$this->finder = $finder;
52
	}
53
54
	/**
55
	 * {@inheritDoc}
56
	 */
57 1
	public function exists( $view ) {
58 1
		return $this->getFinder()->exists( $view );
59
	}
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64 1
	public function canonical( $view ) {
65 1
		return $this->getFinder()->canonical( $view );
66
	}
67
68
	/**
69
	 * {@inheritDoc}
70
	 * @throws ViewNotFoundException
71
	 */
72 2
	public function make( $views ) {
73 2
		foreach ( $views as $view ) {
74 2
			if ( $this->exists( $view ) ) {
75 1
				$filepath = $this->finder->resolveFilepath( $view );
76 1
				return $this->makeView( $view, $filepath );
77
			}
78 1
		}
79
80 1
		throw new ViewNotFoundException( 'View not found for "' . implode( ', ', $views ) . '"' );
81
	}
82
83
	/**
84
	 * Create a view instance.
85
	 *
86
	 * @param  string $name
87
	 * @param  string $filepath
88
	 * @return ViewInterface
89
	 * @throws ViewNotFoundException
90
	 */
91 1
	protected function makeView( $name, $filepath ) {
92 1
		$view = (new PhpView())
93 1
			->setName( $name )
94 1
			->setFilepath( $filepath );
95
96 1
		$layout = $this->getViewLayout( $view );
97
98 1
		if ( $layout !== null ) {
99
			$view->setLayout( $layout );
100
		}
101
102 1
		return $view;
103
	}
104
105
	/**
106
	 * Create a view instance for the given view's layout header, if any.
107
	 *
108
	 * @param  PhpView $view
109
	 * @return ViewInterface
110
	 * @throws ViewNotFoundException
111
	 */
112 2
	protected function getViewLayout( PhpView $view ) {
113 2
		$layout_headers = array_filter( get_file_data(
114 2
			$view->getFilepath(),
115 2
			['App Layout']
116 2
		) );
117
118 2
		if ( empty( $layout_headers ) ) {
119 1
			return null;
120
		}
121
122 2
		$layout_file = trim( $layout_headers[0] );
123
124 2
		if ( ! $this->exists( $layout_file ) ) {
125 1
			throw new ViewNotFoundException( 'View layout not found for "' . $layout_file . '"' );
126
		}
127
128 1
		return $this->makeView( $this->canonical( $layout_file ), $this->finder->resolveFilepath( $layout_file ) );
129
	}
130
}
131