4 trait ResourceExistenceChecker
7 * Checks if the given input is a file or folder.
9 * @param string|string[] $resources
10 * @param string $type "file", "dir", "fileAndDir"
12 * @return bool True if no errors were encountered otherwise false.
14 protected function checkResources($resources, $type = 'fileAndDir')
16 if (!in_array($type, ['file', 'dir', 'fileAndDir'])) {
17 throw new \InvalidArgumentException(sprintf('Invalid resource check of type "%s" used!', $type));
19 if (is_string($resources)) {
20 $resources = [$resources];
23 foreach ($resources as $resource) {
24 $glob = glob($resource);
25 if ($glob === false) {
26 $this->printTaskError(sprintf('Invalid glob "%s"!', $resource), $this);
30 foreach ($glob as $resource) {
31 if (!$this->checkResource($resource, $type)) {
40 * Checks a single resource, file or directory.
42 * It will print an error as well on the console.
44 * @param string $resource File or folder.
45 * @param string $type "file", "dir", "fileAndDir"
49 protected function checkResource($resource, $type)
53 if (!$this->isFile($resource)) {
54 $this->printTaskError(sprintf('File "%s" does not exist!', $resource), $this);
59 if (!$this->isDir($resource)) {
60 $this->printTaskError(sprintf('Directory "%s" does not exist!', $resource), $this);
65 if (!$this->isDir($resource) && !$this->isFile($resource)) {
66 $this->printTaskError(sprintf('File or directory "%s" does not exist!', $resource), $this);
74 * Convenience method to check the often uses "source => target" file / folder arrays.
76 * @param string|array $resources
78 protected function checkSourceAndTargetResource($resources)
80 if (is_string($resources)) {
81 $resources = [$resources];
85 foreach ($resources as $source => $target) {
89 $this->checkResources($sources);
90 $this->checkResources($targets);
94 * Wrapper method around phps is_dir()
96 * @param string $directory
100 protected function isDir($directory)
102 return is_dir($directory);
106 * Wrapper method around phps file_exists()
108 * @param string $file
112 protected function isFile($file)
114 return file_exists($file);