Author: kn
Date: Tue Oct  2 17:43:17 2007
New Revision: 6342

Log:
- Started to use PropertyHandler for property serialization

Modified:
    trunk/Webdav/src/backends/file.php
    trunk/Webdav/tests/backend_file_test.php

Modified: trunk/Webdav/src/backends/file.php
==============================================================================
--- trunk/Webdav/src/backends/file.php [iso-8859-1] (original)
+++ trunk/Webdav/src/backends/file.php [iso-8859-1] Tue Oct  2 17:43:17 2007
@@ -224,17 +224,15 @@
         }
 
         // Check if some browser submitted mime type is available.
-        $storage = $this->getPropertyStoragePath(
-            $resource, 
-            new ezcWebdavDeadProperty( 'DAV:', 'getcontenttype' ) 
-        );
-
-        if ( is_file( $storage ) )
-        {
-            $property = unserialize( file_get_contents( $storage ) );
-            return $property->mime;
-        }
-
+        $storage = $this->getPropertyStorage( $resource );
+        $properties = $storage->getAllProperties();
+
+        if ( isset( $properties['DAV:']['getcontenttype'] ) )
+        {
+            return $properties['DAV:']['getcontenttype']->mime;
+        }
+
+        // Default to 'application/octet-stream' if nothing else is available.
         return 'application/octet-stream';
     }
 
@@ -310,42 +308,104 @@
      * path to the property storage file.
      * 
      * @param string $resource 
-     * @param ezcWebdavProperty $property 
      * @return string
      */
