Skip to content

Commit

Permalink
Remove league/plates and replace with my own View Renderer Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
defunctl committed Oct 20, 2023
1 parent 2748b90 commit a796511
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 18 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"require": {
"php": ">=7.1",
"ext-json": "*",
"league/plates": ">=3.4",
"stellarwp/container-contract": "^1.0"
},
"config": {
Expand Down
7 changes: 5 additions & 2 deletions src/Uplink/Components/Admin/Authorize_Button_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
namespace StellarWP\Uplink\Components\Admin;

use InvalidArgumentException;
use League\Plates\Engine;
use StellarWP\Uplink\API\V3\Auth\Contracts\Auth_Url;
use StellarWP\Uplink\Auth\Admin\Disconnect_Controller;
use StellarWP\Uplink\Auth\Authorizer;
use StellarWP\Uplink\Auth\Nonce;
use StellarWP\Uplink\Auth\Token\Contracts\Token_Manager;
use StellarWP\Uplink\Components\Controller;
use StellarWP\Uplink\Config;
use StellarWP\Uplink\View\Contracts\View;

final class Authorize_Button_Controller extends Controller {

/**
* The view file, without ext, relative to the root views directory.
*/
public const VIEW = 'admin/authorize-button';

/**
Expand Down Expand Up @@ -45,7 +48,7 @@ final class Authorize_Button_Controller extends Controller {
private $auth_url = '';

public function __construct(
Engine $view,
View $view,
Authorizer $authorizer,
Token_Manager $token_manager,
Nonce $nonce,
Expand Down
18 changes: 11 additions & 7 deletions src/Uplink/Components/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@

namespace StellarWP\Uplink\Components;

use League\Plates\Engine;
use StellarWP\Uplink\View\Contracts\View;

/**
* Component/View controller made to accept arguments and render
* them in a view file.
*/
abstract class Controller {

/**
* The Plates View Engine.
* The View Engine to render views.
*
* @var Engine
* @var View
*/
protected $view;

/**
* Echo the plates view.
* Render the view file.
*
* @param mixed[] $args An optional array of arguments to utilize when rendering.
* @param mixed[] $args An optional array of arguments to utilize when rendering.
*/
abstract public function render( array $args = [] ): void;

/**
* @param Engine $view
* @param View $view The View Engine to render views.
*/
public function __construct( Engine $view ) {
public function __construct( View $view ) {
$this->view = $view;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Uplink/Notice/Notice_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
final class Notice_Controller extends Controller {

/**
* The view file, without ext, relative to the root views directory.
*/
public const VIEW = 'admin/notice';

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Uplink/View/Contracts/View.php
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;

}
9 changes: 9 additions & 0 deletions src/Uplink/View/Exceptions/FileNotFoundException.php
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 {

}
12 changes: 6 additions & 6 deletions src/Uplink/View/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

namespace StellarWP\Uplink\View;

use League\Plates\Engine;
use StellarWP\Uplink\Contracts\Abstract_Provider;
use StellarWP\Uplink\View\Contracts\View;

final class Provider extends Abstract_Provider {

/**
* Configure the directory League Plates looks for view files.
*
* @link https://platesphp.com/
* Configure the View Renderer.
*/
public function register() {
$this->container->singleton(
Engine::class,
new Engine( trailingslashit( __DIR__ . '/../../views' ) )
WordPress_View::class,
new WordPress_View( __DIR__ . '/../../views' )
);

$this->container->bind( View::class, $this->container->get( WordPress_View::class ) );
}
}
97 changes: 97 additions & 0 deletions src/Uplink/View/WordPress_View.php
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;
}

}
1 change: 0 additions & 1 deletion src/views/admin/authorize-button.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*
* @see \StellarWP\Uplink\Components\Admin\Authorize_Button_Controller
*
* @var \League\Plates\Template\Template $this
* @var string $link_text The link text, changes based on whether the user is authorized to authorize :)
* @var string $url The location the link goes to, either the custom origin URL, or a link to the admin.
* @var string $target The link target.
Expand Down
1 change: 0 additions & 1 deletion src/views/admin/notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*
* @see \StellarWP\Uplink\Notice\Notice_Controller
*
* @var \League\Plates\Template\Template $this
* @var string $message The message to display.
* @var string $classes The CSS classes for the notice.
*/
Expand Down

0 comments on commit a796511

Please sign in to comment.