Skip to content

Commit

Permalink
Fix template fodler routing
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Mar 24, 2019
1 parent 6e4e3a6 commit 1417643
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
39 changes: 2 additions & 37 deletions src/Infrastructure/View/TemplatedView.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
final class TemplatedView extends SimpleView {

/** @var array<string> */
private $locations = [];
private $locations;

/**
* Instantiate a TemplatedView object.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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 ),
];
}
}
18 changes: 18 additions & 0 deletions src/Infrastructure/View/TemplatedViewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 ),
];
}
}

0 comments on commit 1417643

Please sign in to comment.