diff --git a/src/OneTimeOperationManager.php b/src/OneTimeOperationManager.php index 106eac4..6301dbe 100644 --- a/src/OneTimeOperationManager.php +++ b/src/OneTimeOperationManager.php @@ -14,6 +14,8 @@ class OneTimeOperationManager { + private static $paths = []; + /** * @return Collection */ @@ -54,7 +56,13 @@ public static function getUnprocessedFiles(): Collection public static function getAllFiles(): Collection { try { - return collect(File::files(self::getDirectoryPath())); + $files = []; + + foreach (self::getOperationPaths() as $path) { + $files = array_merge($files, File::files($path)); + } + + return collect($files); } catch (DirectoryNotFoundException $e) { return collect(); } @@ -95,7 +103,17 @@ public static function getOperationFileByName(string $operationName): OneTimeOpe public static function pathToFileByName(string $operationName): string { - return self::getDirectoryPath().self::buildFilename($operationName); + foreach (self::getOperationPaths() as $path) { + $path = Str::of($path)->rtrim('/'); + $fullPath = $path . DIRECTORY_SEPARATOR . self::buildFilename($operationName); + if (!file_exists($fullPath)) { + continue; + } + + return $fullPath; + } + + throw new \RuntimeException("The operation '$operationName' is invalid!"); } public static function fileExistsByName(string $operationName): bool @@ -113,6 +131,29 @@ public static function getDirectoryPath(): string return App::basePath(Str::of(self::getDirectoryName())->rtrim('/')).DIRECTORY_SEPARATOR; } + /** + * Get all the operation paths. + * + * @return array + */ + public static function getOperationPaths() + { + return array_merge( + self::$paths, [self::getDirectoryPath()] + ); + } + + /** + * Register operation paths. + * + * @param array|string $paths + * @return void + */ + public static function loadOperationsFrom($paths) + { + self::$paths = array_merge(self::$paths, (array)$paths); + } + public static function getOperationNameFromFilename(string $filename): string { return str($filename)->remove('.php');