X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fconsolidation%2Frobo%2Fsrc%2FCommon%2FResourceExistenceChecker.php;fp=vendor%2Fconsolidation%2Frobo%2Fsrc%2FCommon%2FResourceExistenceChecker.php;h=233f90a9b7e0304307a0a834fb3bcaf79fa73f29;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/consolidation/robo/src/Common/ResourceExistenceChecker.php b/vendor/consolidation/robo/src/Common/ResourceExistenceChecker.php new file mode 100644 index 000000000..233f90a9b --- /dev/null +++ b/vendor/consolidation/robo/src/Common/ResourceExistenceChecker.php @@ -0,0 +1,116 @@ +printTaskError(sprintf('Invalid glob "%s"!', $resource), $this); + $success = false; + continue; + } + foreach ($glob as $resource) { + if (!$this->checkResource($resource, $type)) { + $success = false; + } + } + } + return $success; + } + + /** + * Checks a single resource, file or directory. + * + * It will print an error as well on the console. + * + * @param string $resource File or folder. + * @param string $type "file", "dir", "fileAndDir" + * + * @return bool + */ + protected function checkResource($resource, $type) + { + switch ($type) { + case 'file': + if (!$this->isFile($resource)) { + $this->printTaskError(sprintf('File "%s" does not exist!', $resource), $this); + return false; + } + return true; + case 'dir': + if (!$this->isDir($resource)) { + $this->printTaskError(sprintf('Directory "%s" does not exist!', $resource), $this); + return false; + } + return true; + case 'fileAndDir': + if (!$this->isDir($resource) && !$this->isFile($resource)) { + $this->printTaskError(sprintf('File or directory "%s" does not exist!', $resource), $this); + return false; + } + return true; + } + } + + /** + * Convenience method to check the often uses "source => target" file / folder arrays. + * + * @param string|array $resources + */ + protected function checkSourceAndTargetResource($resources) + { + if (is_string($resources)) { + $resources = [$resources]; + } + $sources = []; + $targets = []; + foreach ($resources as $source => $target) { + $sources[] = $source; + $target[] = $target; + } + $this->checkResources($sources); + $this->checkResources($targets); + } + + /** + * Wrapper method around phps is_dir() + * + * @param string $directory + * + * @return bool + */ + protected function isDir($directory) + { + return is_dir($directory); + } + + /** + * Wrapper method around phps file_exists() + * + * @param string $file + * + * @return bool + */ + protected function isFile($file) + { + return file_exists($file); + } +}