From 52ca28c2f827e435f1dfabcb564e9c9ebcd532f5 Mon Sep 17 00:00:00 2001 From: bugalot Date: Tue, 10 Apr 2018 11:49:11 +0200 Subject: [PATCH] LocalAdapter implementation with more options. --- src/LocalAdapter.php | 179 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100755 src/LocalAdapter.php diff --git a/src/LocalAdapter.php b/src/LocalAdapter.php new file mode 100755 index 0000000..eace459 --- /dev/null +++ b/src/LocalAdapter.php @@ -0,0 +1,179 @@ +ensureDirectoryWait; + } + + /** + * Set Ensure Directory Wait + * + * Set whether or not to try multiple time to 'ensure' a directory before writing data. + * + * @param bool $ensureDirectoryWait + */ + public function setEnsureDirectoryWait($ensureDirectoryWait) + { + $this->ensureDirectoryWait = $ensureDirectoryWait; + } + + /** + * Set Ensure Directory Wait Time + * + * Set how much time (microseconds) waiting between two directory creation attempts + * before writing data. + * + * @param integer $ensureDirectoryWaitTime A time in microseconds. + */ + public function setEnsureDirectoryWaitTime($ensureDirectoryWaitTime) + { + $this->ensureDirectoryWaitTime = $ensureDirectoryWaitTime; + } + + /** + * Get Ensure Directory Wait Time + * + * Get how much time (microseconds) waiting between two directory creation attempts + * before writing data. + * + * @return integer A time in microseconds. + */ + public function getEnsureDirectoryWaitTime() + { + return $this->ensureDirectoryWaitTime; + } + + /** + * Set Ensure Directory Wait Attempts + * + * Set how much attempts must be performed to create a directory before writing data. + * + * @param integer $ensureDirectoryWaitAttempts + */ + public function setEnsureDirectoryWaitAttempts($ensureDirectoryWaitAttempts) + { + if ($ensureDirectoryWaitAttempts <= 0) { + $ensureDirectoryWaitAttempts = 1; + } + + $this->ensureDirectoryWaitAttempts = $ensureDirectoryWaitAttempts; + } + + /** + * Get Ensure Directory Wait Attempts + * + * Get how much attempts must be performed to create a directory before writing data. + * + * @return integer + */ + public function getEnsureDirectoryWaitAttempts() + { + return $this->ensureDirectoryWaitAttempts; + } + + /** + * Ensure the root directory exists. + * + * In case of 'ensureDirectoryWait' option is enabled, the implementation will try + * to create the directory 'ensureDirectoryWaitAttempts' times before failing. + * + * @param string $root root directory path + * @return void + * @see LocalAdapter::mustEnsureDirectoryWait() + * @see LocalAdapter::setEnsureDirectoryWaitAttempts() + * @see LocalAdapter::setEnsureDirectoryWaitTime() + * + * @throws Exception in case the root directory can not be created + */ + protected function ensureDirectory($root) + { + if ($this->mustEnsureDirectoryWait() === true) { + + if (!is_dir($root) || !is_writable($root)) { + $waitTime = $this->getEnsureDirectoryWaitTime(); + $attempts = $this->getEnsureDirectoryWaitAttempts(); + $i = 0; + + while ($i < $attempts) { + $umask = umask(0); + @mkdir($root, $this->permissionMap['dir']['public'], true); + umask($umask); + + if (is_dir($root) && is_writable($root)) { + break; + } else { + usleep($waitTime); + } + + $i++; + } + + if ($i >= $attempts) { + throw new Exception(sprintf('Impossible to create the root directory "%s" after %d attempts.', $root, $attempts)); + } + } + } else { + parent::ensureDirectory($root); + } + } +} \ No newline at end of file