Passed
Push — master ( 8dc024...328814 )
by Atanas
02:54
created

PhpViewEngine::getViewLayout()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\View;
4
5
use WPEmerge\Exceptions\ViewException;
6
7
/**
8
 * Render view files with php.
9
 */
10
class PhpViewEngine implements ViewEngineInterface {
11
	/**
12
	 * {@inheritDoc}
13
	 */
14 1
	public function exists( $view ) {
15 1
		$file = $this->resolveFilepath( $view );
16 1
		return strlen( $file ) > 0;
17
	}
18
19
	/**
20
	 * {@inheritDoc}
21
	 */
22 1
	public function canonical( $view ) {
23 1
		return $this->resolveFilepath( $view );
24
	}
25
26
	/**
27
	 * {@inheritDoc}
28
	 */
29 2
	public function make( $views ) {
30 2
		foreach ( $views as $view ) {
31 2
			if ( $this->exists( $view ) ) {
32 1
				$filepath = $this->resolveFilepath( $view );
33 2
				return $this->makeView( $view, $filepath );
34
			}
35
		}
36
37 1
		throw new ViewException( 'View not found for "' . implode( ', ', $views ) . '"' );
38
	}
39
40
	/**
41
	 * Create a view instance.
42
	 *
43
	 * @param  string        $name
44
	 * @param  string        $filepath
45
	 * @return ViewInterface
46
	 */
47 2
	protected function makeView( $name, $filepath ) {
48 2
		$view = (new PhpView())
49 2
			->setName( $name )
50 2
			->setFilepath( $filepath );
51
52 2
		$layout = $this->getViewLayout( $view );
53
54 2
		if ( $layout !== null ) {
55 1
			$view->setLayout( $layout );
56
		}
57
58 2
		return $view;
59
	}
60
61
	/**
62
	 * Create a view instance for the given view's layout header, if any.
63
	 *
64
	 * @param  PhpView       $view
65
	 * @return ViewInterface
66
	 */
67 2
	protected function getViewLayout( PhpView $view ) {
68 2
		$layout_headers = array_filter( get_file_data(
69 2
			$view->getFilepath(),
70 2
			['App Layout']
71
		) );
72
73 2
		if ( empty( $layout_headers ) ) {
74 1
			return null;
75
		}
76
77 2
		$layout_file = trim( $layout_headers[0] );
78
79 2
		if ( ! $this->exists( $layout_file ) ) {
80 1
			throw new ViewException( 'View layout not found for "' . $layout_file . '"' );
81
		}
82
83 1
		return $this->makeView( $this->canonical( $layout_file ), $this->resolveFilepath( $layout_file ) );
84
	}
85
86
	/**
87
	 * Resolve a view name to an absolute filepath.
88
	 *
89
	 * @param  string $view
90
	 * @return string
91
	 */
92 1
	protected function resolveFilepath( $view ) {
93 1
		$file = locate_template( $view, false );
94
95 1
		if ( ! $file ) {
96
			// locate_template failed to find the view - try adding a .php extension.
97 1
			$file = locate_template( $view . '.php', false );
98
		}
99
100 1
		if ( ! $file ) {
101
			// locate_template failed to find the view - test if a valid absolute path was passed.
102 1
			$file = $this->resolveFilepathFromFilesystem( $view );
103
		}
104
105 1
		if ( $file ) {
106 1
			$file = realpath( $file );
107
		}
108
109 1
		return $file;
110
	}
111
112
	/**
113
	 * Resolve a view if it exists on the filesystem.
114
	 *
115
	 * @param  string $view
116
	 * @return string
117
	 */
118 1
	protected function resolveFilepathFromFilesystem( $view ) {
119 1
		return file_exists( $view ) ? $view : '';
120
	}
121
}
122