<?php
// Own namespace
-namespace CoreFramework\Template\Engine;
+namespace Org\Mxchange\CoreFramework\Template\Engine;
// Import framework stuff
-use CoreFramework\Factory\ObjectFactory;
-use CoreFramework\Generic\EmptyVariableException;
-use CoreFramework\Manager\ManageableApplication;
-use CoreFramework\Object\BaseFrameworkSystem;
-use CoreFramework\Response\Responseable;
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
+use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
+use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Response\Responseable;
+
+// Import SPL stuff
+use \InvalidArgumentException;
+use \SplFileInfo;
/**
* A generic template engine
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class BaseTemplateEngine extends BaseFrameworkSystem {
+abstract class BaseTemplateEngine extends BaseFrameworkSystem {
/**
* The local path name where all templates and sub folders for special
* templates are stored. We will internally determine the language plus
private $compiledData = '';
/**
- * The last loaded template's FQFN for debugging the engine
+ * The last loaded template's file instance (SplFileInfo)
*/
- private $lastTemplate = '';
+ private $lastTemplate = NULL;
/**
* The variable stack for the templates
/**
* Language support is enabled by default
*/
- private $languageSupport = TRUE;
+ private $languageSupport = true;
/**
* XML compacting is disabled by default
*/
- private $xmlCompacting = FALSE;
+ private $xmlCompacting = false;
// Exception codes for the template engine
const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED = 0x110;
*
* @param $variableName The variable we are looking for
* @param $variableGroup Optional variable group to look in
- * @return $index FALSE means not found, >=0 means found on a specific index
+ * @return $index false means not found, >=0 means found on a specific index
*/
private function getVariableIndex ($variableName, $variableGroup = NULL) {
// Replace all dashes to underscores to match variables with configuration entries
$variableName = trim(self::convertDashesToUnderscores($variableName));
// First everything is not found
- $found = FALSE;
+ $found = false;
// If the stack is NULL, use the current group
if (is_null($variableGroup)) {
$found = $this->getVariableIndex($variableName, $variableGroup);
// Is the variable found?
- if ($found !== FALSE) {
+ if ($found !== false) {
// Read it
$content = $this->getVariableValue($variableGroup, $found);
} // END - if
* @param $add Whether add this group
* @return void
*/
- public function setVariableGroup ($groupName, $add = TRUE) {
+ public function setVariableGroup ($groupName, $add = true) {
// Set group name
//* DEBIG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(__METHOD__.': currGroup=' . $groupName);
$this->currGroup = $groupName;
// Skip group 'general'
- if (($groupName != 'general') && ($add === TRUE)) {
+ if (($groupName != 'general') && ($add === true)) {
$this->variableGroups[$groupName] = 'OK';
} // END - if
}
$index = $this->getVariableIndex($variableName);
// Is the variable set?
- if ($index === FALSE) {
+ if ($index === false) {
// Unset variables cannot be modified
throw new NoVariableException(array($this, $variableName, $value), self::EXCEPTION_VARIABLE_IS_MISSING);
} // END - if
$index = $this->getVariableIndex($variableName);
// Is the variable set?
- if ($index === FALSE) {
+ if ($index === false) {
// Is the stack there?
if (!isset($this->varStack[$variableGroup])) {
// Then initialize it here
}
/**
- * Setter for the last loaded template's FQFN
+ * Setter for the last loaded template's file instance
*
* @param $template The last loaded template
* @return void
*/
- private final function setLastTemplate ($template) {
- $this->lastTemplate = (string) $template;
+ private final function setLastTemplate (SplFileInfo $fileInstance) {
+ $this->lastTemplate = $fileInstance;
}
/**
- * Getter for the last loaded template's FQFN
+ * Getter for the last loaded template's file instance
*
* @return $template The last loaded template
*/
/**
* Private loader for all template types
*
- * @param $template The template we shall load
+ * @param $templateName The template we shall load
* @param $extOther An other extension to use
* @return void
* @throws FileNotFoundException If the template was not found
*/
- protected function loadTemplate ($template, $extOther = '') {
+ protected function loadTemplate ($templateName, $extOther = '') {
// Get extension for the template if empty
if (empty($extOther)) {
// None provided, so get the raw one
* now entirely done by php_intl. These old thing with language-based
* template paths comes from an older time.
*/
- $fqfn = sprintf('%s%s%s%s/%s%s',
- $this->getConfigInstance()->getConfigEntry('base_path'),
+ $fileInstance = new SplFileInfo(sprintf('%s%s%s%s%s%s',
$this->getTemplateBasePath(),
$this->getGenericBasePath(),
$this->getTemplateType(),
- (string) $template,
+ DIRECTORY_SEPARATOR,
+ (string) $templateName,
$ext
- );
+ ));
// First try this
try {
// Load the raw template data
- $this->loadRawTemplateData($fqfn);
+ $this->loadRawTemplateData($fileInstance);
} catch (FileNotFoundException $e) {
// If we shall load a code-template we need to switch the file extension
if (($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('html_template_type')) && (empty($extOther))) {
$ext = $this->getCodeTemplateExtension();
// Try it again...
- $this->loadTemplate($template, $ext);
+ $this->loadTemplate($templateName, $ext);
} else {
// Throw it again
- throw new FileNotFoundException($fqfn, self::EXCEPTION_FILE_NOT_FOUND);
+ throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_NOT_FOUND);
}
}
/**
* A private loader for raw template names
*
- * @param $fqfn The full-qualified file name for a template
+ * @param $fileInstance An instance of a SplFileInfo class
* @return void
*/
- private function loadRawTemplateData ($fqfn) {
+ private function loadRawTemplateData (SplFileInfo $fileInstance) {
// Some debug code to look on the file which is being loaded
- //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: FQFN=' . $fqfn);
+ //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: fileInstance=' . $fileInstance);
// Load the raw template
- $rawTemplateData = $this->getFileIoInstance()->loadFileContents($fqfn);
+ $rawTemplateData = $this->getFileIoInstance()->loadFileContents($fileInstance);
// Store the template's contents into this class
$this->setRawTemplateData($rawTemplateData);
- // Remember the template's FQFN
- $this->setLastTemplate($fqfn);
+ // Remember the template's file instance
+ $this->setLastTemplate($fileInstance);
}
/**
preg_match_all('/\$(\w+)(\[(\w+)\])?/', $rawData, $variableMatches);
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawData(' . strlen($rawData) . ')=' . $rawData . ',variableMatches=' . print_r($variableMatches, TRUE));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawData(' . strlen($rawData) . ')=' . $rawData . ',variableMatches=' . print_r($variableMatches, true));
// Did we find some variables?
if ((is_array($variableMatches)) && (count($variableMatches) == 4) && (count($variableMatches[0]) > 0)) {
//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:template=' . $template);
// Template not found, but maybe variable assigned?
- if ($this->getVariableIndex($template) !== FALSE) {
+ if ($this->getVariableIndex($template) !== false) {
// Use that content here
$this->loadedRawData[$template] = $this->readVariable($template);
$foundIndex = array_search($template, $templateMatches[1]);
// Lookup the matching template replacement
- if (($foundIndex !== FALSE) && (isset($templateMatches[0][$foundIndex]))) {
+ if (($foundIndex !== false) && (isset($templateMatches[0][$foundIndex]))) {
// Get the current raw template
$rawData = $this->getRawTemplateData();
$this->assignVariable($var, $varMatches[3][$key]);
} elseif (!empty($varMatches[2][$key])) {
// @TODO Non-string found so we need some deeper analysis...
- ApplicationEntryPoint::app_exit('Deeper analysis not yet implemented!');
+ ApplicationEntryPoint::exitApplication('Deeper analysis not yet implemented!');
}
} // END - foreach
}
$foundIndex = array_search($template, $templateMatches[1]);
// Lookup the matching variable data
- if (($foundIndex !== FALSE) && (isset($templateMatches[3][$foundIndex]))) {
+ if (($foundIndex !== false) && (isset($templateMatches[3][$foundIndex]))) {
// Split it up with another reg. exp. into variable=value pairs
preg_match_all($this->regExpVarValue, $templateMatches[3][$foundIndex], $varMatches);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varMatches=' . print_r($varMatches, TRUE));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varMatches=' . print_r($varMatches, true));
// Assign all variables
$this->assignAllVariables($varMatches);
* @param $variableName The variable we are looking for
* @param $value The value we want to store in the variable
* @return void
- * @throws EmptyVariableException If the variable name is left empty
+ * @throws InvalidArgumentException If the variable name is left empty
*/
public final function assignVariable ($variableName, $value) {
// Replace all dashes to underscores to match variables with configuration entries
// Empty variable found?
if (empty($variableName)) {
// Throw an exception
- throw new EmptyVariableException(array($this, 'variableName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+ throw new InvalidArgumentException('Parameter "variableName" is empty');
} // END - if
// First search for the variable if it was already added
$index = $this->getVariableIndex($variableName);
// Was it found?
- if ($index === FALSE) {
+ if ($index === false) {
// Add it to the stack
//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:ADD: ' . $variableName . '[' . gettype($value) . ']=' . $value);
$this->addVariable($variableName, $value);
$index = $this->getVariableIndex($variableName, $variableGroup);
// Was it found?
- if ($index !== FALSE) {
+ if ($index !== false) {
// Remove this variable
//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:UNSET: variableGroup=' . $variableGroup . ',variableName=' . $variableName . ',index=' . $index);
$this->unsetVariableStackOffset($index, $variableGroup);
*/
public function loadCodeTemplate ($template) {
// Set template type
- $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type'));
+ $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type'));
// Load the special template
$this->loadTemplate($template);
// Iterate through all general variables
foreach ($this->getVarStack('general') as $index => $currVariable) {
// Compile the value
- $value = $this->compileRawCode($this->readVariable($currVariable['name']), TRUE);
+ $value = $this->compileRawCode($this->readVariable($currVariable['name']), true);
// Debug message
//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: name=' . $currVariable['name'] . ',value=' . $value);
);
// This loop does remove the backslashes (\) in PHP parameters
- while (strpos($eval, $this->codeBegin) !== FALSE) {
+ while (strpos($eval, $this->codeBegin) !== false) {
// Get left part before "<?"
$evalLeft = substr($eval, 0, strpos($eval, $this->codeBegin));
// Goes something wrong?
if ((!isset($result)) || (empty($result))) {
// Output eval command
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Failed eval() code: <pre>%s</pre>', $this->markupCode($eval, TRUE)), TRUE);
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('Failed eval() code: <pre>%s</pre>', $this->markupCode($eval, true)), true);
// Output backtrace here
$this->debugBackTrace();
*/
public function compileTemplate () {
// Get code type to make things shorter
- $codeType = $this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type');
+ $codeType = $this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type');
// We will only work with template type "code" from configuration
if (substr($this->getTemplateType(), 0, strlen($codeType)) != $codeType) {
// Abort here
- throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_' . self::getResponseTypeFromSystem() . '_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED);
+ throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED);
} // END - if
// Get the raw data.
preg_match_all($this->regExpCodeTags, $rawData, $templateMatches);
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:templateMatches=' . print_r($templateMatches , TRUE));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:templateMatches=' . print_r($templateMatches , true));
// Analyze the matches array
if ((is_array($templateMatches)) && (count($templateMatches) == 4) && (count($templateMatches[0]) > 0)) {
* @param $setMatchAsCode Sets $match if readVariable() returns empty result
* @return $rawCode Compile code with inserted variable value
*/
- public function compileRawCode ($rawCode, $setMatchAsCode=FALSE) {
+ public function compileRawCode ($rawCode, $setMatchAsCode=false) {
// Find the variables
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:rawCode=<pre>' . htmlentities($rawCode) . '</pre>');
preg_match_all($this->regExpVarValue, $rawCode, $varMatches);
// Compile all variables
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:<pre>' . print_r($varMatches, TRUE) . '</pre>');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:<pre>' . print_r($varMatches, true) . '</pre>');
foreach ($varMatches[0] as $match) {
// Add variable tags around it
$varCode = '{?' . $match . '?}';
//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']:varCode=' . $varCode);
// Is the variable found in code? (safes some calls)
- if (strpos($rawCode, $varCode) !== FALSE) {
+ if (strpos($rawCode, $varCode) !== false) {
// Debug message
//* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-TEMPLATE[' . __METHOD__ . ':' . __LINE__ . ']: match=' . $match . ',rawCode[' . gettype($rawCode) . ']=' . $rawCode);
// Use $match as new value or $value from read variable?
- if ($setMatchAsCode === TRUE) {
+ if ($setMatchAsCode === true) {
// Insert match
$rawCode = str_replace($varCode, $match, $rawCode);
} else {
* @param $languageSupport New language support setting
* @return void
*/
- public final function enableLanguageSupport ($languageSupport = TRUE) {
+ public final function enableLanguageSupport ($languageSupport = true) {
$this->languageSupport = (bool) $languageSupport;
}
* @param $xmlCompacting New XML compacting setting
* @return void
*/
- public final function enableXmlCompacting ($xmlCompacting = TRUE) {
+ public final function enableXmlCompacting ($xmlCompacting = true) {
$this->xmlCompacting = (bool) $xmlCompacting;
}