From b36fd437e0dae0589826d6f485906b8a8660b46a Mon Sep 17 00:00:00 2001 From: Theodore Brown Date: Tue, 10 Dec 2024 23:37:41 -0600 Subject: [PATCH] Remove custom loaders --- README.md | 72 +---------- src/Handlebars/Handlebars.php | 139 +------------------- src/Handlebars/HandlebarsString.php | 65 ---------- src/Handlebars/Helpers.php | 2 +- src/Handlebars/Loader.php | 29 ----- src/Handlebars/Loader/FilesystemLoader.php | 144 --------------------- src/Handlebars/Loader/StringLoader.php | 36 ------ tests/Handlebars/HandlebarsTest.php | 12 +- 8 files changed, 11 insertions(+), 488 deletions(-) delete mode 100644 src/Handlebars/HandlebarsString.php delete mode 100755 src/Handlebars/Loader.php delete mode 100755 src/Handlebars/Loader/FilesystemLoader.php delete mode 100755 src/Handlebars/Loader/StringLoader.php diff --git a/README.md b/README.md index 11bff2e..fe3a62d 100644 --- a/README.md +++ b/README.md @@ -1,65 +1,30 @@ -[![PHPUnit](https://github.com/salesforce/handlebars-php/actions/workflows/ci.yml/badge.svg)](https://github.com/salesforce/handlebars-php/actions/workflows/ci.yml) - ---- - #handlebars-php ---- - #### A simple, logic-less, yet powerful templating engine for PHP ---- - Name: **handlebars-php** License: MIT -Requirements: PHP >= 7.4 - ---- - - ## About Handlebars Handlebars provides the power necessary to let you build semantic templates effectively with no frustration, that keep the view and the code separated like we all know they should be. - Fork of: [Handlebars.php by XaminProject](https://github.com/mardix/Handlebars) Handlebars, is the PHP port of [Handlebars.js](http://handlebarsjs.com/) ---- - ## Install Handlebars - -You can just download Handlebars.php as is, or with Composer. - -To install with composer, add the following in the require key in your **composer.json** file - -`"salesforce/handlebars-php": "1.*"` - -composer.json - -```json -{ - "name": "myapp/name", - "description": "My awesome app name", - "require": { - "salesforce/handlebars-php": "1.*" - } -} ``` - ------ +composer require salesforce/handlebars-php:"^3.0" +``` ## Getting Started At the minimum, we are required to have an array model and a template string. Alternatively we can have a file containing handlebars (or html, text, etc) expression. - - #### Template Handlebars templates look like regular HTML, with embedded handlebars expressions. @@ -79,40 +44,10 @@ The string above can be used as is in your PHP file, or be put in a file (ie: */ #### PHP file -Now the we've created our template file, in a php file (index.php) we'll create the data to passed to the model. The model is a key/value array. +Now that we've created our template file, in a php file (index.php) we'll create the data to passed to the model. The model is a key/value array. Below we are going to create the Handlebars object, set the partials loader, and put some data in the model. -**/index.php** - -```php - "html" - ] -); - -# We'll use $handlebars throughout this the examples, assuming the will be all set this way -$handlebars = new Handlebars([ - "loader" => $partialsLoader, - "partials_loader" => $partialsLoader -]); - -# Will render the model to the templates/main.tpl template -$model = [...]; -echo $handlebars->render("main", $model); -``` - #### Assign Data The simplest way to assign data is to create an Array model. The model will contain all the data that will be passed to the template. @@ -740,7 +675,6 @@ $object->{'@unknown'} = 'zucchini'; $data = ['objects' => [$object]]; $engine = new \Handlebars\Handlebars(array( - 'loader' => new \Handlebars\Loader\StringLoader(), 'helpers' => new \Handlebars\Helpers(), 'enableDataVariables'=> $enabled, )); diff --git a/src/Handlebars/Handlebars.php b/src/Handlebars/Handlebars.php index 4c4f518..0b2205a 100755 --- a/src/Handlebars/Handlebars.php +++ b/src/Handlebars/Handlebars.php @@ -16,40 +16,18 @@ namespace Handlebars; -use Handlebars\Loader\StringLoader; use InvalidArgumentException; class Handlebars { - private static $instance = null; - const VERSION = '2.2'; - const OPTION_ENABLE_DATA_VARIABLES = 'enableDataVariables'; - /** - * factory method - * - * @param array $options see __construct's options parameter - */ - public static function factory(array $options = []): Handlebars - { - if (! self::$instance) { - self::$instance = new self($options); - } - - return self::$instance; - } - private Tokenizer $tokenizer; private Parser $parser; private Helpers $helpers; - private Loader $loader; - - private Loader $partialsLoader; - private array $aliases = []; /** @@ -61,34 +39,18 @@ public static function factory(array $options = []): Handlebars * Handlebars engine constructor * $options array can contain : * helpers => Helpers object - * loader => Loader object - * partials_loader => Loader object * enableDataVariables => boolean. Enables @data variables (default: false) * * @param array $options array of options to set * * @throws \InvalidArgumentException */ - public function __construct(Array $options = []) + public function __construct(array $options = []) { if (isset($options['helpers'])) { $this->setHelpers($options['helpers']); } - if (isset($options['loader'])) { - $this->setLoader($options['loader']); - } - - if (isset($options['partials_loader'])) { - $this->setPartialsLoader($options['partials_loader']); - } - - if (isset($options['partials_alias']) - && is_array($options['partials_alias']) - ) { - $this->aliases = $options['partials_alias']; - } - if (isset($options[self::OPTION_ENABLE_DATA_VARIABLES])) { if (!is_bool($options[self::OPTION_ENABLE_DATA_VARIABLES])) { throw new InvalidArgumentException( @@ -110,16 +72,6 @@ public function render(string $template, $data): string { return $this->loadTemplate($template)->render($data); } - /** - * To invoke when this object is called as a function - * - * @param string $template template name - * @param mixed $data data to use as context - */ - public function __invoke(string $template, $data): string - { - return $this->render($template, $data); - } /** * Set helpers for current engine @@ -172,52 +124,6 @@ public function removeHelper(string $name): void $this->getHelpers()->remove($name); } - /** - * Set current loader - */ - public function setLoader(Loader $loader): void - { - $this->loader = $loader; - } - - /** - * Get current loader - */ - public function getLoader(): Loader - { - if (! isset($this->loader)) { - $this->loader = new StringLoader(); - } - return $this->loader; - } - - /** - * Set current partials loader - */ - public function setPartialsLoader(Loader $loader): void - { - $this->partialsLoader = $loader; - } - - /** - * Get current partials loader - */ - public function getPartialsLoader(): Loader - { - if (!isset($this->partialsLoader)) { - $this->partialsLoader = new StringLoader(); - } - return $this->partialsLoader; - } - - /** - * Set the Handlebars Tokenizer instance. - */ - public function setTokenizer(Tokenizer $tokenizer): void - { - $this->tokenizer = $tokenizer; - } - /** * Get the current Handlebars Tokenizer instance. * @@ -233,14 +139,6 @@ public function getTokenizer(): Tokenizer return $this->tokenizer; } - /** - * Set the Handlebars Parser instance. - */ - public function setParser(Parser $parser): void - { - $this->parser = $parser; - } - /** * Get the current Handlebars Parser instance. * @@ -266,9 +164,8 @@ public function isDataVariablesEnabled(): bool /** * Load a template by name with current template loader */ - public function loadTemplate(string $name): Template + public function loadTemplate(string $source): Template { - $source = $this->getLoader()->load($name); $tree = $this->tokenize($source); return new Template($this, $tree, $source); } @@ -281,36 +178,8 @@ public function loadPartial(string $name): Template if (isset($this->aliases[$name])) { $name = $this->aliases[$name]; } - $source = $this->getPartialsLoader()->load($name); - $tree = $this->tokenize($source); - return new Template($this, $tree, $source); - } - - /** - * Register partial alias - */ - public function registerPartial(string $alias, string $content): void - { - $this->aliases[$alias] = $content; - } - - /** - * Un-register partial alias - */ - public function unRegisterPartial(string $alias): void - { - if (isset($this->aliases[$alias])) { - unset($this->aliases[$alias]); - } - } - - /** - * Load string into a template object - */ - public function loadString(string $source): Template - { - $tree = $this->tokenize($source); - return new Template($this, $tree, $source); + $tree = $this->tokenize($name); + return new Template($this, $tree, $name); } /** diff --git a/src/Handlebars/HandlebarsString.php b/src/Handlebars/HandlebarsString.php deleted file mode 100644 index 9efc101..0000000 --- a/src/Handlebars/HandlebarsString.php +++ /dev/null @@ -1,65 +0,0 @@ - - * @author Behrooz Shabani - * @author Mardix - * @copyright 2012 (c) ParsPooyesh Co - * @copyright 2013 (c) Behrooz Shabani - * @copyright 2013 (c) Mardix - * @license MIT - * @link http://voodoophp.org/docs/handlebars - */ - -namespace Handlebars; - -class HandlebarsString -{ - private $string = ""; - - /** - * Create new string - * - * @param string $string input source - */ - public function __construct($string) - { - $this->setString($string); - } - - /** - * To String - * - * @return string - */ - public function __toString() - { - return $this->getString(); - } - - /** - * Get string - * - * @return string - */ - public function getString() - { - return $this->string; - } - - /** - * Create new string - * - * @param string $string input source - * - * @return void - */ - public function setString($string) - { - $this->string = $string; - } - -} \ No newline at end of file diff --git a/src/Handlebars/Helpers.php b/src/Handlebars/Helpers.php index 318146f..d430038 100755 --- a/src/Handlebars/Helpers.php +++ b/src/Handlebars/Helpers.php @@ -177,7 +177,7 @@ public function isEmpty(): bool */ public function helperIf(Template $template, Context $context, string $args, string $source) { - $tpl = $template->getEngine()->loadString('{{#if ' . $args . '}}' . $source . '{{/if}}'); + $tpl = $template->getEngine()->loadTemplate('{{#if ' . $args . '}}' . $source . '{{/if}}'); $tree = $tpl->getTree(); $tmp = $context->get($args); if ($tmp) { diff --git a/src/Handlebars/Loader.php b/src/Handlebars/Loader.php deleted file mode 100755 index 91f71b9..0000000 --- a/src/Handlebars/Loader.php +++ /dev/null @@ -1,29 +0,0 @@ - - * @author Behrooz Shabani - * @copyright 2012 (c) ParsPooyesh Co - * @copyright 2013 (c) Behrooz Shabani - * @license MIT - * @link http://voodoophp.org/docs/handlebars - */ - -namespace Handlebars; - -interface Loader -{ - - /** - * Load a Template by name. - * - * @param string $name template name to load - * - * @return String - */ - public function load($name); - -} diff --git a/src/Handlebars/Loader/FilesystemLoader.php b/src/Handlebars/Loader/FilesystemLoader.php deleted file mode 100755 index 22adb83..0000000 --- a/src/Handlebars/Loader/FilesystemLoader.php +++ /dev/null @@ -1,144 +0,0 @@ - - * @author Behrooz Shabani - * @author Craig Bass - * @author ^^ - * @copyright 2012 (c) ParsPooyesh Co - * @copyright 2013 (c) Behrooz Shabani - * @license MIT - * @link http://voodoophp.org/docs/handlebars - */ - -namespace Handlebars\Loader; -use Handlebars\Loader; -use Handlebars\HandlebarsString; - - -class FilesystemLoader implements Loader -{ - private $_baseDir; - private $_extension = '.handlebars'; - private $_prefix = ''; - private $_templates = array(); - - /** - * Handlebars filesystem Loader constructor. - * - * $options array allows overriding certain Loader options during instantiation: - * - * $options = array( - * // extension used for Handlebars templates. Defaults to '.handlebars' - * 'extension' => '.other', - * ); - * - * @param string|array $baseDirs A path contain template files or array of paths - * @param array $options Array of Loader options (default: array()) - * - * @throws \RuntimeException if $baseDir does not exist. - */ - public function __construct($baseDirs, Array $options = []) - { - if (is_string($baseDirs)) { - $baseDirs = array(rtrim(realpath($baseDirs), '/')); - } else { - foreach ($baseDirs as &$dir) { - $dir = rtrim(realpath($dir), '/'); - } unset( $dir ); - } - - $this->_baseDir = $baseDirs; - - foreach ($this->_baseDir as $dir) { - if (!is_dir($dir)) { - throw new \RuntimeException( - 'FilesystemLoader baseDir must be a directory: ' . $dir - ); - } - } - - if (isset($options['extension'])) { - $this->_extension = '.' . ltrim($options['extension'], '.'); - } - - if (isset($options['prefix'])) { - $this->_prefix = $options['prefix']; - } - } - - /** - * Load a Template by name. - * - * $loader = new FilesystemLoader(dirname(__FILE__).'/views'); - * // loads "./views/admin/dashboard.handlebars"; - * $loader->load('admin/dashboard'); - * - * @param string $name template name - * - * @return HandlebarsString Handlebars Template source - */ - public function load($name) - { - if (!isset($this->_templates[$name])) { - $this->_templates[$name] = $this->loadFile($name); - } - - return new HandlebarsString($this->_templates[$name]); - } - - /** - * Helper function for loading a Handlebars file by name. - * - * @param string $name template name - * - * @throws \InvalidArgumentException if a template file is not found. - * @return string Handlebars Template source - */ - protected function loadFile($name) - { - $fileName = $this->getFileName($name); - - if ($fileName === false) { - throw new \InvalidArgumentException('Template ' . $name . ' not found.'); - } - - return file_get_contents($fileName); - } - - /** - * Helper function for getting a Handlebars template file name. - * - * @param string $name template name - * - * @return string Template file name - */ - protected function getFileName($name) - { - foreach ($this->_baseDir as $baseDir) { - $fileName = $baseDir . '/'; - $fileParts = explode('/', $name); - $file = array_pop($fileParts); - - if (substr($file, strlen($this->_prefix)) !== $this->_prefix) { - $file = $this->_prefix . $file; - } - - $fileParts[] = $file; - $fileName .= implode('/', $fileParts); - $lastCharacters = substr($fileName, 0 - strlen($this->_extension)); - - if ($lastCharacters !== $this->_extension) { - $fileName .= $this->_extension; - } - if (file_exists($fileName)) { - return $fileName; - } - } - - return false; - } - -} diff --git a/src/Handlebars/Loader/StringLoader.php b/src/Handlebars/Loader/StringLoader.php deleted file mode 100755 index 70dc369..0000000 --- a/src/Handlebars/Loader/StringLoader.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @author Behrooz Shabani - * @author Mardix - * @copyright 2012 (c) ParsPooyesh Co - * @copyright 2013 (c) Behrooz Shabani - * @copyright 2013 (c) Mardix - * @license MIT - * @link http://voodoophp.org/docs/handlebars - */ - -namespace Handlebars\Loader; -use Handlebars\Loader; -use Handlebars\HandlebarsString; - -class StringLoader implements Loader -{ - - /** - * Load a Template by source. - * - * @param string $name Handlebars Template source - * - * @return HandlebarsString Handlebars Template source - */ - public function load($name) - { - return new HandlebarsString($name); - } - -} diff --git a/tests/Handlebars/HandlebarsTest.php b/tests/Handlebars/HandlebarsTest.php index 559e86a..05dca37 100644 --- a/tests/Handlebars/HandlebarsTest.php +++ b/tests/Handlebars/HandlebarsTest.php @@ -9,8 +9,7 @@ class HandlebarsTest extends PHPUnit\Framework\TestCase */ public function testBasicTags(string $src, array $data, string $result): void { - $loader = new \Handlebars\Loader\StringLoader(); - $engine = new \Handlebars\Handlebars(array('loader' => $loader)); + $engine = new \Handlebars\Handlebars(); $this->assertEquals($result, $engine->render($src, $data)); } @@ -46,9 +45,8 @@ public function simpleTagdataProvider(): array */ public function testSimpleHelpers(string $src, array $data, string $result): void { - $loader = new \Handlebars\Loader\StringLoader(); $helpers = new \Handlebars\Helpers(); - $engine = new \Handlebars\Handlebars(array('loader' => $loader, 'helpers' => $helpers)); + $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $this->assertEquals($result, $engine->render($src, $data)); } @@ -187,10 +185,8 @@ public function internalHelpersdataProvider(): array */ public function testDataVariables(string $src, array $data, string $result, bool $enableDataVariables): void { - $loader = new \Handlebars\Loader\StringLoader(); $helpers = new \Handlebars\Helpers(); $engine = new \Handlebars\Handlebars(array( - 'loader' => $loader, 'helpers' => $helpers, 'enableDataVariables'=> $enableDataVariables, )); @@ -207,7 +203,6 @@ public function testDataVariables1() $object->{'@unknown'} = 'zucchini'; $data = ['data' => [$object]]; $engine = new \Handlebars\Handlebars(array( - 'loader' => new \Handlebars\Loader\StringLoader(), 'helpers' => new \Handlebars\Helpers(), 'enableDataVariables'=> false, )); @@ -376,8 +371,7 @@ public function testHelpersManagement() */ public function testCustomHelper() { - $loader = new \Handlebars\Loader\StringLoader(); - $engine = new \Handlebars\Handlebars(array('loader' => $loader)); + $engine = new \Handlebars\Handlebars(); $engine->addHelper('test', function () { return 'Test helper is called'; });