Skip to content

Commit 8f4037b

Browse files
Adding documentation
1 parent 24a1f1f commit 8f4037b

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

docs/userguide/ObjectStore/Storage/Container.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,22 @@ access your objects, where they come from, how many requests for each object you
146146
```php
147147
$container->enableLogging();
148148
$container->disableLogging();
149-
```
149+
```
150+
151+
## Syncing containers
152+
153+
You can synchronize local directories with your CloudFiles/Swift containers very easily. When you do this, the container
154+
will mirror exactly the nested file structure within your local directory:
155+
156+
```php
157+
$container->uploadDirectory('/home/Jamie/blog');
158+
```
159+
160+
There are four scenarios you should be aware of:
161+
162+
Local|Remote|Comparison|Action
163+
---|---|---|---
164+
File exists|File exists|Identical checksum|No action
165+
File exists|File exists|Different checksum|Local file overwrites remote
166+
File exists|File does not exist|-|Local file created in Swift
167+
Files does not exist|File exists|-|Remote file deleted

lib/OpenCloud/ObjectStore/Upload/DirectorySync.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
<?php
2+
/**
3+
* PHP OpenCloud library.
4+
*
5+
* @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
6+
* @license https://www.apache.org/licenses/LICENSE-2.0
7+
* @author Jamie Hannaford <[email protected]>
8+
*/
29

310
namespace OpenCloud\ObjectStore\Upload;
411

@@ -8,12 +15,34 @@
815
use OpenCloud\Common\Exceptions\InvalidArgumentError;
916
use OpenCloud\ObjectStore\Resource\Container;
1017

18+
/**
19+
* DirectorySync upload class, in charge of creating, replacing and delete data objects on the API. The goal of
20+
* this execution is to sync local directories with remote CloudFiles containers so that they are consistent.
21+
*
22+
* @package OpenCloud\ObjectStore\Upload
23+
*/
1124
class DirectorySync
1225
{
26+
/**
27+
* @var string The path to the directory you're syncing.
28+
*/
1329
private $basePath;
30+
/**
31+
* @var ResourceIterator A collection of remote files in Swift.
32+
*/
1433
private $remoteFiles;
34+
/**
35+
* @var AbstractContainer The Container object you are syncing.
36+
*/
1537
private $container;
1638

39+
/**
40+
* Basic factory method to instantiate a new DirectorySync object with all the appropriate properties.
41+
*
42+
* @param $path The local path
43+
* @param Container $container The container you're syncing
44+
* @return DirectorySync
45+
*/
1746
public static function factory($path, Container $container)
1847
{
1948
$transfer = new self();
@@ -24,6 +53,10 @@ public static function factory($path, Container $container)
2453
return $transfer;
2554
}
2655

56+
/**
57+
* @param $path
58+
* @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
59+
*/
2760
public function setBasePath($path)
2861
{
2962
if (!file_exists($path)) {
@@ -33,16 +66,31 @@ public function setBasePath($path)
3366
$this->basePath = $path;
3467
}
3568

69+
/**
70+
* @param ResourceIterator $remoteFiles
71+
*/
3672
public function setRemoteFiles(ResourceIterator $remoteFiles)
3773
{
3874
$this->remoteFiles = $remoteFiles;
3975
}
4076

77+
/**
78+
* @param Container $container
79+
*/
4180
public function setContainer(Container $container)
4281
{
4382
$this->container = $container;
4483
}
4584

85+
/**
86+
* Execute the sync process. This will collect all the remote files from the API and do a comparison. There are
87+
* four scenarios that need to be dealt with:
88+
*
89+
* - Exists locally, exists remotely (identical checksum) = no action
90+
* - Exists locally, exists remotely (diff checksum) = local overwrites remote
91+
* - Exists locally, not exists remotely = local is written to remote
92+
* - Not exists locally, exists remotely = remote file is deleted
93+
*/
4694
public function execute()
4795
{
4896
$localFiles = $this->traversePath($this->basePath);
@@ -95,6 +143,12 @@ public function execute()
95143
$this->container->getService()->bulkDelete($deletePaths);
96144
}
97145

146+
/**
147+
* Given a path, traverse it recursively for nested files.
148+
*
149+
* @param $path
150+
* @return array
151+
*/
98152
private function traversePath($path)
99153
{
100154
$filenames = array();
@@ -115,11 +169,23 @@ private function traversePath($path)
115169
return $filenames;
116170
}
117171

172+
/**
173+
* Given a path, trim away leading slashes and strip the base path.
174+
*
175+
* @param $file
176+
* @return string
177+
*/
118178
private function trimFilename($file)
119179
{
120180
return ltrim(str_replace($this->basePath, '', $file->getPathname()), '/');
121181
}
122182

183+
/**
184+
* Get the callback used to do a search function on the remote iterator.
185+
*
186+
* @param $name The name of the file we're looking for.
187+
* @return callable
188+
*/
123189
private function getCallback($name)
124190
{
125191
$name = trim($name, '/');

lib/OpenCloud/ObjectStore/Upload/TransferBuilder.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*
55
* @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
66
* @license https://www.apache.org/licenses/LICENSE-2.0
7-
* @author Glen Campbell <[email protected]>
87
* @author Jamie Hannaford <[email protected]>
98
*/
109

0 commit comments

Comments
 (0)