[Pkg-owncloud-commits] [php-sabredav] 138/163: Arbitrary property storage plugin.

David Prévot taffit at moszumanska.debian.org
Tue May 20 18:55:02 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 2192b777b8ceb1a68dd5c6659af7b62317ded118
Author: Evert Pot <me at evertpot.com>
Date:   Tue May 13 18:11:52 2014 -0400

    Arbitrary property storage plugin.
---
 .gitignore                                         |   7 ++
 examples/sql/mysql.propertystorage.sql             |   7 ++
 examples/sql/sqlite.propertystorage.sql            |   9 ++
 .../PropertyStorage/Backend/BackendInterface.php   |  48 +++++++++
 lib/DAV/PropertyStorage/Backend/PDO.php            | 107 +++++++++++++++++++++
 lib/DAV/PropertyStorage/Plugin.php                 |  90 +++++++++++++++++
 6 files changed, 268 insertions(+)

diff --git a/.gitignore b/.gitignore
index 65b2327..e9c4ca5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,6 +21,10 @@ bin/phpunit
 # Assuming every .php file in the root is for testing
 /*.php
 
+# Other testing stuff
+/tmpdata
+/data
+/public
 
 # Build
 build
@@ -29,3 +33,6 @@ build.properties
 # Docs
 docs/api
 docs/wikidocs
+
+# Mac
+.DS_Store
diff --git a/examples/sql/mysql.propertystorage.sql b/examples/sql/mysql.propertystorage.sql
new file mode 100644
index 0000000..1b069df
--- /dev/null
+++ b/examples/sql/mysql.propertystorage.sql
@@ -0,0 +1,7 @@
+CREATE TABLE propertystorage (
+    id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
+    path VARCHAR(1024) NOT NULL,
+    name VARCHAR(100) NOT NULL,
+    value MEDIUMTEXT,
+);
+CREATE UNIQUE INDEX path_property ON propertystorage (path, name);
diff --git a/examples/sql/sqlite.propertystorage.sql b/examples/sql/sqlite.propertystorage.sql
new file mode 100644
index 0000000..1af7f19
--- /dev/null
+++ b/examples/sql/sqlite.propertystorage.sql
@@ -0,0 +1,9 @@
+CREATE TABLE propertystorage (
+    id integer primary key asc,
+    path TEXT,
+    name TEXT,
+    value TEXT
+);
+
+
+CREATE UNIQUE INDEX path_property ON propertystorage (path, name);
diff --git a/lib/DAV/PropertyStorage/Backend/BackendInterface.php b/lib/DAV/PropertyStorage/Backend/BackendInterface.php
new file mode 100644
index 0000000..c2e03d5
--- /dev/null
+++ b/lib/DAV/PropertyStorage/Backend/BackendInterface.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Sabre\DAV\PropertyStorage\Backend;
+
+use Sabre\DAV\PropFind;
+use Sabre\DAV\PropPatch;
+
+interface BackendInterface {
+
+    /**
+     * Fetches properties for a path.
+     *
+     * This method received a PropFind object, which contains all the
+     * information about the properties that need to be fetched.
+     *
+     * Ususually you would just want to call 'get404Properties' on this object,
+     * as this will give you the _exact_ list of properties that need to be
+     * fetched, and haven't yet.
+     *
+     * @param string $path
+     * @param PropFind $propFind
+     * @return void
+     */
+    public function propFind($path, PropFind $propFind);
+
+    /**
+     * Updates properties for a path
+     *
+     * This method received a PropPatch object, which contains all the
+     * information about the update.
+     *
+     * Usually you would want to call 'handleRemaining' on this object, to get;
+     * a list of all properties that need to be stored.
+     *
+     * @param string $path
+     * @param PropPatch $propPatch
+     * @return void
+     */
+    public function propPatch($path, PropPatch $propPatch);
+
+    /**
+     * This method is called after a node is deleted.
+     *
+     * This allows a backend to clean up all associated properties.
+     */
+    public function delete($path);
+
+}
diff --git a/lib/DAV/PropertyStorage/Backend/PDO.php b/lib/DAV/PropertyStorage/Backend/PDO.php
new file mode 100644
index 0000000..08ab0d6
--- /dev/null
+++ b/lib/DAV/PropertyStorage/Backend/PDO.php
@@ -0,0 +1,107 @@
+<?php
+
+namespace Sabre\DAV\PropertyStorage\Backend;
+
+use Sabre\DAV\PropFind;
+use Sabre\DAV\PropPatch;
+
+class PDO implements BackendInterface {
+
+    /**
+     * PDO
+     *
+     * @var \PDO
+     */
+    protected $pdo;
+
+    /**
+     * Creates the PDO property storage engine
+     *
+     * @param \PDO $pdo
+     */
+    public function __construct(\PDO $pdo) {
+
+        $this->pdo = $pdo;
+
+    }
+
+    /**
+     * Fetches properties for a path.
+     *
+     * This method received a PropFind object, which contains all the
+     * information about the properties that need to be fetched.
+     *
+     * Ususually you would just want to call 'get404Properties' on this object,
+     * as this will give you the _exact_ list of properties that need to be
+     * fetched, and haven't yet.
+     *
+     * @param string $path
+     * @param PropFind $propFind
+     * @return void
+     */
+    public function propFind($path, PropFind $propFind) {
+
+        $propertyNames = $propFind->get404Properties();
+        if (!$propertyNames) {
+            return;
+        }
+
+        $query = 'SELECT name, value FROM propertystorage WHERE path = ?';
+        $stmt = $this->pdo->prepare($query);
+        $stmt->execute([$path]);
+
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+            $propFind->set($row['name'], $row['value']);
+        }
+
+    }
+
+    /**
+     * Updates properties for a path
+     *
+     * This method received a PropPatch object, which contains all the
+     * information about the update.
+     *
+     * Usually you would want to call 'handleRemaining' on this object, to get;
+     * a list of all properties that need to be stored.
+     *
+     * @param string $path
+     * @param PropPatch $propPatch
+     * @return void
+     */
+    public function propPatch($path, PropPatch $propPatch) {
+
+        $propPatch->handleRemaining(function($properties) use ($path) {
+
+            $updateStmt = $this->pdo->prepare("REPLACE INTO propertystorage (path, name, value) VALUES (?, ?, ?)");
+            $deleteStmt = $this->pdo->prepare("DELETE FROM propertystorage WHERE path = ? AND name = ?");
+
+            foreach($properties as $name=>$value) {
+
+                if (!is_null($value)) {
+                    $updateStmt->execute([$path, $name, $value]);
+                } else {
+                    $deleteStmt->execute([$path, $name]);
+                }
+
+            }
+
+            return true;
+
+        });
+
+    }
+
+    /**
+     * This method is called after a node is deleted.
+     *
+     * This allows a backend to clean up all associated properties.
+     */
+    public function delete($path) {
+
+        $stmt = $this->pdo->prepare("DELETE FROM propertystorage WHERE path = ?");
+        $stmt->execute([$path]);
+
+    }
+
+}
diff --git a/lib/DAV/PropertyStorage/Plugin.php b/lib/DAV/PropertyStorage/Plugin.php
new file mode 100644
index 0000000..e787a1f
--- /dev/null
+++ b/lib/DAV/PropertyStorage/Plugin.php
@@ -0,0 +1,90 @@
+<?php
+
+namespace Sabre\DAV\PropertyStorage;
+
+use Sabre\DAV\Server;
+use Sabre\DAV\ServerPlugin;
+use Sabre\DAV\PropPatch;
+use Sabre\DAV\PropFind;
+use Sabre\DAV\INode;
+
+class Plugin extends ServerPlugin {
+
+    /**
+     * Creates the plugin
+     *
+     * @param Backend\BackendInterface $backend
+     */
+    public function __construct(Backend\BackendInterface $backend) {
+
+        $this->backend = $backend;
+
+    }
+
+    /**
+     * This initializes the plugin.
+     *
+     * This function is called by Sabre\DAV\Server, after
+     * addPlugin is called.
+     *
+     * This method should set up the required event subscriptions.
+     *
+     * @param Server $server
+     * @return void
+     */
+    public function initialize(Server $server) {
+
+        $server->on('propFind', [$this, 'propFind'], 101);
+        $server->on('propPatch', [$this, 'propPatch'], 300);
+        $server->on('afterUnbind', [$this, 'afterUnbind']);
+
+    }
+
+    /**
+     * Called during PROPFIND operations.
+     *
+     * If there's any requested properties that don't have a value yet, this
+     * plugin will look in the property storage backend to find them.
+     *
+     * @param PropFind $propFind
+     * @param INode $node
+     * @return void
+     */
+    public function propFind(PropFind $propFind, INode $node) {
+
+        $this->backend->propFind($propFind->getPath(), $propFind);
+
+    }
+
+    /**
+     * Called during PROPPATCH operations
+     *
+     * If there's any updated properties that haven't been stored, the
+     * propertystorage backend can handle it.
+     *
+     * @param string $path
+     * @param PropPatch $propPatch
+     * @return void
+     */
+    public function propPatch($path, PropPatch $propPatch) {
+
+        $this->backend->propPatch($path, $propPatch);
+
+    }
+
+    /**
+     * Called after a node is deleted.
+     *
+     * This allows the backend to clean up any properties still in the
+     * database.
+     *
+     * @param string $path
+     * @return void
+     */
+    public function afterUnbind($path) {
+
+        $this->backend->delete($path);
+
+    }
+
+}

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