Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow finding operations from multiple directories #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/OneTimeOperationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class OneTimeOperationManager
{
private static $paths = [];

/**
* @return Collection<OneTimeOperationFile>
*/
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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');
Expand Down