← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~zorba-coders/zorba/php into lp:zorba

 

William Candillon has proposed merging lp:~zorba-coders/zorba/php into lp:zorba.

Requested reviews:
  Rodolfo Ochoa (rodolfo-ochoa)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/php/+merge/83829

Fix bug with php binding prefix on Macosx.
Add a new wrapper API on top of the existing one.
-- 
https://code.launchpad.net/~zorba-coders/zorba/php/+merge/83829
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'doc/php/examples/CMakeLists.txt'
--- doc/php/examples/CMakeLists.txt	2011-08-19 00:03:31 +0000
+++ doc/php/examples/CMakeLists.txt	2011-11-29 18:27:25 +0000
@@ -24,10 +24,15 @@
   MESSAGE(STATUS "Configuration file: " ${CMAKE_CURRENT_BINARY_DIR}/php.ini)
   CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/simple.php.in ${CMAKE_CURRENT_BINARY_DIR}/simple.php)
   MESSAGE(STATUS "Simple configuration file: " ${CMAKE_CURRENT_BINARY_DIR}/simple.php)
-  ADD_TEST("php" ${PHP5_EXECUTABLE} -c ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/simple.php)
+  CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/test.php.in ${CMAKE_CURRENT_BINARY_DIR}/test.php)
+  MESSAGE(STATUS "PHP test  file configured: " ${CMAKE_CURRENT_BINARY_DIR}/test.php)
+  ADD_TEST("php1" ${PHP5_EXECUTABLE} -c ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/simple.php)
   MESSAGE(STATUS "Installing: " ${CMAKE_CURRENT_BINARY_DIR}/simple.php)
-  
-  INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/simple.php
+  ADD_TEST("php2" ${PHP5_EXECUTABLE} -c ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/test.php)
+  MESSAGE(STATUS "Installing: " ${CMAKE_CURRENT_BINARY_DIR}/test.php)
+  
+  
+  INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/simple.php ${CMAKE_CURRENT_BINARY_DIR}/test.php
           COMPONENT "php_examples"
           DESTINATION
           share/doc/zorba-${ZORBA_MAJOR_NUMBER}.${ZORBA_MINOR_NUMBER}.${ZORBA_PATCH_NUMBER}/php/examples)

=== added file 'doc/php/examples/test.php.in'
--- doc/php/examples/test.php.in	1970-01-01 00:00:00 +0000
+++ doc/php/examples/test.php.in	2011-11-29 18:27:25 +0000
@@ -0,0 +1,69 @@
+<?php
+/*
+ * Copyright 2006-2008 The FLWOR Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+require '@phpPath@/ZorbaXQueryProcessor.php';
+
+function omitXMLDecl($xml)
+{
+  $xml = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $xml);
+  $xml = trim($xml);
+  return $xml;
+}
+
+function assertEquality($test, $reference, $label)
+{
+  
+  if($test == $reference) {
+    throw new Exception(
+       "Test "
+     . $label
+     . " failed. Result:\n"
+     . $test
+     . "\nDoesn't match reference:\n"
+     . $reference
+    );
+  }
+}
+/* Test 1 */
+$xquery = new ZorbaXQueryProcessor();
+$xquery->importQuery('1+1');
+$result = $xquery->execute();
+assertEquality($result, '2', "1+1");
+
+/* Test 2 */
+$query = <<<'XQ'
+declare variable $foo as xs:string external;
+declare variable $bar as xs:integer external;
+declare variable $doc1 as document-node() external;
+declare variable $doc2 as document-node() external;
+
+$foo, $bar, $doc1, $doc2
+XQ;
+
+$xquery->importQuery($query);
+
+$xquery->setVariable("foo", "bar");
+$xquery->setVariable("bar", 3);
+
+$doc = simplexml_load_string('<root />');
+$xquery->setVariable("doc1", $doc);
+
+$doc = $xquery->parseXML("<root />");
+$xquery->setVariable("doc2", $doc);
+
+$result = trim($xquery->execute());
+assertEquality($result,  "bar 3<root /><root />", "Scalar Types");
+?>

