[Pkg-owncloud-commits] [php-sabredav] 20/34: Moved Sabre\DAV\Client to new xml system.

David Prévot taffit at moszumanska.debian.org
Wed May 27 13:57:10 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to tag 3.0.0-beta1
in repository php-sabredav.

commit abb1e3d2ea7c2217c63d1dc06c4f993abacb3692
Author: Evert Pot <me at evertpot.com>
Date:   Mon May 25 18:08:30 2015 -0400

    Moved Sabre\DAV\Client to new xml system.
    
    sabre/xml wins again with much less and more legible code ;)
    
    Fixes #665.
---
 CHANGELOG.md                                  |  2 +
 lib/DAV/Client.php                            | 82 ++++++++-------------------
 lib/DAV/Xml/Request/PropPatch.php             | 42 ++++++++++++--
 tests/Sabre/DAV/Xml/Request/PropPatchTest.php | 47 +++++++++++++++
 4 files changed, 110 insertions(+), 63 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e7f455c..d0cf1b0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@ ChangeLog
 * Using php-cs-fixer for automated coding standards enforcement and fixing.
 * #660: principals could break html output.
 * #662: Fixed several bugs in the `share` request parser.
+* #665: Fix a bug in serialization of complex properties in the proppatch
+  request in the client.
 
 
 3.0.0-alpha1 (2015-05-19)
diff --git a/lib/DAV/Client.php b/lib/DAV/Client.php
index e4473f4..44b7e7e 100644
--- a/lib/DAV/Client.php
+++ b/lib/DAV/Client.php
@@ -19,15 +19,21 @@ use Sabre\HTTP;
 class Client extends HTTP\Client {
 
     /**
-     * The propertyMap is a key-value array.
+     * The xml service.
      *
-     * If you use the propertyMap, any {DAV:}multistatus responses with the
-     * properties listed in this array, will automatically be mapped to a
-     * respective class.
+     * Uset this service to configure the property and namespace maps.
      *
-     * The {DAV:}resourcetype property is automatically added. This maps to
-     * Sabre\DAV\Property\ResourceType
+     * @var mixed
+     */
+    public $xml;
+
+    /**
+     * The elementMap
+     *
+     * This property is linked via reference to $this->xml->elementMap.
+     * It's deprecated as of version 3.0.0, and should no longer be used.
      *
+     * @deprecated
      * @var array
      */
     public $propertyMap = [];
@@ -144,7 +150,9 @@ class Client extends HTTP\Client {
             $this->addCurlSetting(CURLOPT_ENCODING, implode(',', $encodings));
         }
 
-        $this->propertyMap['{DAV:}resourcetype'] = 'Sabre\\DAV\\Property\\ResourceType';
+        $this->xml = new Xml\Service();
+        // BC
+        $this->propertyMap = & $this->xml->elementMap;
 
     }
 
@@ -241,60 +249,17 @@ class Client extends HTTP\Client {
      */
     function propPatch($url, array $properties) {
 
-        $dom = new \DOMDocument('1.0', 'UTF-8');
-        $dom->formatOutput = true;
-        $root = $dom->createElementNS('DAV:', 'd:propertyupdate');
-
-        foreach ($properties as $propName => $propValue) {
-
-            list(
-                $namespace,
-                $elementName
-            ) = \Sabre\Xml\Service::parseClarkNotation($propName);
-
-            if ($propValue === null) {
-
-                $remove = $dom->createElement('d:remove');
-                $prop = $dom->createElement('d:prop');
-
-                if ($namespace === 'DAV:') {
-                    $element = $dom->createElement('d:' . $elementName);
-                } else {
-                    $element = $dom->createElementNS($namespace, 'x:' . $elementName);
-                }
-
-                $root->appendChild($remove)->appendChild($prop)->appendChild($element);
-
-            } else {
-
-                $set = $dom->createElement('d:set');
-                $prop = $dom->createElement('d:prop');
-
-                if ($namespace === 'DAV:') {
-                    $element = $dom->createElement('d:' . $elementName);
-                } else {
-                    $element = $dom->createElementNS($namespace, 'x:' . $elementName);
-                }
-
-                if ($propValue instanceof Property) {
-                    $propValue->serialize(new Server(), $element);
-                } else {
-                    $element->nodeValue = htmlspecialchars($propValue, ENT_NOQUOTES, 'UTF-8');
-                }
-
-                $root->appendChild($set)->appendChild($prop)->appendChild($element);
-
-            }
-
-        }
-
-        $dom->appendChild($root);
-        $body = $dom->saveXML();
+        $propPatch = new Xml\Request\PropPatch();
+        $propPatch->properties = $properties;
+        $xml = $this->xml->write(
+            '{DAV:}propertyupdate',
+            $propPatch
+        );
 
         $url = $this->getAbsoluteUrl($url);
         $request = new HTTP\Request('PROPPATCH', $url, [
             'Content-Type' => 'application/xml',
-        ], $body);
+        ], $xml);
         $this->send($request);
     }
 
