Initial work

This commit is contained in:
2025-08-30 16:11:06 +02:00
parent 4e5f154492
commit 774d1cf45f
41 changed files with 1217 additions and 281 deletions

View File

@@ -0,0 +1,164 @@
<?php
namespace App\Livewire;
use App\Bots\BotContract;
use Cron\CronExpression;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\URL;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Lorisleiva\CronTranslator\CronParsingException;
use Lorisleiva\CronTranslator\CronTranslator;
class CreateEditBot extends Component
{
public string $name = '';
public string $class = '';
public string $schedule = '* * * * *';
public bool $enabled = true;
public string $cron_text = 'Every minute';
public string $config = '{}';
public array $classList = [];
public array $configSchema = [];
public string $routeName = '';
public function mount(?int $bot = null)
{
$this->routeName = URL::current() === route('bots.create') ? 'Create Bot' : 'Edit Bot';
$bot = \App\Models\Bot::find($bot);
if ($bot) {
$this->name = $bot->name;
$this->class = $bot->class;
$this->schedule = $bot->schedule;
$this->enabled = $bot->enabled;
$this->cron_text = \Lorisleiva\CronTranslator\CronTranslator::translate($bot->schedule);
}
$basePath = $basePath ?? app_path('Bots');
$baseNamespace = $baseNamespace ?? 'App\\Bots';
foreach (File::allFiles($basePath) as $file) {
// Convert file path → class name
$relativePath = str_replace([$basePath . DIRECTORY_SEPARATOR, '.php'], '', $file->getPathname());
$relativeNamespace = str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);
$class = $baseNamespace . '\\' . $relativeNamespace;
// Make sure class exists and implements BotContract
if (class_exists($class) && in_array(BotContract::class, class_implements($class))) {
$label = method_exists($class, 'label')
? $class::label()
: class_basename($class);
$this->classList[$label] = $class;
}
}
}
public function updatedClass($value)
{
$this->class = $this->classList[$value];
$this->configSchema = $this->classList[$value]::configSchema();
}
protected function rules(): array
{
$schemaRules = [];
foreach ($this->configSchema as $field => $meta) {
$schemaRules["config.$field"] = $meta['rules'] ?? [];
}
return array_merge([
'name' => [
'required',
'string',
'max:255',
Rule::unique('bots', 'name')->ignore($this->name, 'name'),
],
'class' => [
'required',
'string',
'max:255',
],
'schedule' => [
'required',
'string',
'max:255',
function ($attribute, $value, $fail) {
if (!CronExpression::isValidExpression($value)) {
$fail('The ' . $attribute . ' is not a valid cron expression.');
}
},
],
'enabled' => ['boolean'],
], $schemaRules);
}
public function save(): void
{
$this->validate();
if (!class_exists($this->class)) {
$this->addError('class', 'The specified class does not exist.');
return;
}
if (!in_array(\App\Bots\BotContract::class, class_implements($this->class))) {
$this->addError('class', 'The specified class does not exist.');
return;
}
if (URL::current() !== route('bots.create')) {
$bot = \App\Models\Bot::where('name', $this->name)->first();
if ($bot) {
$bot->update([
'name' => $this->name,
'class' => $this->class,
'enabled' => $this->enabled,
'schedule' => $this->schedule,
]);
flash()->success('Bot updated successfully.');
return;
}
} else {
\App\Models\Bot::create([
'name' => $this->name,
'class' => $this->class,
'enabled' => $this->enabled,
'schedule' => $this->schedule,
]);
}
flash()->success('Bot created successfully.');
$this->reset(['name', 'namespace', 'class', 'enabled', 'schedule']);
}
public function updatedSchedule($value)
{
try {
$this->cron_text = CronTranslator::translate($value);
$this->resetErrorBag('schedule');
} catch (CronParsingException $e) {
$this->cron_text = '';
}
}
public function render()
{
return view('livewire.create-edit-bot');
}
}