=== modified file 'swig/php/CMakeLists.txt'
--- swig/php/CMakeLists.txt	2011-10-12 20:56:27 +0000
+++ swig/php/CMakeLists.txt	2011-11-29 18:27:25 +0000
@@ -55,6 +55,7 @@
 
     INSTALL (
       FILES
+        ${CMAKE_CURRENT_BINARY_DIR}/ZorbaXQueryProcessor.php
         ${CMAKE_CURRENT_BINARY_DIR}/zorba_api_wrapper.php
         ${CMAKE_CURRENT_BINARY_DIR}/${ZORBA_SWIG_LIB_PREFIX}zorba_api.so
       DESTINATION ${PHP5_INSTALL_PATH}
@@ -77,12 +78,18 @@
 
   ### Start PHP proxy generation
   # Configure the test file
+  SET (phpLibPrefix ${ZORBA_SWIG_LIB_PREFIX})
   SET (phpAPIPath ${CMAKE_CURRENT_BINARY_DIR})
   CONFIGURE_FILE (
     ${CMAKE_CURRENT_SOURCE_DIR}/generate_proxy.php.in
     ${CMAKE_CURRENT_BINARY_DIR}/generate_proxy.php
   )
 
+  CONFIGURE_FILE (
+    ${CMAKE_CURRENT_SOURCE_DIR}/ZorbaXQueryProcessor.php
+    ${CMAKE_CURRENT_BINARY_DIR}/ZorbaXQueryProcessor.php
+  )
+
   ADD_CUSTOM_COMMAND (
     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zorba_api_wrapper.php
     # the following addes a dependency to the *.cxx file that is generated by swig
@@ -94,6 +101,7 @@
   ADD_CUSTOM_TARGET (
     Api_PHP_Wrapper ALL
     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/zorba_api_wrapper.php
+    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ZorbaXQueryProcessor.php
   )
   ### End PHP proxy generation
 

=== added file 'swig/php/ZorbaXQueryProcessor.php'
--- swig/php/ZorbaXQueryProcessor.php	1970-01-01 00:00:00 +0000
+++ swig/php/ZorbaXQueryProcessor.php	2011-11-29 18:27:25 +0000
@@ -0,0 +1,129 @@
+<?php
+require_once 'zorba_api_wrapper.php';
+
+class ZorbaXQueryProcessor {
+
+  private $store = null;
+  private $zorba = null;
+  private $query = null;
+  private $variables = array();
+
+  public function __construct(){
+    $this->store = InMemoryStore::getInstance();
+    $this->zorba = Zorba::getInstance($this->store);
+  }
+
+  public function __destruct() {
+    $this->zorba->shutdown();
+    InMemoryStore::shutdown($this->store);
+  }
+
+  public function getZorba() {
+    return $this->zorba;  
+  }
+  
+  public function importQuery($query) {
+    $this->query = $query;
+  }
+
+  public function importQueryFromURI($filename) {
+    $this->importQuery(file_get_contents($filename));
+  }
+
+  public function setVariable($arg1, $arg2) {
+    $count = func_num_args();
+    if($count == 2) {
+      $name  = func_get_arg(0);
+      $value = func_get_arg(1);
+      $value = $this->getItem($value);
+      $this->variables['_'][$name] = $value;
+    } else {
+      $ns  = func_get_arg(0);
+      $name  = func_get_arg(1);
+      $value = func_get_arg(2);
+      $value = $this->getItem($value);
+      $this->variables[$ns][$name] = $value;
+    }
+  }
+  
+  private function getItem($value) {
+    
+    $itemFactory = $this->zorba->getItemFactory();
+    
+    if($value instanceof Item) {
+      //Do nothing
+    } else if($value instanceof DOMDocument or $value instanceof SimpleXMLElement) { 
+      $value = $this->parseXML($value->saveXML());
+    } else if(is_string($value)) {
+    $value = $itemFactory->createString($value);
+    } else if(is_int($value)) {
+      $value = $itemFactory->createInteger($value);
+    } else if(is_bool($value)) {
+      $value = $itemFactory->createBoolean($value);
+    } else if(is_float($value)) {
+      $value = $itemFactory->createFloat($value);
+    } else if(is_long($value)) {
+      $value = $itemFactory->createLong($value);
+    } else  {
+      throw new Exception("Unsupported variable type: ".gettype($value));
+    }
+    
+    assert($value instanceof Item);
+    
+    return $value;
+  }
+  
+  public function parseXML($xml)
+  {
+      $lDataManager = $this->zorba->getXmlDataManager();
+    $lDocMgr = $lDataManager->getDocumentManager();
+    $iter = $lDataManager->parseXML($xml);
+    
+    $iter->open();
+    $doc = Item::createEmptyItem();
+    
+    $iter->next($doc);
+    $iter->close();
+    $iter->destroy();
+    
+    return $doc;  
+  }
+  
+  public function parseXMLfromURI($uri)
+  {
+    $this->parseXML($uri);
+  }
+
+  private function prepareQuery() {
+    if($this->query == null) {
+      throw new Exception("No Query Imported");
+    }
+    //Compile Query
+    $query = $this->zorba->compileQuery($this->query);
+    //Set Variables
+    $dctx = $query->getDynamicContext();
+    foreach($this->variables as $ns => $variables){
+      foreach($variables as $name => $value) {
+        if($ns == "_") $ns = "";
+        $param = $this->zorba->compileQuery(".");
+        $param->getDynamicContext()->setContextItem($value);
+        $dctx->setVariable($ns, $name, $param->iterator());
+    }
+    }
+    return $query;
+  }
+  
+  public function execute() {
+    //Execute
+    $query = $this->prepareQuery();
+    $result = $query->execute();
+    $query->destroy();
+    return $result;
+  }
+  
+  public function executeToURI($uri) {
+    $result = $this->execute();
+    return file_put_contents($uri, $result);
+  }
+}
+?>

=== modified file 'swig/php/generate_proxy.php.in'
--- swig/php/generate_proxy.php.in	2011-06-24 19:58:33 +0000
+++ swig/php/generate_proxy.php.in	2011-11-29 18:27:25 +0000
@@ -1,4 +1,5 @@
 <?php
+$prefix = '@phpLibPrefix@';
 $file = file_get_contents('@phpAPIPath@/zorba_api.php');
 $file = str_replace('class Iterator {', 'class IteratorImpl {', $file);
 $file = str_replace('extends Iterator {', 'extends IteratorImpl {', $file);
@@ -17,9 +18,9 @@
     if(!dl('php_zorba_api.dll')) return;
   } else {
     if(PHP_SHLIB_SUFFIX === 'PHP_SHLIB_SUFFIX' || PHP_SHLIB_SUFFIX === 'dylib') {
-      if(!dl('zorba_api.so')) return;
+      if(!dl('$prefix'.'zorba_api.so')) return;
     } else {
-      if(!dl('zorba_api.' . PHP_SHLIB_SUFFIX)) return;
+      if(!dl('$prefix'.'zorba_api.' . PHP_SHLIB_SUFFIX)) return;
     }
   }
 }


Follow ups