From 0f6e7be3b921701db00660de2345d48d99c0d565 Mon Sep 17 00:00:00 2001 From: Julian Seidenberg Date: Fri, 27 Jul 2012 14:21:24 +1200 Subject: [PATCH] API-CHANGE: enabling replace document --- code/DMS.php | 10 +++++----- code/DMSDocument.php | 13 +++---------- tests/DMSDocumentTest.php | 2 +- tests/DMSTagTest.php | 10 +++------- tests/DMSTest.php | 6 +++--- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/code/DMS.php b/code/DMS.php index 47d6274..42d3b24 100644 --- a/code/DMS.php +++ b/code/DMS.php @@ -18,12 +18,12 @@ class DMS implements DMSInterface { self::$dmsPath = BASE_PATH . DIRECTORY_SEPARATOR . self::$dmsFolder; $dms = new DMS(); - $dms->createStorageFolder(self::$dmsPath); + self::createStorageFolder(self::$dmsPath); $dms->modelClass = $className; return $dms; } - function transformFileToFilePath($file) { + static function transformFileToFilePath($file) { //confirm we have a file $filePath = null; if (is_string($file)) $filePath = $file; @@ -45,7 +45,7 @@ class DMS implements DMSInterface { //create a new document and get its ID $modelClass = $this->modelClass; - $doc = new $modelClass($this); + $doc = new $modelClass(); $doc->write(); $doc->storeDocument($filePath); @@ -90,7 +90,7 @@ class DMS implements DMSInterface { * Creates a storage folder for the given path * @param $path Path to create a folder for */ - protected function createStorageFolder($path) { + static function createStorageFolder($path) { if (!is_dir($path)) { mkdir($path, 0777); } @@ -99,7 +99,7 @@ class DMS implements DMSInterface { /** * Calculates the storage path from a database DMSDocument ID */ - function getStorageFolder($id) { + static function getStorageFolder($id) { $folderName = intval($id / self::$dmsFolderSize); return $folderName; } diff --git a/code/DMSDocument.php b/code/DMSDocument.php index 8b9e386..24cda78 100644 --- a/code/DMSDocument.php +++ b/code/DMSDocument.php @@ -13,12 +13,6 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { 'Tags' => 'DMSTag' ); - protected $dms; //this DMSDocument's associated DMS instance - - function __construct($dms = null) { - $this->dms = $dms; - } - /** * Associates this document with a Page. This method does nothing if the association already exists. * This could be a simple wrapper around $myDoc->Pages()->add($myPage) to add a many_many relation @@ -335,14 +329,13 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { function storeDocument($filePath) { if (empty($this->ID)) user_error("Document must be written to database before it can store documents",E_USER_ERROR); - if (empty($dms) || !(is_a($dms, 'DMS'))) user_error("You need to provide a DMS object when storing a document",E_USER_ERROR); //calculate all the path to copy the file to $fromFilename = basename($filePath); $toFilename = $this->ID. '~' . $fromFilename; //add the docID to the start of the Filename - $toFolder = $this->dms->getStorageFolder($this->ID); + $toFolder = DMS::getStorageFolder($this->ID); $toPath = DMS::$dmsPath . DIRECTORY_SEPARATOR . $toFolder . DIRECTORY_SEPARATOR . $toFilename; - $this->dms->createStorageFolder(DMS::$dmsPath . DIRECTORY_SEPARATOR . $toFolder); + DMS::createStorageFolder(DMS::$dmsPath . DIRECTORY_SEPARATOR . $toFolder); //copy the file into place $fromPath = BASE_PATH . DIRECTORY_SEPARATOR . $filePath; @@ -366,7 +359,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { * @return DMSDocumentInstance Document object that we replaced the file in */ function replaceDocument($file) { - $filePath = $this->dms->transformFileToFilePath($file); + $filePath = DMS::transformFileToFilePath($file); $doc = $this->storeDocument($filePath); //replace the document return $doc; } diff --git a/tests/DMSDocumentTest.php b/tests/DMSDocumentTest.php index 60c1247..06f1552 100644 --- a/tests/DMSDocumentTest.php +++ b/tests/DMSDocumentTest.php @@ -39,7 +39,7 @@ class DMSDocumentTest extends SapphireTest { $s2 = $this->objFromFixture('SiteTree','s2'); $s3 = $this->objFromFixture('SiteTree','s3'); - $doc = new DMSDocument(DMS::getDMSInstance()); + $doc = new DMSDocument(); $doc->Filename = "test file"; $doc->Folder = "0"; $doc->write(); diff --git a/tests/DMSTagTest.php b/tests/DMSTagTest.php index 4ccff29..7dade60 100644 --- a/tests/DMSTagTest.php +++ b/tests/DMSTagTest.php @@ -15,9 +15,7 @@ class DMSTagTest extends SapphireTest { } function testAddingTags() { - $dms = DMS::getDMSInstance(); - - $doc = new DMSDocument($dms); + $doc = new DMSDocument(); $doc->Filename = "test file"; $doc->Folder = "0"; $doc->write(); @@ -34,7 +32,7 @@ class DMSTagTest extends SapphireTest { $this->assertTrue(in_array("banana",$fruits),"correct fruit tags returned"); //sneakily create another document and link one of the tags to that, too - $doc2 = new DMSDocument($dms); + $doc2 = new DMSDocument(); $doc2->Filename = "sneaky file"; $doc2->Folder = "0"; $doc2->write(); @@ -63,9 +61,7 @@ class DMSTagTest extends SapphireTest { } function testRemovingTags() { - $dms = DMS::getDMSInstance(); - - $doc = new DMSDocument($dms); + $doc = new DMSDocument(); $doc->Filename = "test file"; $doc->Folder = "0"; $doc->write(); diff --git a/tests/DMSTest.php b/tests/DMSTest.php index 11df6df..dd029fc 100644 --- a/tests/DMSTest.php +++ b/tests/DMSTest.php @@ -111,9 +111,9 @@ class DMSTest extends SapphireTest { $this->assertNotNull($document, "Document object created"); $this->assertTrue(file_exists(DMS::$dmsPath . DIRECTORY_SEPARATOR . $document->Folder . DIRECTORY_SEPARATOR . $document->Filename),"Document file copied into DMS folder"); - $this->assertContains($document->Filename, "DMS-test-document-2", "Original document filename is contain in the new filename"); - $this->assertEquals($document->Title, "My custom title", "Custom title not modified"); - $this->assertEquals($document->Description, "My custom description", "Custom description not modified"); + $this->assertContains("DMS-test-document-2",$document->Filename, "Original document filename is contain in the new filename"); + $this->assertEquals("My custom title", $document->Title , "Custom title not modified"); + $this->assertEquals("My custom description", $document->Description, "Custom description not modified"); }