-    protected function getPropertyStoragePath( $resource, ezcWebdavProperty 
$property = null )
+    protected function getPropertyStoragePath( $resource )
     {
         // Get storage path for properties depending on the type of the
         // ressource.
-        if ( is_dir( $resource ) )
-        {
-            $storage = realpath( $this->root . $resource ) . '/' . 
$this->options->propertyStoragePath . '/';
-        }
-        else
-        {
-            $storage = realpath( $this->root . dirname( $resource ) ) . '/' . 
$this->options->propertyStoragePath . '/';
-        }
+        $storagePath = realpath( $this->root . dirname( $resource ) ) 
+            . '/' . $this->options->propertyStoragePath . '/'
+            . basename( $resource ) . '.xml';
 
         // Create property storage if it does not exist yet
-        if ( !is_dir( $storage ) )
-        {
-            mkdir( $storage, $this->options->directoryMode );
-        }
-
-        // Return root storage dir, if property has been ommitted.
-        if ( $property === null )
-        {
-            return $storage;
-        }
-
-        // Check if sub path for namespace exists and create otherwise
-        if ( !is_dir( $storage = $storage . base64_encode( 
$property->namespace ) ) )
-        {
-            mkdir( $storage, $this->options->directoryMode );
-        }
-
-        // Return path to property.
-        return $storage . '/' . $property->name . '.xml';
+        if ( !is_dir( dirname( $storagePath ) ) )
+        {
+            mkdir( dirname( $storagePath ), $this->options->directoryMode );
+        }
+
+        // Append name of namespace to property storage path
+        return $storagePath;
+    }
+
+    /**
+     * Get property storage
+     *
+     * Get property storage for a ressource for one namespace.
+     * 
+     * @param string $resource 
+     * @return ezcWebdavBasicPropertyStorage
+     */
+    protected function getPropertyStorage( $resource )
+    {
+        $storagePath = $this->getPropertyStoragePath( $resource );
+
+        // If no properties has been stored yet, just return an empty property
+        // storage.
+        if ( !is_file( $storagePath ) )
+        {
+            return new ezcWebdavBasicPropertyStorage();
+        }
+
+        // Create handler structure to read properties
+        $handler = new ezcWebdavPropertyHandler(
+            $xml = new ezcWebdavXmlTool()
+        );
+        $storage = new ezcWebdavBasicPropertyStorage();
+
+        // Read document
+        if ( !$doc = $xml->createDomDocument( file_get_contents( $storagePath 
) ) )
+        {
+            throw new ezcWebdavFileBackendBrokenStorageException(
+                "Could not open XML as DOMDocument: '{$storage}'."
+            );
+        }
+
+        // Get property node from document
+        $properties = $doc->getElementsByTagname( 'properties' )->item( 0 
)->childNodes;
+
+        // Extract and return properties
+        $handler->extractProperties( 
+            $properties,
+            $storage
+        );
+
+        return $storage;
+    }
+
+    /**
+     * Store properties back to file
+     *
+     * Create a new property storage file and store the properties back there.
+     * This depends on the affected resource and the actual properties in the
+     * property storage.
+     * 
+     * @param string $resource 
+     * @param ezcWebdavBasicPropertyStorage $storage 
+     * @return void
+     */
+    protected function storeProperties( $resource, 
ezcWebdavBasicPropertyStorage $storage )
+    {
+        $storagePath = $this->getPropertyStoragePath( $resource );
+
+        // Create handler structure to read properties
+        $handler = new ezcWebdavPropertyHandler(
+            $xml = new ezcWebdavXmlTool()
+        );
+
+        // Create new dom document with property storage for one namespace
+        $doc = new DOMDocument( '1.0' );
+
+        $properties = $doc->createElement( 'properties' );
+        $doc->appendChild( $properties );
+
+        // Store and store properties
+        $handler->serializeProperties(
+            $storage,
+            $properties
+        );
+
+        return $doc->save( $storagePath );
     }
 
     /**
@@ -357,8 +417,6 @@
      */
     public function setProperty( $resource, ezcWebdavProperty $property )
     {
-        $storage = $this->getPropertyStoragePath( $resource, $property );
-
         // Check if property is a self handled live property and return an
         // error in this case.
         if ( ( $property->namespace === 'DAV:' ) &&
@@ -368,10 +426,15 @@
             return false;
         }
 
-        // @TODO: Get rid of serialize here. We should either store them as
-        // vlaid XML, or use var_export. Both make some internal serialize
-        // methods fpr all properties necessary.
-        file_put_contents( $storage, serialize( $property ) );
+        // Get namespace property storage
+        $storage = $this->getPropertyStorage( $resource );
+
+        // Attach property to store
+        $storage->attach( $property );
+
+        // Store document back
+        $this->storeProperties( $resource, $storage );
+
         return true;
     }
 
@@ -390,12 +453,14 @@
             return false;
         }
 
-        $storage = $this->getPropertyStoragePath( $resource, $property );
-        
-        if ( is_file( $storage ) )
-        {
-            unlink( $storage );
-        }
+        // Get namespace property storage
+        $storage = $this->getPropertyStorage( $resource );
+
+        // Attach property to store
+        $storage->detach( $property );
+
+        // Store document back
+        $this->storeProperties( $resource, $storage );
 
         return true;
     }
@@ -404,20 +469,12 @@
      * Reset property storage for a resource.
      * 
      * @param string $resource 
-     * @param ezcWebdavPropertyStorage $properties
+     * @param ezcWebdavPropertyStorage $storage
      * @return bool
      */
