From 0a26bb0f9847462642035ab3cef7b808991a21b5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 17 Feb 2023 20:39:57 +0100 Subject: [PATCH] Continued: - added more debug lines - added more checks on parameters --- .../classes/parser/xml/class_XmlParser.php | 2 +- .../xml/class_BaseXmlTemplateEngine.php | 89 ++++++++++++++++--- 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/framework/main/classes/parser/xml/class_XmlParser.php b/framework/main/classes/parser/xml/class_XmlParser.php index ce764424..bfbc88f5 100644 --- a/framework/main/classes/parser/xml/class_XmlParser.php +++ b/framework/main/classes/parser/xml/class_XmlParser.php @@ -104,7 +104,7 @@ class XmlParser extends BaseParser implements Parseable { if (!xml_parse($xmlParser, $content)) { // Error found in XML //* DEBUG: */ exit(__METHOD__ . ':
'.htmlentities($content).'
'); - throw new XmlParserException(array($this, $xmlParser), Parseable::EXCEPTION_XML_PARSER_ERROR); + throw new XmlParserException([$this, $xmlParser], Parseable::EXCEPTION_XML_PARSER_ERROR); } // Free the parser diff --git a/framework/main/classes/template/xml/class_BaseXmlTemplateEngine.php b/framework/main/classes/template/xml/class_BaseXmlTemplateEngine.php index f2be3e21..a7063ec4 100644 --- a/framework/main/classes/template/xml/class_BaseXmlTemplateEngine.php +++ b/framework/main/classes/template/xml/class_BaseXmlTemplateEngine.php @@ -109,6 +109,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi * @param $typePrefix Type prefix * @param $xmlTemplateType Type of XML template * @return $templateInstance An instance of TemplateEngine + * @throws InvalidArgumentException If a parameter has an invalid value * @throws BasePathIsEmptyException If the provided $templateBasePath is empty * @throws InvalidBasePathStringException If $templateBasePath is no string * @throws BasePathIsNoDirectoryException If $templateBasePath is no @@ -117,6 +118,16 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi * read-protected */ protected function initXmlTemplateEngine (string $typePrefix, string $xmlTemplateType) { + // Check on parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: typePrefix=%s,xmlTemplateType=%s - CALLED!', $typePrefix, $xmlTemplateType)); + if (empty($typePrefix)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "typePrefix" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (empty($xmlTemplateType)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "xmlTemplateType" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Set XML template type and prefix $this->xmlTemplateType = $xmlTemplateType; $this->typePrefix = $typePrefix; @@ -141,6 +152,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi } // Set the base path + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: templateBasePath=%s', $templateBasePath)); $this->setTemplateBasePath($templateBasePath); // Set template extensions @@ -160,13 +172,18 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi $this->stackerName = $typePrefix . '_' . $xmlTemplateType; // Init stacker + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: this->stackerName=%s', $this->stackerName)); $stackInstance->initStack($this->stackerName); // Set it $this->setStackInstance($stackInstance); // Set it in main nodes + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: Adding xmlTemplateType=%s to this->mainNodes ...', $xmlTemplateType)); array_push($this->mainNodes, str_replace('_', '-', $xmlTemplateType)); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-XML-TEMPLATE-ENGINE: CALLED!'); } /** @@ -177,6 +194,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi */ public function loadXmlTemplate (string $templateName = '') { // Is the template name empty? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: templateName=%s - CALLED!', $templateName)); if (empty($templateName)) { // Set generic template name $templateName = $this->typePrefix . '_' . $this->xmlTemplateType . '_template_type'; @@ -186,7 +204,11 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi $this->setTemplateType(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($templateName)); // Load the special template + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: Invoking this->loadTemplate(%s) ...', $this->xmlTemplateType)); $this->loadTemplate($this->xmlTemplateType); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-XML-TEMPLATE-ENGINE: CALLED!'); } /** @@ -254,7 +276,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi */ public function readXmlData (string $key) { // Is key parameter valid? - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: key=%s - CALLED!', $key)); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: key=%s - CALLED!', $key)); if (empty($key)) { // Throw exception throw new InvalidArgumentException('Parameter key is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); @@ -264,12 +286,14 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi $value = parent::readVariable($key, 'general'); // Is this null? + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: value[]=%s', gettype($value))); if (is_null($value)) { // Bah, needs fixing. $this->debugInstance(sprintf('[%s:%d]: key=%s returns NULL', __METHOD__, __LINE__, $key)); } // Return value + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: value=%s - EXIT!', $value)); return $value; } @@ -279,10 +303,18 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi * @param $node The XML node we should load a dependency template * @param $templateDependency A template to load to satisfy dependencies * @return void + * @throws InvalidArgumentException If a parameter has an invalid value */ protected function handleTemplateDependency (string $node, string $templateDependency) { - // Check that the XML node is not empty - assert(!empty($node)); + // Check parameter + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: node=%s,templateDependency=%s - CALLED!', $node, $templateDependency)); + if (empty($node)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "node" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (empty($templateDependency)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "templateDependency" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } // Is the template dependency set? if ((!empty($templateDependency)) && (!isset($this->dependencyContent[$node]))) { @@ -298,6 +330,9 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi // Save the parsed raw content in our dependency array $this->dependencyContent[$node] = $templateInstance->getRawTemplateData(); } + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-XML-TEMPLATE-ENGINE: CALLED!'); } /** @@ -307,9 +342,23 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi * @param $element The element we shall handle * @param $attributes All attributes * @return void + * @throws InvalidArgumentException If a parameter has an invalid value * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found */ public final function startElement ($resource, string $element, array $attributes) { + // Check parameters + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: resource[%s]=%s,element=%s,attributes()=%d - CALLED!', gettype($resource), $resource, $element, count($attributes))); + if (!is_resource($resource)) { + // Throw IAE + throw new InvalidArgumentException(sprintf('Parameter resource has unexpected type %s', gettype($resource)), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (empty($element)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "element" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (count($attributes) == 0) { + // Throw IAE + throw new InvalidArgumentException(sprintf('Parameter attributes()=%d is an empty array', count($attributes)), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Initial method name which will never be called... $methodName = $this->initMethodName; @@ -317,7 +366,6 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi $element = strtolower($element); // Is the element a main node? - //* DEBUG: */ echo "START: >".$element."<
\n"; if (in_array($element, $this->getMainNodes())) { // Okay, main node found! $methodName = 'start' . StringUtils::convertToClassName($element); @@ -333,7 +381,11 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi } // Call method - call_user_func_array(array($this, $methodName), $attributes); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: Invoking this->%s(attributes()=%d) ...', $methodName, count($attributes))); + call_user_func_array([$this, $methodName], $attributes); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-XML-TEMPLATE-ENGINE: CALLED!'); } /** @@ -345,22 +397,34 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi * @throws XmlNodeMismatchException If current main node mismatches the closing one */ public final function finishElement ($resource, string $nodeName) { + // Check parameters + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: resource[%s]=%s,nodeName=%s - CALLED!', gettype($resource), $resource, $nodeName)); + if (!is_resource($resource)) { + // Throw IAE + throw new InvalidArgumentException(sprintf('Parameter resource has unexpected type %s', gettype($resource)), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } elseif (empty($nodeName)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "nodeName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); + } + // Make all lower-case $nodeName = strtolower($nodeName); // Does this match with current main node? - //* DEBUG: */ echo "END: >".$nodeName."<
\n"; if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) { // Did not match! - throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH); + throw new XmlNodeMismatchException ([$this, $nodeName, $this->getCurrMainNode()], XmlParser::EXCEPTION_XML_NODE_MISMATCH); } // Construct method name $methodName = 'finish' . StringUtils::convertToClassName($nodeName); // Call the corresponding method - //* DEBUG: */ echo "call: ".$methodName."
\n"; - call_user_func_array(array($this, $methodName), array()); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: Invoking this->%s() ...', $methodName)); + call_user_func_array([$this, $methodName], []); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-XML-TEMPLATE-ENGINE: CALLED!'); } /** @@ -372,6 +436,7 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi */ public function renderXmlContent (string $content = NULL) { // Is the content set? + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: content[%s]()=%d - CALLED!', gettype($content), strlen($content))); if (is_null($content)) { // Get current content $content = $this->getRawTemplateData(); @@ -383,11 +448,15 @@ abstract class BaseXmlTemplateEngine extends BaseTemplateEngine implements Compi // Check if XML compacting is enabled if ($this->isXmlCompactingEnabled()) { // Yes, so get a decorator class for transparent compacting - $parserInstance = ObjectFactory::createObjectByConfiguredName('deco_compacting_xml_parser_class', array($parserInstance)); + $parserInstance = ObjectFactory::createObjectByConfiguredName('deco_compacting_xml_parser_class', [$parserInstance]); } // Parse the XML document + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-XML-TEMPLATE-ENGINE: Invoking parserInstance->parseXmlContent(content()=%d) ...', strlen($content))); $parserInstance->parseXmlContent($content); + + // Trace message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-XML-TEMPLATE-ENGINE: CALLED!'); } /** -- 2.39.2