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

PhpViewFilesystemFinder::getDirectories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
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
use WPEmerge\Helpers\MixedType;
13
14
/**
15
 * Render view files with php.
16
 */
17
class PhpViewFilesystemFinder implements ViewFinderInterface {
18
	/**
19
	 * Custom views directories to check first.
20
	 *
21
	 * @var array<string>
22
	 */
23
	protected $directories = [];
24
25
	/**
26
	 * Constructor.
27
	 *
28
	 * @codeCoverageIgnore
29
	 * @param array<string> $directories
30
	 */
31
	public function __construct( $directories = [] ) {
32
		$this->setDirectories( $directories );
33
	}
34
35
	/**
36
	 * Get the custom views directories.
37
	 *
38
	 * @codeCoverageIgnore
39
	 * @return array<string>
40
	 */
41
	public function getDirectories() {
42
		return $this->directories;
43
	}
44
45
	/**
46
	 * Set the custom views directories.
47
	 *
48
	 * @codeCoverageIgnore
49
	 * @param  array<string> $directories
50
	 * @return void
51
	 */
52
	public function setDirectories( $directories ) {
53
		$this->directories = array_filter( array_map( [MixedType::class, 'removeTrailingSlash'], $directories ) );
54
	}
55
56
	/**
57
	 * {@inheritDoc}
58
	 */
59 1
	public function exists( $view ) {
60 1
		return ! empty( $this->resolveFilepath( $view ) );
61
	}
62
63
	/**
64
	 * {@inheritDoc}
65
	 */
66 1
	public function canonical( $view ) {
67 1
		return $this->resolveFilepath( $view );
68
	}
69
70
	/**
71
	 * Resolve a view to an absolute filepath.
72
	 *
73
	 * @param  string $view
74
	 * @return string
75
	 */
76 3
	public function resolveFilepath( $view ) {
77 3
		$file = $this->resolveFromAbsoluteFilepath( $view );
78
79 3
		if ( ! $file ) {
80 3
			$file = $this->resolveFromCustomDirectories( $view );
81 3
		}
82
83 3
		if ( ! $file ) {
84 3
			$file = $this->resolveFromWordPress( $view );
85 3
		}
86
87 3
		return $file;
88
	}
89
90
	/**
91
	 * Resolve a view if it is a valid absolute filepath.
92
	 *
93
	 * @param  string $view
94
	 * @return string
95
	 */
96 1
	protected function resolveFromAbsoluteFilepath( $view ) {
97 1
		$path = realpath( MixedType::normalizePath( $view ) );
98
99 1
		if ( ! empty( $path ) && ! is_file( $path ) ) {
100 1
			$path = '';
101 1
		}
102
103 1
		return $path ? $path : '';
104
	}
105
106
	/**
107
	 * Resolve a view if it exists in the custom views directories.
108
	 *
109
	 * @param  string $view
110
	 * @return string
111
	 */
112 1
	protected function resolveFromCustomDirectories( $view ) {
113 1
		$directories = $this->getDirectories();
114
115 1
		foreach ( $directories as $directory ) {
116 1
			$file = MixedType::normalizePath( $directory . DIRECTORY_SEPARATOR . $view );
117
118 1
			if ( ! file_exists( $file ) ) {
119
				// Try adding a .php extension.
120 1
				$file .= '.php';
121 1
			}
122
123 1
			if ( file_exists( $file ) ) {
124 1
				return $file;
125
			}
126 1
		}
127
128 1
		return '';
129
	}
130
131
	/**
132
	 * Resolve a view if WordPress can locate it.
133
	 *
134
	 * @param  string $view
135
	 * @return string
136
	 */
137 1
	protected function resolveFromWordPress( $view ) {
138 1
		$file = locate_template( $view, false );
139
140 1
		if ( ! $file ) {
141
			// Try adding a .php extension.
142 1
			$file = locate_template( $view . '.php', false );
143 1
		}
144
145 1
		return $this->resolveFromAbsoluteFilepath( $file );
146
	}
147
}
148