2023-10-06 13:07:09 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Commands;
|
|
|
|
|
|
|
|
|
|
use DirectoryIterator;
|
|
|
|
|
use Illuminate\Console\Command;
|
2023-10-17 22:57:25 +03:00
|
|
|
use Lucent\Schema\Schema;
|
|
|
|
|
use Lucent\Schema\SchemaService;
|
|
|
|
|
use Lucent\Schema\Type;
|
2023-10-06 13:07:09 +03:00
|
|
|
|
|
|
|
|
class CompileSchemas extends Command
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
protected $signature = 'lucent:schemas';
|
|
|
|
|
|
|
|
|
|
protected $description = 'Compiles schemas';
|
|
|
|
|
|
|
|
|
|
|
2023-10-17 22:57:25 +03:00
|
|
|
public function __construct(
|
|
|
|
|
public SchemaService $schemaService
|
|
|
|
|
)
|
2023-10-06 13:07:09 +03:00
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
$configDir = base_path(config('lucent.schemas_path'));
|
|
|
|
|
$schemasDirIterator = new DirectoryIterator($configDir);
|
|
|
|
|
$schemas = [];
|
|
|
|
|
foreach ($schemasDirIterator as $file) {
|
|
|
|
|
if ($file->getExtension() !== "json") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$schemaJson = file_get_contents($configDir . "/" . $file->getFilename());
|
|
|
|
|
$schema = json_decode($schemaJson, true);
|
|
|
|
|
if (empty($schema)) {
|
|
|
|
|
$this->error("Invalid JSON " . $file->getFilename());
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$schemas[] = $schema;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 22:57:25 +03:00
|
|
|
$schemas = collect($schemas)->sortBy("label")->values();
|
|
|
|
|
$roles = $schemas
|
|
|
|
|
->map([$this->schemaService, 'fromArray'])
|
|
|
|
|
->whereIn("type", [Type::COLLECTION, Type::FILES])
|
|
|
|
|
->reduce(fn($carry, Schema $schema) => array_merge(
|
|
|
|
|
$carry,
|
|
|
|
|
$schema->read,
|
|
|
|
|
$schema->write,
|
2023-10-19 19:55:32 +03:00
|
|
|
config("lucent.canInvite") ?? [],
|
|
|
|
|
config("lucent.canBuild") ?? [],
|
2023-10-17 22:57:25 +03:00
|
|
|
), []);
|
|
|
|
|
|
|
|
|
|
$json = [
|
|
|
|
|
"schemas" => $schemas->toArray(),
|
|
|
|
|
"roles" => collect($roles)->push("admin")->push("removed")->unique()->values()->toArray()
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (!file_exists(storage_path("lucent"))) {
|
2023-10-06 13:07:09 +03:00
|
|
|
mkdir(storage_path("lucent"));
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 22:57:25 +03:00
|
|
|
file_put_contents(schemas_path(), json_encode($json));
|
2023-10-06 13:07:09 +03:00
|
|
|
|
|
|
|
|
$this->info("Lucent Schemas were updated");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|