/** * Get the instance of the filesystem. * @return MongoFileSystem */ public static function getInstance() { if (self::$s_instance === null) { self::$s_instance = new MongoFileSystem(); } return self::$s_instance; }
function main() { $filesystem = MongoFileSystem::getInstance(); $rootFolder = new MongoFolder("/"); $filesystem->createRootFolder($rootFolder); $myNewFolder = new MongoFolder("user1"); $filesystem->createFolder($myNewFolder, $rootFolder); $filepath = __DIR__ . '/merkel-tree.jpg'; $myFile = new MongoFile($filepath); # At this point the file is saved into the storage system. $filesystem->createFile($myFile, $rootFolder); $myNewFolder->setPath("/a/really/long/path/of/folders/that/dont/exist"); $myNewFolder->setName("myNewName"); # Get the parent, this should be called "dont" print "my new folder after setting name: " . print_r($myNewFolder, true) . PHP_EOL; $parentFolder = $myNewFolder->getParentFolder(); $parentFolderName = $parentFolder->getName(); if ($parentFolderName !== "exist") { die("Parent folder should be called exist"); } if ($filesystem->getDirectorySize($parentFolder) != 1) { die("The 'dont' folder should only contain one folder so size should be 1"); } $filesystem->renameFolder($parentFolder, "changedName"); if ($myNewFolder->getPath() !== "/a/really/long/path/of/folders/that/dont/changedName") { $msg = "checking folder path after rename failed" . PHP_EOL . "expected: /a/really/long/path/of/folders/that/dont/changedName" . PHP_EOL . "recieved: " . $myNewFolder->getPath(); die($msg); } $filesystem->deleteFolder($myNewFolder); if ($filesystem->getDirectorySize($parentFolder) != 0) { die("The 'dont' folder should not contain anything"); } print "Completed without failing!" . PHP_EOL; }
/** * Set the path to this file/folder. I am assuming that this does not include the * name of the file/folder but am unsure. This means that if you want to rename a file * or folder you need to use the setName() method. * Setting the path should result in parent folders being created where necessary. (mkdir -p) * Changing the path of a folder act like updating a pointer rather than * resulting in having to send queries to update all of the sub-items. * * @param string $path - and absolute path such as /path/to/folder/or/file.php * * @return $this */ public function setPath($path) { if ($path !== "" && $path !== "/") { $folderNames = explode("/", $path); array_shift($folderNames); $parentFolder = MongoFileSystem::loadRoot(); foreach ($folderNames as $folderName) { $subFolders = $parentFolder->getSubFolders(); $indexedSubFolders = array(); foreach ($subFolders as $index => $subFolder) { $indexedSubFolders[$subFolder->getName()] = $subFolder; } if (isset($indexedSubFolders[$subFolder])) { $parentFolder = $indexedSubFolders[$subFolder]; } else { # Need to create the subfolders from here. $newFolder = new MongoFolder($folderName); $parentFolder = MongoFileSystem::getInstance()->createFolder($newFolder, $parentFolder); } } # The last "parent folder" from the loop should be the last in the path which we want to make # our parent if ($parentFolder === null) { throw new Exception("error set path resulted in a null parent folder"); } print PHP_EOL; print "SETPATH {$path}: setting parent folder" . PHP_EOL; $this->setParentFolder($parentFolder); $this->updateField('path', $path); } else { } return $this; }