| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 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\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 string[] |
| 22 |
*/ |
| 23 |
protected $directories = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor. |
| 27 |
* |
| 28 |
* @codeCoverageIgnore |
| 29 |
* @param 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 string[] |
| 40 |
*/ |
| 41 |
public function getDirectories() { |
| 42 |
return $this->directories; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set the custom views directories. |
| 47 |
* |
| 48 |
* @codeCoverageIgnore |
| 49 |
* @param 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 |
public function exists( $view ) { |
| 60 |
return ! empty( $this->resolveFilepath( $view ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* {@inheritDoc} |
| 65 |
*/ |
| 66 |
public function canonical( $view ) { |
| 67 |
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 |
public function resolveFilepath( $view ) { |
| 77 |
$file = $this->resolveFromAbsoluteFilepath( $view ); |
| 78 |
|
| 79 |
if ( ! $file ) { |
| 80 |
$file = $this->resolveFromCustomDirectories( $view ); |
| 81 |
} |
| 82 |
|
| 83 |
return $file; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Resolve a view if it is a valid absolute filepath. |
| 88 |
* |
| 89 |
* @param string $view |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
protected function resolveFromAbsoluteFilepath( $view ) { |
| 93 |
$path = realpath( MixedType::normalizePath( $view ) ); |
| 94 |
|
| 95 |
if ( ! empty( $path ) && ! is_file( $path ) ) { |
| 96 |
$path = ''; |
| 97 |
} |
| 98 |
|
| 99 |
return $path ? $path : ''; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Resolve a view if it exists in the custom views directories. |
| 104 |
* |
| 105 |
* @param string $view |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
protected function resolveFromCustomDirectories( $view ) { |
| 109 |
$directories = $this->getDirectories(); |
| 110 |
|
| 111 |
foreach ( $directories as $directory ) { |
| 112 |
$file = MixedType::normalizePath( $directory . DIRECTORY_SEPARATOR . $view ); |
| 113 |
|
| 114 |
if ( ! is_file( $file ) ) { |
| 115 |
// Try adding a .php extension. |
| 116 |
$file .= '.php'; |
| 117 |
} |
| 118 |
|
| 119 |
$file = realpath( $file ); |
| 120 |
|
| 121 |
if ( $file && is_file( $file ) ) { |
| 122 |
return $file; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return ''; |
| 127 |
} |
| 128 |
} |
| 129 |
|