| 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\Application\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 |
* Application. |
| 27 |
* |
| 28 |
* @var Application |
| 29 |
*/ |
| 30 |
protected $app = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Array of filename_suffix=>engine_container_key bindings |
| 34 |
* |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
protected $bindings = []; |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor |
| 41 |
* |
| 42 |
* @param Application $app |
| 43 |
* @param array $bindings |
| 44 |
* @param string $default |
| 45 |
*/ |
| 46 |
public function __construct( Application $app, $bindings, $default = '' ) { |
| 47 |
$this->app = $app; |
| 48 |
$this->bindings = $bindings; |
| 49 |
|
| 50 |
if ( ! empty( $default ) ) { |
| 51 |
$this->default = $default; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* {@inheritDoc} |
| 57 |
*/ |
| 58 |
public function exists( $view ) { |
| 59 |
$engine_key = $this->getBindingForFile( $view ); |
| 60 |
$engine = $this->app->resolve( $engine_key ); |
| 61 |
return $engine->exists( $view ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* {@inheritDoc} |
| 66 |
*/ |
| 67 |
public function canonical( $view ) { |
| 68 |
$engine_key = $this->getBindingForFile( $view ); |
| 69 |
$engine = $this->app->resolve( $engine_key ); |
| 70 |
return $engine->canonical( $view ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* {@inheritDoc} |
| 75 |
* @throws ViewNotFoundException |
| 76 |
*/ |
| 77 |
public function make( $views ) { |
| 78 |
foreach ( $views as $view ) { |
| 79 |
if ( $this->exists( $view ) ) { |
| 80 |
$engine_key = $this->getBindingForFile( $view ); |
| 81 |
$engine = $this->app->resolve( $engine_key ); |
| 82 |
return $engine->make( [$view] ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
throw new ViewNotFoundException( 'View not found for "' . implode( ', ', $views ) . '"' ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the default binding |
| 91 |
* |
| 92 |
* @return string $binding |
| 93 |
*/ |
| 94 |
public function getDefaultBinding() { |
| 95 |
return $this->default; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get all bindings |
| 100 |
* |
| 101 |
* @return array $bindings |
| 102 |
*/ |
| 103 |
public function getBindings() { |
| 104 |
return $this->bindings; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get the engine key binding for a specific file |
| 109 |
* |
| 110 |
* @param string $file |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function getBindingForFile( $file ) { |
| 114 |
$engine_key = $this->default; |
| 115 |
|
| 116 |
foreach ( $this->bindings as $suffix => $engine ) { |
| 117 |
if ( substr( $file, -strlen( $suffix ) ) === $suffix ) { |
| 118 |
$engine_key = $engine; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $engine_key; |
| 124 |
} |
| 125 |
} |
| 126 |
|