diff --git a/HTML/Template/IT.php b/HTML/Template/IT.php index 6a151fb..2a4ca70 100644 --- a/HTML/Template/IT.php +++ b/HTML/Template/IT.php @@ -390,9 +390,12 @@ class HTML_Template_IT var $_options = array( 'preserve_data' => false, 'use_preg' => true, - 'preserve_input'=> true + 'preserve_input'=> true, + 'check_placeholder_exists' => true, ); + public static $globalOptions = []; + /** * Builds some complex regular expressions and optinally sets the * file root directory. @@ -407,8 +410,11 @@ class HTML_Template_IT * @see setRoot() * @access public */ - function HTML_Template_IT($root = '', $options = null) + function __construct($root = '', $options = null) { + if (!empty(self::$globalOptions)) { + $this->setOptions(self::$globalOptions); + } if (!is_null($options)) { $this->setOptions($options); } @@ -427,6 +433,13 @@ function HTML_Template_IT($root = '', $options = null) $this->setRoot($root); } // end constructor + /** + * Dont break the php4 constructor + */ + function HTML_Template_IT($root = '', $options = null) + { + self::__construct($root, $options); + } /** * Sets the option for the template class @@ -482,6 +495,16 @@ function setOptions($options) return IT_OK; } + /** + * Define global options used for all new instances. + * options defined using constructor parameter will overwrite + * globalOptions + */ + public static function setGlobalOptions($options) + { + self::$globalOptions = $options; + } + /** * Print a certain block with all replacements done. * @@ -767,7 +790,7 @@ function setVariable($variable, $value = '') $this->setVariable($key, $value); } } else { - if ($this->checkPlaceholderExists($this->currentBlock, $variable)) { + if (!$this->_options['check_placeholder_exists'] || $this->checkPlaceholderExists($this->currentBlock, $variable)) { $this->variableCache[$variable] = $value; } } @@ -1065,7 +1088,7 @@ function findBlocks($string) */ function getFile($filename) { - if ($filename{0} == '/' && substr($this->fileRoot, -1) == '/') { + if ($filename[0] == '/' && substr($this->fileRoot, -1) == '/') { $filename = substr($filename, 1); } @@ -1088,9 +1111,13 @@ function getFile($filename) $content = fread($fh, $fsize); fclose($fh); - return preg_replace( - "##ime", - "\$this->getFile('\\1')", + $callback = function($matches) { + return $this->getFile($matches[1]); + }; + + return preg_replace_callback( + "##im", + $callback, $content ); } // end func getFile @@ -1183,4 +1210,3 @@ function errorMessage($value, $blockname = '') $errorMessages[$value] : $errorMessages[IT_ERROR]; } } // end class IntegratedTemplate -?> diff --git a/HTML/Template/ITX.php b/HTML/Template/ITX.php index cdb6dfa..eb9b455 100644 --- a/HTML/Template/ITX.php +++ b/HTML/Template/ITX.php @@ -136,7 +136,7 @@ class HTML_Template_ITX extends HTML_Template_IT * @access public * @see HTML_Template_IT() */ - function HTML_Template_ITX($root = '') + function __construct($root = '') { $this->checkblocknameRegExp = '@' . $this->blocknameRegExp . '@'; @@ -147,6 +147,14 @@ function HTML_Template_ITX($root = '') $this->HTML_Template_IT($root); } // end func constructor + /** + * Dont break the php4 constructor + */ + function HTML_Template_ITX($root = '') + { + self::__construct($root); + } + /** * Clears all datafields of the object and rebuild the internal blocklist * @@ -311,7 +319,7 @@ function addBlock($placeholder, $blockname, $template) } elseif (count($parents) > 1) { reset($parents); - while (list($k, $parent) = each($parents)) { + foreach ($parents as $k => $parent) { $msg .= "$parent, "; } $msg = substr($parent, -2); @@ -397,7 +405,7 @@ function placeholderExists($placeholder, $block = '') if (is_array($variables = $this->blockvariables[$block])) { // search the value in the list of blockvariables reset($variables); - while (list($k, $variable) = each($variables)) { + foreach ($variables as $k => $variable) { if ($k == $placeholder) { $found = $block; break; @@ -409,7 +417,7 @@ function placeholderExists($placeholder, $block = '') // search all blocks and return the name of the first block that // contains the placeholder reset($this->blockvariables); - while (list($blockname, $variables) = each($this->blockvariables)) { + foreach ($this->blockvariables as $blockname => $variables) { if (is_array($variables) && isset($variables[$placeholder])) { $found = $blockname; break; @@ -430,7 +438,7 @@ function placeholderExists($placeholder, $block = '') function performCallback() { reset($this->functions); - while (list($func_id, $function) = each($this->functions)) { + foreach ($this->functions as $func_id => $function) { if (isset($this->callback[$function['name']])) { if ($this->callback[$function['name']]['expandParameters']) { $callFunction = 'call_user_func_array'; @@ -696,7 +704,7 @@ function buildFunctionlist() while ($head != '' && $args2 = $this->getValue($head, ',')) { $arg2 = trim($args2); - $args[] = ('"' == $arg2{0} || "'" == $arg2{0}) ? + $args[] = ('"' == $arg2[0] || "'" == $arg2[0]) ? substr($arg2, 1, -1) : $arg2; if ($arg2 == $head) { @@ -782,7 +790,7 @@ function deleteFromBlockvariablelist($block, $variables) } reset($this->blockvariables[$block]); - while (list($varname, $val) = each($this->blockvariables[$block])) { + foreach ($this->blockvariables[$block] as $varname => $val) { if (isset($variables[$varname])) { unset($this->blockvariables[$block][$varname]); } @@ -840,10 +848,10 @@ function findPlaceholderBlocks($variable) { $parents = array(); reset($this->blocklist); - while (list($blockname, $content) = each($this->blocklist)) { + foreach ($this->blocklist as $blockname => $content) { reset($this->blockvariables[$blockname]); - while (list($varname, $val) = each($this->blockvariables[$blockname])) { + foreach ($this->blockvariables[$blockname] as $varname => $val) { if ($variable == $varname) { $parents[] = $blockname; } diff --git a/HTML/Template/IT_Error.php b/HTML/Template/IT_Error.php index a83213e..0dc477c 100644 --- a/HTML/Template/IT_Error.php +++ b/HTML/Template/IT_Error.php @@ -56,10 +56,18 @@ class IT_Error extends PEAR_Error * @param string $file file where the error occured * @param string $line linenumber where the error occured */ - function IT_Error($msg, $file = __FILE__, $line = __LINE__) + function __construct($msg, $file = __FILE__, $line = __LINE__) { $this->PEAR_Error(sprintf("%s [%s on line %d].", $msg, $file, $line)); } // end func IT_Error - + + /** + * Dont break the php4 constructor + */ + function IT_Error($msg, $file = __FILE__, $line = __LINE__) + { + self::__construct($msg, $file, $line); + } + } // end class IT_Error ?> diff --git a/README b/README index c9d2687..732c4d2 100644 --- a/README +++ b/README @@ -4,10 +4,11 @@ Please report all new issues via the PEAR bug tracker. If this package is marked as unmaintained and you have fixes, please submit your pull requests and start discussion on the pear-qa mailing list. -To test, run either -$ phpunit tests/ - or -$ pear run-tests -r +To test, run + +$ composer install +$ php vendor/bin/phpunit + To build, simply $ pear package diff --git a/composer.json b/composer.json index a9c63dd..8775b32 100644 --- a/composer.json +++ b/composer.json @@ -36,16 +36,21 @@ "./" ], "license": "Modified BSD license", - "name": "pear/html_template_it", + "name": "elma/html_template_it", "support": { "issues": "http://pear.php.net/bugs/search.php?cmd=display&package_name[]=HTML_Template_IT", - "source": "https://github.com/pear/HTML_Template_IT" + "source": "https://github.com/Elma/HTML_Template_IT" }, "type": "library", "require": { "pear/pear_exception": "*" }, "require-dev": { - "phpunit/phpunit": "*" + "phpunit/phpunit": "^9.4" + }, + "extra": { + "branch-alias": { + "dev-v1.4.0-rc": "1.4.x-dev" + } } -} \ No newline at end of file +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..c476c03 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,10 @@ + + + + + + ./tests + + + + diff --git a/tests/AllTests.php b/tests/AllTests.php index 60c53eb..88cdeb2 100644 --- a/tests/AllTests.php +++ b/tests/AllTests.php @@ -1,6 +1,6 @@ diff --git a/tests/ITTest.php b/tests/ITTest.php index 309e116..3c471ae 100644 --- a/tests/ITTest.php +++ b/tests/ITTest.php @@ -1,8 +1,9 @@ tpl = new HTML_Template_IT(dirname(__FILE__) . '/templates'); } - function tearDown() + function tearDown(): void { unset($this->tpl); } @@ -391,9 +392,9 @@ public function testShouldSetOptionsCorrectly() { } - public function testPlaceholderReplacementScope() { + public function testPlaceholderReplacementScope() { $result = $this->tpl->loadTemplateFile('placeholderreplacementscope.html', true, true); - + if (PEAR::isError($result)) { $this->fail('Error loading template file: ' . $result->getMessage()); } diff --git a/tests/ITXTest.php b/tests/ITXTest.php index 2521aee..e2f72de 100644 --- a/tests/ITXTest.php +++ b/tests/ITXTest.php @@ -1,6 +1,5 @@ tpl = new HTML_Template_ITX(dirname(__FILE__) . '/templates'); }