Forráskód Böngészése

added test php inside xsl

Piotr Labudda 8 éve
szülő
commit
939a0c64c9

+ 40 - 0
SE/se-lib/Lib.php

@@ -41,4 +41,44 @@ class Lib {
 		}
 	}
 
+	public static function loadXslFunction($funcName) {
+		return self::_loadXslFunction($funcName, $required = true);
+	}
+
+
+	public static function tryLoadXslFunction($funcName) {
+		return self::_loadXslFunction($funcName, $required = false);
+	}
+
+
+	/**
+	 * load function.
+	 *
+	 * @example testFetchWfs -> SE/se-lib/xsl-functions/testFetchWfs.php // function testFetchWfs() {}
+	 * @example test_fetchWfs -> SE/se-lib/xsl-functions/test/fetchWfs.php // function test_fetchWfs() {}
+	 */
+	public static function _loadXslFunction($funcName, $required = true) {
+		DBG::log("_loadXslFunction({$funcName})...");
+		if (function_exists($funcName)) return true;
+		$path = APP_PATH_LIB . '/xsl-functions/' . implode('/', explode('_', $funcName)) . '.php';
+		DBG::log("DBG _loadXslFunction: {$path}");
+		if (file_exists($path)) {
+			require_once $path;
+		} else {
+			$path = APP_PATH_LIB . "/xsl-functions/{$funcName}.php";
+			DBG::log("DBG _loadXslFunction: {$path}");
+			if (file_exists($path)) {
+				require_once $path;
+			}
+		}
+		if (function_exists($funcName)) {
+			return true;
+		} else {
+			if ($required) {
+				die("Cant load xsl function {$funcName}");
+			}
+			return false;
+		}
+	}
+
 }

+ 72 - 0
SE/se-lib/Route/Test/XslRecurse.php

@@ -0,0 +1,72 @@
+<?php
+
+Lib::loadClass('RouteBase');
+
+class Route_Test_XslRecurse extends RouteBase {
+
+	function defaultAction() {
+		UI::layout( [ $this, 'defaultView' ], [ 'showMenu' => false ] );
+	}
+	function defaultView() {
+		Lib::loadXslFunction('testFetchWfs');
+		Lib::loadXslFunction('testHtmlContent');
+		$xml = "
+			<root>
+			 <item typeName=\"default_db:TEST_PERMS\">
+			  <featureId>TEST_PERMS.1</featureId>
+			 </item>
+			 <item typeName=\"default_db__x3A__TEST_PERMS:TestPermAnt\">
+			  <featureId>TestPermAnt.1</featureId>
+			 </item>
+			</root>
+		";
+		$xsl = '<?xml version="1.0" encoding="UTF-8"?>';
+		$xsl .= '
+			<xsl:stylesheet version="1.0"
+			     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+			     xmlns:php="http://php.net/xsl">
+			<xsl:output method="html" encoding="utf-8" indent="yes"/>
+			 <xsl:template match="root">
+			  <html><body>
+			    <h2>Items</h2>
+			    <table class="table table-bordered">
+						<thead>
+							<tr>
+								<th>featureId</th>
+								<th>item</th>
+							</tr>
+						</thead>
+			    <xsl:for-each select="item">
+			      <tr>
+							<td><xsl:value-of select="featureId"/></td>
+							<td><pre>
+				        <xsl:copy-of select="php:function(\'testFetchWfs\',@typeName, string(featureId))/node()"/>
+				      </pre></td>
+						</tr>
+			    </xsl:for-each>
+			    </table>
+
+					<h3>Test function to get xml content from php function</h3>
+					<xsl:copy-of select="php:function(\'testHtmlContent\', \'test-arg-1-from-xsl\', \'test-arg-2-from-xsl\')/node()"/>
+
+			  </body></html>
+			 </xsl:template>
+			</xsl:stylesheet>
+		';
+		$xmldoc = new DOMDocument;
+		$xmldoc->loadXML($xml);
+		$xsldoc = new DOMDocument;
+		$xsldoc->loadXML($xsl);
+
+		$proc = new XSLTProcessor();
+		$proc->registerPHPFunctions();
+		$proc->importStyleSheet($xsldoc);
+		$transformed = $proc->transformToXML($xmldoc);
+		DBG::nicePrint(htmlspecialchars($xml), 'xml');
+		DBG::nicePrint(htmlspecialchars($transformed), 'xml transformed');
+		echo "\n".'<hr>';
+		echo $transformed;
+		echo '<hr>'."\n";
+	}
+
+}

+ 11 - 0
SE/se-lib/xsl-functions/testFetchWfs.php

@@ -0,0 +1,11 @@
+<?php
+
+function testFetchWfs($typeName, $featureId) {
+	DBG::log([$typeName, $featureId], 'array', "TODO: testFetchWfs");
+	$dom = new DOMDocument();
+	$tmp = $dom->createDocumentFragment();
+	$tmp->appendXML(' <b> foo <i val="123"> bar</i> </b> TEST');
+
+	return $tmp; // TODO: require <xsl:copy-of select="php:function(...)/node() - @see https://en.wikibooks.org/wiki/PHP_Programming/XSL/registerPHPFunctions#XSL_receiving_external_fragments
+	// return $dom->saveXML($tmp);
+}

+ 10 - 0
SE/se-lib/xsl-functions/testHtmlContent.php

@@ -0,0 +1,10 @@
+<?php
+
+function testHtmlContent($arg1, $arg2) {
+	DBG::log([$arg1, $arg2], 'array', "testHtmlContent");
+	$dom = new DOMDocument();
+	$tmp = $dom->createDocumentFragment();
+	$tmp->appendXML(" <b> args:</b>: <ul><li>arg1: {$arg1}</li><li>arg2: {$arg2}</li></ul> TEST");
+
+	return $tmp;
+}