-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove league/plates and replace with my own View Renderer Engine
- Loading branch information
Showing
10 changed files
with
156 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace StellarWP\Uplink\View\Contracts; | ||
|
||
use StellarWP\Uplink\View\Exceptions\FileNotFoundException; | ||
|
||
interface View { | ||
|
||
/** | ||
* Renders a view and returns it as a string to be echoed. | ||
* | ||
* @example If the server path is /app/views, and you wish to load /app/views/admin/notice.php, | ||
* pass `admin/notice` as the view name. | ||
* | ||
* @param string $name The relative path/name of the view file without extension. | ||
* | ||
* @param mixed[] $args Arguments to be extracted and passed to the view. | ||
* | ||
* @throws FileNotFoundException If the view file cannot be found. | ||
* | ||
* @return string | ||
*/ | ||
public function render( string $name, array $args = [] ): string; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace StellarWP\Uplink\View\Exceptions; | ||
|
||
use Exception; | ||
|
||
final class FileNotFoundException extends Exception { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace StellarWP\Uplink\View; | ||
|
||
use StellarWP\Uplink\View\Exceptions\FileNotFoundException; | ||
|
||
use Throwable; | ||
|
||
/** | ||
* Loads and renders PHP view files. | ||
*/ | ||
final class WordPress_View implements Contracts\View { | ||
|
||
/** | ||
* The server path to the views folder. | ||
* | ||
* @var string | ||
* | ||
* @example /app/views/ | ||
*/ | ||
private $directory; | ||
|
||
/** | ||
* The file extension of view files. | ||
* | ||
* @var string | ||
* | ||
* @example .php | ||
*/ | ||
private $extension; | ||
|
||
/** | ||
* @param string $directory The server path to the views folder. | ||
* @param string $extension The file extension of view files. | ||
*/ | ||
public function __construct( string $directory, string $extension = '.php' ) { | ||
$this->directory = trailingslashit( realpath( $directory ) ); | ||
$this->extension = $extension; | ||
} | ||
|
||
/** | ||
* Renders a view and returns it as a string to be echoed. | ||
* | ||
* @example If the server path is /app/views, and you wish to load /app/views/admin/notice.php, | ||
* pass `admin/notice` as the view name. | ||
* | ||
* @param string $name The relative path/name of the view file without extension. | ||
* | ||
* @param mixed[] $args Arguments to be extracted and passed to the view. | ||
* | ||
* @throws FileNotFoundException If the view file cannot be found. | ||
* | ||
* @return string | ||
*/ | ||
public function render( string $name, array $args = [] ): string { | ||
$file = $this->get_path( $name ); | ||
|
||
try { | ||
$level = ob_get_level(); | ||
ob_start(); | ||
|
||
extract( $args ); | ||
include $file; | ||
|
||
return (string) ob_get_clean(); | ||
} catch ( Throwable $e ) { | ||
while ( ob_get_level() > $level ) { | ||
ob_end_clean(); | ||
} | ||
|
||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* Get the absolute server path to a view file. | ||
* | ||
* @param string $name The relative view path/name, e.g. `admin/notice`. | ||
* | ||
* @throws FileNotFoundException If the view file cannot be found. | ||
* | ||
* @return string The absolute path to the view file. | ||
*/ | ||
private function get_path( string $name ): string { | ||
$file = $this->directory . $name . $this->extension; | ||
$path = realpath( $file ); | ||
|
||
if( $path === false ) { | ||
throw new FileNotFoundException( | ||
sprintf( __( 'View file "%s" not found or not readable.', '%TEXTDOMAIN%' ), $file ) | ||
); | ||
} | ||
|
||
return $path; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters