Skip to content

Commit 89f4d1d

Browse files
Allow finding operations from multiple directories
1 parent 7376604 commit 89f4d1d

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/OneTimeOperationManager.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class OneTimeOperationManager
1616
{
17+
private static $paths = [];
18+
1719
/**
1820
* @return Collection<OneTimeOperationFile>
1921
*/
@@ -54,7 +56,13 @@ public static function getUnprocessedFiles(): Collection
5456
public static function getAllFiles(): Collection
5557
{
5658
try {
57-
return collect(File::files(self::getDirectoryPath()));
59+
$files = [];
60+
61+
foreach (self::getOperationPaths() as $path) {
62+
$files = array_merge($files, File::files($path));
63+
}
64+
65+
return collect($files);
5866
} catch (DirectoryNotFoundException $e) {
5967
return collect();
6068
}
@@ -95,7 +103,17 @@ public static function getOperationFileByName(string $operationName): OneTimeOpe
95103

96104
public static function pathToFileByName(string $operationName): string
97105
{
98-
return self::getDirectoryPath().self::buildFilename($operationName);
106+
foreach (self::getOperationPaths() as $path) {
107+
$path = Str::of($path)->rtrim('/');
108+
$fullPath = $path . DIRECTORY_SEPARATOR . self::buildFilename($operationName);
109+
if (!file_exists($fullPath)) {
110+
continue;
111+
}
112+
113+
return $fullPath;
114+
}
115+
116+
throw new \RuntimeException("The operation '$operationName' is invalid!");
99117
}
100118

101119
public static function fileExistsByName(string $operationName): bool
@@ -113,6 +131,29 @@ public static function getDirectoryPath(): string
113131
return App::basePath(Str::of(self::getDirectoryName())->rtrim('/')).DIRECTORY_SEPARATOR;
114132
}
115133

134+
/**
135+
* Get all the operation paths.
136+
*
137+
* @return array
138+
*/
139+
public static function getOperationPaths()
140+
{
141+
return array_merge(
142+
self::$paths, [self::getDirectoryPath()]
143+
);
144+
}
145+
146+
/**
147+
* Register operation paths.
148+
*
149+
* @param array|string $paths
150+
* @return void
151+
*/
152+
public static function loadOperationsFrom($paths)
153+
{
154+
self::$paths = array_merge(self::$paths, (array)$paths);
155+
}
156+
116157
public static function getOperationNameFromFilename(string $filename): string
117158
{
118159
return str($filename)->remove('.php');

0 commit comments

Comments
 (0)