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
310namespace OpenCloud \ObjectStore \Upload ;
411
815use OpenCloud \Common \Exceptions \InvalidArgumentError ;
916use 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+ */
1124class 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 , '/ ' );
0 commit comments