/** * Adds a JS file to be loaded upon clientside rendering. Leave the localpath empty for remote files * @param \SimpleXMLElement The XML root * @param string The (full)path to the local JS file * @param string The (full)path to the remote file * @param int The \System\Web\BasePage\Page\InclusionLocation for inclusion location */ public function addJSFile(\SimpleXMLElement $xml, $localFile, $remoteFile, $location = \System\Web\BasePage\Page\InclusionLocation::LOCATION_HEAD) { if (!isset($xml->jsfiles)) { $xml->addChild('jsfiles'); } $jsFiles = $xml->jsfiles; $jsFile = $jsFiles->addChild('jsfile'); if ($localFile) { $file = new \System\IO\File($localFile); if (MINIFY_ENABLE && mb_strpos($file->getFilename(), self::JS_MIN_EXTENSION) === false && mb_strpos($file->getFilename(), self::JS_MIN_PACKED) === false) { $localMinFile = $file->getPath() . basename($file->getFilename(), self::JS_EXTENSION) . self::JS_MIN_EXTENSION; if (!file_exists($localMinFile)) { $file = \System\IO\File::writeContents($localMinFile, \System\Web\Minify\JS\Minify::minify($file->getContents())); } else { $file = new \System\IO\File($localMinFile); } $remoteFile = str_ireplace(self::JS_EXTENSION, self::JS_MIN_EXTENSION, $remoteFile); } $jsFile->addChild('filesize', $file->getFileSizeInBytes()); } $jsFile->addChild('name', $remoteFile); $jsFile->addChild('location', $location); }
/** * Loads the XML file from disk and parses it to an XML structure. This structure is then returned. * This function is used internally to parse the given DynamicBaseObj XML descriptors and should not * be called directly, as it serves no further purpose. * @param string The XML file to load * @return \SimpleXMLElement The XML tree */ public static final function parseXml($xmlFile) { try { if (!($xml = \simplexml_load_file($xmlFile))) { throw new \Exception('XML file does not exist, or could not be loaded: ' . $xmlFile); } else { /* check for xml inheritance. we currently support adding of parent fields, conditions and virtuals only we apply this recursively, to allow inheritance trees */ if (isset($xml['extends'])) { $fileName = (string) $xml['extends']; $currentFilePath = new \System\IO\File($xmlFile); $parentFile = $currentFilePath->getPath() . $fileName; $parentXml = self::parseXml($parentFile); //we only add some field types in the parent xml to our current loaded xml foreach ($parentXml->children() as $item) { switch ($item->getName()) { case 'fields': foreach ($item->children() as $field) { $result = $xml->xpath('fields/' . $field->getName()); if (is_array($result) && count($result) == 0) { if (!isset($xml->fields)) { $xml->addChild('fields'); } \System\XML\XML::appendToXML($xml->fields, $field); } } break; case 'virtuals': foreach ($item->children() as $virtual) { $result = $xml->xpath('virtuals/' . $virtual->getName()); if (is_array($result) && count($result) == 0) { if (!isset($xml->virtuals)) { $xml->addChild('virtuals'); } \System\XML\XML::appendToXML($xml->virtuals, $virtual); } } break; case 'conditions': foreach ($item->children() as $condition) { //conditions follow a different structure and work on the name attribute $result = $xml->xpath('conditions/condition[@name="' . $condition['name'] . '"]'); if (is_array($result) && count($result) == 0) { if (!isset($xml->conditions)) { $xml->addChild('conditions'); } \System\XML\XML::appendToXML($xml->conditions, $condition); } } break; default: //we ignore other items } } } return $xml; } } catch (Exception $e) { throw new \System\Error\Exception\ObjectLoaderSourceException($e->getMessage()); } }