[Pkg-owncloud-commits] [php-sabredav] 105/163: Refactored the sharing plugin to use the new propfind event.

David Prévot taffit at moszumanska.debian.org
Tue May 20 18:54:59 UTC 2014


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

taffit pushed a commit to annotated tag upstream/2.0.0_beta1
in repository php-sabredav.

commit bee78a00117b0571404b282d244670bc35095fd6
Author: Evert Pot <evert at rooftopsolutions.nl>
Date:   Sat May 3 01:39:16 2014 -0400

    Refactored the sharing plugin to use the new propfind event.
---
 lib/Sabre/CalDAV/SharingPlugin.php | 80 ++++++++++++++------------------------
 lib/Sabre/DAV/PropFind.php         | 22 +++++++++--
 lib/Sabre/DAV/Server.php           |  6 +--
 tests/Sabre/DAV/PropFindTest.php   |  6 +--
 4 files changed, 55 insertions(+), 59 deletions(-)

diff --git a/lib/Sabre/CalDAV/SharingPlugin.php b/lib/Sabre/CalDAV/SharingPlugin.php
index 55d8ada..3d28903 100644
--- a/lib/Sabre/CalDAV/SharingPlugin.php
+++ b/lib/Sabre/CalDAV/SharingPlugin.php
@@ -92,10 +92,10 @@ class SharingPlugin extends DAV\ServerPlugin {
             '{' . Plugin::NS_CALENDARSERVER . '}shared-url'
         );
 
-        $this->server->on('beforeGetProperties', [$this, 'beforeGetProperties']);
-        $this->server->on('afterGetProperties',  [$this, 'afterGetProperties']);
-        $this->server->on('propPatch',           [$this, 'propPatch'], 40);
-        $this->server->on('method:POST',         [$this, 'httpPost']);
+        $this->server->on('propFind',     [$this,'propFindEarly']);
+        $this->server->on('propFind',     [$this,'propFindLate'], 200);
+        $this->server->on('propPatch',    [$this, 'propPatch'], 40);
+        $this->server->on('method:POST',  [$this, 'httpPost']);
 
     }
 
