diff --git a/src/Infrastructure/View/TemplatedView.php b/src/Infrastructure/View/TemplatedView.php index d9c7193..c3fcc30 100644 --- a/src/Infrastructure/View/TemplatedView.php +++ b/src/Infrastructure/View/TemplatedView.php @@ -25,7 +25,7 @@ final class TemplatedView extends SimpleView { /** @var array */ - private $locations = []; + private $locations; /** * Instantiate a TemplatedView object. @@ -39,28 +39,10 @@ public function __construct( ViewFactory $view_factory, array $locations = [] ) { - $this->set_locations( $locations ); + array_walk( $locations, [ $this, 'add_location' ] ); parent::__construct( $path, $view_factory ); } - /** - * Set the locations for the templated view. - * - * @param array $locations Array of locations. - * @return self Modified templated view. - */ - public function set_locations( array $locations ): self { - if ( empty( $locations ) ) { - $locations = $this->get_default_locations(); - } - - $this->locations = array_map( function ( $location ) { - return $this->ensure_trailing_slash( $location ); - }, $locations ); - - return $this; - } - /** * Add a location to the templated view. * @@ -105,25 +87,8 @@ protected function validate( string $path ): string { * @return array Array of possible locations. */ private function get_locations( string $path ): array { - if ( empty( $this->locations ) ) { - $this->set_default_locations(); - } - return array_map( function ( $location ) use ( $path ) { return "{$location}{$path}"; }, $this->locations ); } - - /** - * Get the default locations for the templated view. - * - * @return array Array of default locations. - */ - private function get_default_locations(): array { - return [ - STYLESHEETPATH, - TEMPLATEPATH, - \dirname( __DIR__, 2 ), - ]; - } } diff --git a/src/Infrastructure/View/TemplatedViewFactory.php b/src/Infrastructure/View/TemplatedViewFactory.php index 9b1a4f9..3c69a93 100644 --- a/src/Infrastructure/View/TemplatedViewFactory.php +++ b/src/Infrastructure/View/TemplatedViewFactory.php @@ -34,6 +34,10 @@ final class TemplatedViewFactory implements Service, ViewFactory { * @param array $locations Array of locations to use. */ public function __construct( array $locations = [] ) { + if ( empty( $locations ) ) { + $locations = $this->get_default_locations(); + } + $this->locations = $locations; } @@ -46,4 +50,18 @@ public function __construct( array $locations = [] ) { public function create( string $relative_path ): View { return new TemplatedView( $relative_path, $this, $this->locations ); } + + + /** + * Get the default locations for the templated view. + * + * @return array Array of default locations. + */ + private function get_default_locations(): array { + return [ + \get_stylesheet_directory(), + \get_template_directory(), + \dirname( __DIR__, 3 ), + ]; + } }