Skip to content

Commit

Permalink
Feat: enable create multiple pools of executors
Browse files Browse the repository at this point in the history
  • Loading branch information
komtcho committed Jul 27, 2024
1 parent c1a9c28 commit 6db50a4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
18 changes: 18 additions & 0 deletions src/ExecutorPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Pharaonic\Laravel\Executor;

class ExecutorPool
{
private static $paths = [];

public static function getPaths()
{
return static::$paths;
}

public static function addPath($path)
{
array_push(static::$paths, $path);
}
}
3 changes: 3 additions & 0 deletions src/ExecutorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Pharaonic\Laravel\Executor\Console\ExecuteMakeCommand;
use Pharaonic\Laravel\Executor\Console\ExecuteRollbackCommand;
use Pharaonic\Laravel\Executor\Console\ExecuteStatusCommand;
use Pharaonic\Laravel\Executor\ExecutorPool;

class ExecutorServiceProvider extends ServiceProvider
{
Expand All @@ -18,6 +19,8 @@ class ExecutorServiceProvider extends ServiceProvider
*/
public function boot()
{
ExecutorPool::addPath(base_path('executors'));

if ($this->app->runningInConsole()) {
// Publish Migrations
$this->publishes(
Expand Down
53 changes: 31 additions & 22 deletions src/Services/ExecutorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Pharaonic\Laravel\Executor\Services;

use Illuminate\Support\Facades\File;
use Pharaonic\Laravel\Executor\ExecutorPool;
use Pharaonic\Laravel\Executor\Models\Executor;
use ReflectionClass;

Expand Down Expand Up @@ -39,29 +40,37 @@ protected function prepareExecutors()
{
$db = Executor::all()->keyBy('executor');

return collect(array_map(function ($file) use ($db) {
$class = new ReflectionClass(include $file);
$name = basename($class->getFileName(), '.php');
$executor = [
'name' => $name,
'type' => $class->getProperty('type')->getDefaultValue(),
'tag' => $class->getProperty('tag')->getDefaultValue(),
'path' => $class->getFileName(),
'model' => $db[$name] ?? null
];

if ($executor['model']) {
$executor['model']->fill([
'type' => $executor['type'],
'tag' => $executor['tag'],
]);

if ($executor['model']->isDirty()) {
$executor['model']->save();
$collectExecutors = collect([]);

foreach (ExecutorPool::getPaths() as $path) {
$executors = collect(array_map(function ($file) use ($db) {
$class = new ReflectionClass(include $file);
$name = basename($class->getFileName(), '.php');
$executor = [
'name' => $name,
'type' => $class->getProperty('type')->getDefaultValue(),
'tag' => $class->getProperty('tag')->getDefaultValue(),
'path' => $class->getFileName(),
'model' => $db[$name] ?? null
];

if ($executor['model']) {
$executor['model']->fill([
'type' => $executor['type'],
'tag' => $executor['tag'],
]);

if ($executor['model']->isDirty()) {
$executor['model']->save();
}
}
}
return $executor;
}, File::glob($this->dir . '/*')))->keyBy('name');
return $executor;
}, File::glob($path . '/*')))->keyBy('name');

$collectExecutors = $collectExecutors->merge($executors->all());
}

return $collectExecutors;
}


Expand Down

0 comments on commit 6db50a4

Please sign in to comment.