@@ -105,44 +105,31 @@ class SharingPlugin extends DAV\ServerPlugin {
      *
      * This allows us to inject any properties early.
      *
-     * @param string $path
+     * @param DAV\PropFind $propFind
      * @param DAV\INode $node
-     * @param array $requestedProperties
-     * @param array $returnedProperties
      * @return void
      */
-    public function beforeGetProperties($path, DAV\INode $node, &$requestedProperties, &$returnedProperties) {
+    public function propFindEarly(DAV\PropFind $propFind, DAV\INode $node) {
 
         if ($node instanceof IShareableCalendar) {
-            if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}invite', $requestedProperties))!==false) {
-
-                unset($requestedProperties[$index]);
-                $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}invite'] =
-                    new Property\Invite(
-                        $node->getShares()
-                    );
 
-            }
+            $propFind->handle('{' . Plugin::NS_CALENDARSERVER . '}invite', function() use ($node) {
+                return new Property\Invite(
+                    $node->getShares()
+                );
+            });
 
         }
 
         if ($node instanceof ISharedCalendar) {
 
-            if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}shared-url', $requestedProperties))!==false) {
-
-                unset($requestedProperties[$index]);
-                $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}shared-url'] =
-                    new DAV\Property\Href(
-                        $node->getSharedUrl()
-                    );
-
-            }
-            // The 'invite' property is slightly different for the 'shared'
-            // instance of the calendar, as it also contains the owner
-            // information.
-            if (($index = array_search('{' . Plugin::NS_CALENDARSERVER . '}invite', $requestedProperties))!==false) {
+            $propFind->handle('{' . Plugin::NS_CALENDARSERVER . '}shared-url', function() use ($node) {
+                return new DAV\Property\Href(
+                    $node->getSharedUrl()
+                );
+            });
 
-                unset($requestedProperties[$index]);
+            $propFind->handle('{' . Plugin::NS_CALENDARSERVER . '}invite', function() use ($node) {
 
                 // Fetching owner information
                 $props = $this->server->getPropertiesForPath($node->getOwner(), array(
@@ -167,14 +154,12 @@ class SharingPlugin extends DAV\ServerPlugin {
 
                 }
 
-                $returnedProperties[200]['{' . Plugin::NS_CALENDARSERVER . '}invite'] =
-                    new Property\Invite(
-                        $node->getShares(),
-                        $ownerInfo
-                    );
-
-            }
+                return new Property\Invite(
+                    $node->getShares(),
+                    $ownerInfo
+                );
 
+            });
 
         }
 
@@ -185,26 +170,21 @@ class SharingPlugin extends DAV\ServerPlugin {
      * This allows us to inject the correct resourcetype for calendars that
      * have been shared.
      *
-     * @param string $path
-     * @param array $properties
+     * @param DAV\PropFind $propFind
      * @param DAV\INode $node
      * @return void
      */
-    public function afterGetProperties($path, &$properties, DAV\INode $node) {
+    public function propFindLate(DAV\PropFind $propFind, DAV\INode $node) {
 
         if ($node instanceof IShareableCalendar) {
-            if (isset($properties[200]['{DAV:}resourcetype'])) {
-                if (count($node->getShares())>0) {
-                    $properties[200]['{DAV:}resourcetype']->add(
-                        '{' . Plugin::NS_CALENDARSERVER . '}shared-owner'
-                    );
+            if ($rt = $propFind->get('{DAV:}resourcetype')) {
+                if (count($node->getShares()) > 0) {
+                    $rt->add('{' . Plugin::NS_CALENDARSERVER . '}shared-owner');
                 }
             }
-            $propName = '{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes';
-            if (array_key_exists($propName, $properties[404])) {
-                unset($properties[404][$propName]);
-                $properties[200][$propName] = new Property\AllowedSharingModes(true,false);
-            }
+            $propFind->handle('{' . Plugin::NS_CALENDARSERVER . '}allowed-sharing-modes', function() {
+                return new Property\AllowedSharingModes(true,false);
+            });
 
         }
 
diff --git a/lib/Sabre/DAV/PropFind.php b/lib/Sabre/DAV/PropFind.php
index 787ce55..6737302 100644
--- a/lib/Sabre/DAV/PropFind.php
+++ b/lib/Sabre/DAV/PropFind.php
@@ -109,17 +109,21 @@ class PropFind {
     }
 
     /**
-     * Sets the status code and optionally the value of a property.
+     * Sets the value of the property
      *
-     * If the property was never requested, it will be ignored.
+     * If status is not supplied, the status will default to 200 for non-null
+     * properties, and 404 for null properties.
      *
      * @param string $propertyName
      * @param int $status
      * @param mixed $value
      * @return void
      */
-    public function set($propertyName, $status, $value = null) {
+    public function set($propertyName, $value, $status = null) {
 
+        if (is_null($status)) {
+            $status = is_null($value) ? 404 : 200;
+        }
         if (isset($this->result[$propertyName])) {
             if ($status!==404 && $this->result[$propertyName][0]===404) {
                 $this->itemsLeft--;
@@ -132,6 +136,18 @@ class PropFind {
     }
 
     /**
+     * Returns the current value for a property.
+     *
+     * @param string $propertyName
+     * @return void
+     */
+    public function get($propertyName) {
+
+        return isset($this->result[$propertyName])?$this->result[$propertyName][1]:null;
+
+    }
+
+    /**
      * Updates the path for this PROPFIND.
      *
      * @param string $path
diff --git a/lib/Sabre/DAV/Server.php b/lib/Sabre/DAV/Server.php
index a4262df..306f2fe 100644
--- a/lib/Sabre/DAV/Server.php
+++ b/lib/Sabre/DAV/Server.php
@@ -1022,7 +1022,7 @@ class Server extends EventEmitter {
 
             foreach($propertyList as $propertyName=>$value) {
 
-                $propFind->set($propertyName, $status, $value);
+                $propFind->set($propertyName, $value, $status);
 
             }
 
@@ -1035,7 +1035,7 @@ class Server extends EventEmitter {
             $nodeProperties = $node->getProperties($propertyNames);
 
             foreach($nodeProperties as $propertyName=>$value) {
-                $propFind->set($propertyName, 200, $value);
+                $propFind->set($propertyName, $value, 200);
             }
 
         }
@@ -1051,7 +1051,7 @@ class Server extends EventEmitter {
 
             foreach($propertyList as $propertyName=>$value) {
 
-                $propFind->set($propertyName, $status, $value);
+                $propFind->set($propertyName, $value, $status);
 
             }
 
diff --git a/tests/Sabre/DAV/PropFindTest.php b/tests/Sabre/DAV/PropFindTest.php
index 9e05863..f2ef2d6 100644
--- a/tests/Sabre/DAV/PropFindTest.php
+++ b/tests/Sabre/DAV/PropFindTest.php
@@ -50,7 +50,7 @@ class PropFindTest extends \PHPUnit_Framework_TestCase {
     function testSet() {
 
         $propFind = new PropFind('foo', ['{DAV:}displayname']);
-        $propFind->set('{DAV:}displayname', 200, 'bar');
+        $propFind->set('{DAV:}displayname', 'bar');
 
         $this->assertEquals([
             200 => ['{DAV:}displayname' => 'bar'],
@@ -62,8 +62,8 @@ class PropFindTest extends \PHPUnit_Framework_TestCase {
     function testSetUnset() {
 
         $propFind = new PropFind('foo', ['{DAV:}displayname']);
-        $propFind->set('{DAV:}displayname', 200, 'bar');
-        $propFind->set('{DAV:}displayname', 404);
+        $propFind->set('{DAV:}displayname', 'bar');
+        $propFind->set('{DAV:}displayname', null);
 
         $this->assertEquals([
             200 => [],

-- 
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