-    public function resetProperties( $resource, ezcWebdavPropertyStorage 
$properties )
-    {
-        // Remove all properties by removing the property storage directory.
-        $storageDir = $this->getPropertyStoragePath( $resource );
-        ezcBaseFile::removeRecursive( $storageDir );
-
-        // Recreate all properties
-        foreach( $properties as $property )
-        {
-            $this->setProperty( $resource, $property );
-        }
+    public function resetProperties( $resource, ezcWebdavPropertyStorage 
$storage )
+    {
+        $this->storeProperties( $resource, $storage );
     }
 
     /**
@@ -433,12 +490,13 @@
      */
     public function getProperty( $resource, $propertyName, $namespace = 'DAV:' 
)
     {
-        $storage = $this->getPropertyStoragePath( $resource, new 
ezcWebdavDeadProperty( $namespace, $propertyName ) );
+        $storage = $this->getPropertyStorage( $resource );
 
         // Handle dead propreties
         if ( $namespace !== 'DAV:' )
         {
-            return unserialize( file_get_contents( $storage ) );
+            $properties = $storage->getAllProperties();
+            return $properties[$namespace][$name];
         }
 
         // Handle live properties
@@ -486,8 +544,9 @@
                 return $property;
 
             default:
-                // Handle unknown live properties like dead properties
-                return unserialize( file_get_contents( $storage ) );
+                // Handle all other live properties like dead properties
+                $properties = $storage->getAllProperties();
+                return $properties[$namespace][$name];
         }
     }
 