@@ -420,8 +385,7 @@ class Client extends HTTP\Client {
      */
     function parseMultiStatus($body) {
 
-        $xmlUtil = new Xml\Service();
-        $multistatus = $xmlUtil->expect('{DAV:}multistatus', $body);
+        $multistatus = $this->xml->expect('{DAV:}multistatus', $body);
 
         $result = [];
 
diff --git a/lib/DAV/Xml/Request/PropPatch.php b/lib/DAV/Xml/Request/PropPatch.php
index 72f296c..848e531 100644
--- a/lib/DAV/Xml/Request/PropPatch.php
+++ b/lib/DAV/Xml/Request/PropPatch.php
@@ -2,8 +2,9 @@
 
 namespace Sabre\DAV\Xml\Request;
 
-use Sabre\Xml\XmlDeserializable;
+use Sabre\Xml\Element;
 use Sabre\Xml\Reader;
+use Sabre\Xml\Writer;
 
 /**
  * WebDAV PROPPATCH request parser.
@@ -16,7 +17,7 @@ use Sabre\Xml\Reader;
  * @author Evert Pot (http://www.rooftopsolutions.nl/)
  * @license http://sabre.io/license/ Modified BSD License
  */
-class PropPatch implements XmlDeserializable {
+class PropPatch implements Element {
 
     /**
      * The list of properties that will be updated and removed.
@@ -28,6 +29,39 @@ class PropPatch implements XmlDeserializable {
     public $properties = [];
 
     /**
+     * The xmlSerialize metod is called during xml writing.
+     *
+     * Use the $writer argument to write its own xml serialization.
+     *
+     * An important note: do _not_ create a parent element. Any element
+     * implementing XmlSerializble should only ever write what's considered
+     * its 'inner xml'.
+     *
+     * The parent of the current element is responsible for writing a
+     * containing element.
+     *
+     * This allows serializers to be re-used for different element names.
+     *
+     * If you are opening new elements, you must also close them again.
+     *
+     * @param Writer $writer
+     * @return void
+     */
+    function xmlSerialize(Writer $writer) {
+
+        foreach ($this->properties as $propertyName => $propertyValue) {
+
+            if (is_null($propertyValue)) {
+                $writer->write(['{DAV:}remove' => [$propertyName => $propertyValue]]);
+            } else {
+                $writer->write(['{DAV:}set' => [$propertyName => $propertyValue]]);
+            }
+
+        }
+
+    }
+
+    /**
      * The deserialize method is called during xml parsing.
      *
      * This method is called statictly, this is because in theory this method
@@ -36,8 +70,8 @@ class PropPatch implements XmlDeserializable {
      * Often you want to return an instance of the current class, but you are
      * free to return other data as well.
      *
-     * Important note 2: You are responsible for advancing the reader to the
-     * next element. Not doing anything will result in a never-ending loop.
+     * You are responsible for advancing the reader to the next element. Not
+     * doing anything will result in a never-ending loop.
      *
      * If you just want to skip parsing for this element altogether, you can
      * just call $reader->next();
diff --git a/tests/Sabre/DAV/Xml/Request/PropPatchTest.php b/tests/Sabre/DAV/Xml/Request/PropPatchTest.php
new file mode 100644
index 0000000..486d4bb
--- /dev/null
+++ b/tests/Sabre/DAV/Xml/Request/PropPatchTest.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Sabre\DAV\Xml\Request;
+
+use Sabre\DAV\Xml\XmlTest;
+use Sabre\DAV\Xml\Property\Href;
+
+class PropPatchTest extends XmlTest {
+
+    function testSerialize() {
+
+        $propPatch = new PropPatch();
+        $propPatch->properties = [
+            '{DAV:}displayname' => 'Hello!',
+            '{DAV:}delete-me'   => null,
+            '{DAV:}some-url'    => new Href('foo/bar')
+        ];
+
+        $result = $this->write(
+            ['{DAV:}propertyupdate' => $propPatch]
+        );
+
+        $expected = <<<XML
+<?xml version="1.0"?>
+<d:propertyupdate xmlns:d="DAV:">
+    <d:set>
+        <d:displayname>Hello!</d:displayname>
+    </d:set>
+    <d:remove>
+        <d:delete-me />
+    </d:remove>
+    <d:set>
+        <d:some-url>
+            <d:href>/foo/bar</d:href>
+        </d:some-url>
+    </d:set>
+</d:propertyupdate>
+XML;
+
+        $this->assertXmlStringEqualsXmlString(
+            $expected,
+            $result
+        );
+
+    }
+
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list