@@ -502,25 +561,14 @@
      */
     public function getAllProperties( $resource )
     {
-        $storageDir = $this->getPropertyStoragePath( $resource );
-
-        $storage = new ezcWebdavBasicPropertyStorage();
+        $storage = $this->getPropertyStorage( $resource );
         
-        // Scan through namespaces
-        foreach ( glob( $storageDir . '*', GLOB_ONLYDIR ) as $dir )
-        {
-            foreach ( glob( $dir . '/*.xml' ) as $file )
-            {
-                $storage->attach( 
-                    unserialize( file_get_contents( $file ) )
-                );
-            }
-        }
-
-        // Also attach generated live properties
-        foreach( $this->handledLiveProperties as $name )
-        {
-            $storage->attach( $this->getProperty( $resource, $name ) );
+        // Add all live properties to stored properties
+        foreach ( $this->handledLiveProperties as $property )
+        {
+            $storage->attach(
+                $this->getProperty( $resource, $property )
+            );
         }
 
         return $storage;

Modified: trunk/Webdav/tests/backend_file_test.php
==============================================================================
--- trunk/Webdav/tests/backend_file_test.php [iso-8859-1] (original)
+++ trunk/Webdav/tests/backend_file_test.php [iso-8859-1] Tue Oct  2 17:43:17 
2007
@@ -1425,10 +1425,10 @@
     
         $newProperties = new ezcWebdavFlaggedPropertyStorage();
         $newProperties->attach( $p1 = new ezcWebdavDeadProperty( 
-            'foo:', 'bar', 'some content'
+            'foo:', 'bar', '<content>some content</content>'
         ), ezcWebdavPropPatchRequest::SET );
         $newProperties->attach( $p2 = new ezcWebdavDeadProperty( 
-            'foo:', 'blubb', 'some other content'
+            'foo:', 'blubb', '<content>some other content</content>'
         ), ezcWebdavPropPatchRequest::SET );
 
         $addedProperties = new ezcWebdavBasicPropertyStorage();
@@ -1523,13 +1523,13 @@
         // Add properties, but cause errors
         $newProperties = new ezcWebdavFlaggedPropertyStorage();
         $newProperties->attach( $p_bar = new ezcWebdavDeadProperty( 
-            'foo:', 'bar', 'some content'
+            'foo:', 'bar', '<content>some content</content>'
         ), ezcWebdavPropPatchRequest::SET );
         $newProperties->attach( $p_blubb = new ezcWebdavDeadProperty( 
-            'foo:', 'blubb', 'some other content'
+            'foo:', 'blubb', '<content>some other content</content>'
         ), ezcWebdavPropPatchRequest::SET );
         $newProperties->attach( $p_blah = new ezcWebdavDeadProperty( 
-            'foo:', 'blah', 'evn more content'
+            'foo:', 'blah', '<content>even more content</content>'
         ), ezcWebdavPropPatchRequest::SET );
 
         $addedProperties = new ezcWebdavBasicPropertyStorage();
@@ -1560,10 +1560,10 @@
         // First add some custom properties.
         $newProperties = new ezcWebdavFlaggedPropertyStorage();
         $newProperties->attach( $p_bar = new ezcWebdavDeadProperty( 
-            'foo:', 'bar', 'some content'
+            'foo:', 'bar', '<content>some content</content>'
         ), ezcWebdavPropPatchRequest::SET );
         $newProperties->attach( $p_blubb = new ezcWebdavDeadProperty( 
-            'foo:', 'blubb', 'some other content'
+            'foo:', 'blubb', '<content>some other content</content>'
         ), ezcWebdavPropPatchRequest::SET );
 
         $request = new ezcWebdavPropPatchRequest( '/resource' );
@@ -1644,10 +1644,10 @@
         // First add some custom properties.
         $newProperties = new ezcWebdavFlaggedPropertyStorage();
         $newProperties->attach( $p_bar = new ezcWebdavDeadProperty( 
-            'foo:', 'bar', 'some content'
+            'foo:', 'bar', '<content>some content</content>'
         ), ezcWebdavPropPatchRequest::SET );
         $newProperties->attach( $p_blubb = new ezcWebdavDeadProperty( 
-            'foo:', 'blubb', 'some other content'
+            'foo:', 'blubb', '<content>some other content</content>'
         ), ezcWebdavPropPatchRequest::SET );
 
         $request = new ezcWebdavPropPatchRequest( '/resource' );
@@ -1706,10 +1706,10 @@
         // First add some custom properties.
         $newProperties = new ezcWebdavFlaggedPropertyStorage();
         $newProperties->attach( $p_bar = new ezcWebdavDeadProperty( 
-            'foo:', 'bar', 'some content'
+            'foo:', 'bar', '<content>some content</content>'
         ), ezcWebdavPropPatchRequest::SET );
         $newProperties->attach( $p_blubb = new ezcWebdavDeadProperty( 
-            'foo:', 'blubb', 'some other content'
+            'foo:', 'blubb', '<content>some other content</content>'
         ), ezcWebdavPropPatchRequest::SET );
 
         $request = new ezcWebdavPropPatchRequest( '/resource' );
@@ -1732,7 +1732,7 @@
         $updateProperties = new ezcWebdavFlaggedPropertyStorage();
         $updateProperties->attach( $p_blubb, ezcWebdavPropPatchRequest::REMOVE 
);
         $updateProperties->attach( 
-            $p_foo = new ezcWebdavDeadProperty( 'foo:', 'foo', 'random 
content' ),
+            $p_foo = new ezcWebdavDeadProperty( 'foo:', 'foo', 
'<content>random content</content>' ),
             ezcWebdavPropPatchRequest::SET
         );
         $updateProperties->attach( $p_bar, ezcWebdavPropPatchRequest::REMOVE );
@@ -1800,10 +1800,10 @@
         // First add some custom properties.
         $newProperties = new ezcWebdavFlaggedPropertyStorage();
         $newProperties->attach( $p_bar = new ezcWebdavDeadProperty( 
-            'foo:', 'bar', 'some content'
+            'foo:', 'bar', '<content>some content</content>'
         ), ezcWebdavPropPatchRequest::SET );
         $newProperties->attach( $p_blubb = new ezcWebdavDeadProperty( 
-            'foo:', 'blubb', 'some other content'
+            'foo:', 'blubb', '<content>some other content</content>'
         ), ezcWebdavPropPatchRequest::SET );
 
         $request = new ezcWebdavPropPatchRequest( '/resource' );
@@ -1830,7 +1830,7 @@
             ezcWebdavPropPatchRequest::REMOVE
         );
         $updateProperties->attach( 
-            $p_foo = new ezcWebdavDeadProperty( 'foo:', 'foo', 'random 
content' ),
+            $p_foo = new ezcWebdavDeadProperty( 'foo:', 'foo', 
'<content>random content</content>' ),
             ezcWebdavPropPatchRequest::SET
         );
         $updateProperties->attach( $p_bar, ezcWebdavPropPatchRequest::REMOVE );


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to