lucent config

This commit is contained in:
2023-10-06 13:07:09 +03:00
parent 341ad267a5
commit 9bbe5ee9b4
9 changed files with 95 additions and 71 deletions
+1 -1
View File
@@ -51,7 +51,7 @@
</div> </div>
{#if channel.previewTarget} {#if channel.previewTarget}
<a href="{channel.previewTarget}?schema={schema.name}&id={record.id}" target="_blank" class="btn btn-outline-info ms-3"> <a href="{channel.previewTargetUrl}?schema={schema.name}&id={record.id}" target="_blank" class="btn btn-outline-info ms-3">
Preview Preview
</a> </a>
{/if} {/if}
+7 -1
View File
@@ -22,9 +22,15 @@ readonly class AuthService
} }
public function currentUserId(): ?string public function currentUserId(): ?string
{ {
return $this->session->get("user.id"); if(app()->runningInConsole()){
return config("lucent.systemUserId");
}else{
return $this->session->get("user.id");
}
} }
public function isLoggedIn(): bool public function isLoggedIn(): bool
+2 -1
View File
@@ -9,6 +9,7 @@ final class Channel
{ {
public string $lucentUrl; public string $lucentUrl;
public string $filesUrl; public string $filesUrl;
public string $previewTargetUrl;
/** /**
* @param Collection<Schema> $schemas * @param Collection<Schema> $schemas
@@ -20,11 +21,11 @@ final class Channel
public string $generateCommand, public string $generateCommand,
public Collection $schemas, public Collection $schemas,
public array $imageFilters, public array $imageFilters,
// public Collection $previewTargets,
) )
{ {
$this->lucentUrl = $url . "/lucent"; $this->lucentUrl = $url . "/lucent";
$this->filesUrl = $url . "/storage"; $this->filesUrl = $url . "/storage";
$this->previewTargetUrl = $url . "/". $previewTarget;
} }
} }
+10 -11
View File
@@ -21,25 +21,24 @@ final class ChannelService
public static function fromConfig(): ChannelService public static function fromConfig(): ChannelService
{ {
if(file_exists(storage_path("lucent.config.json"))){ $schemasArray = [];
$configJson = file_get_contents(storage_path("lucent.config.json")); if(file_exists(schemas_path())){
$configArray = json_decode($configJson, true); $schemasJson = file_get_contents(schemas_path());
}else{ $schemasArray = json_decode($schemasJson, true);
$configArray = null;
} }
$schemaService = new SchemaService(); $schemaService = new SchemaService();
$schemasCollection = (new Collection($configArray["schemas"] ?? []))->map([$schemaService, 'fromArray']); $schemasCollection = (new Collection($schemasArray))->map([$schemaService, 'fromArray']);
$channel = new Channel( $channel = new Channel(
name: $configArray["name"] ?? "", name: config("lucent.name") ?? "",
url: rtrim($configArray["url"] ?? "", "/"), url: rtrim( config("lucent.url") ?? "", "/"),
previewTarget: rtrim($configArray["previewTarget"] ?? "", "/"), previewTarget: rtrim( config("lucent.previewTarget") ?? "", "/"),
generateCommand: $configArray["generateCommand"] ?? "", generateCommand: config("lucent.generateCommand") ?? "",
schemas: $schemasCollection, schemas: $schemasCollection,
imageFilters: $configArray["imageFilters"] ?? [], imageFilters: config("lucent.imageFilters") ?? [],
); );
$channelService = new ChannelService($schemaService); $channelService = new ChannelService($schemaService);
$channelService->channel = $channel; $channelService->channel = $channel;
-52
View File
@@ -1,52 +0,0 @@
<?php
namespace Lucent\Commands;
use DirectoryIterator;
use Illuminate\Console\Command;
class CompileConfig extends Command
{
protected $signature = 'lucent:config';
protected $description = 'Compiles Config';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$configDir = base_path(config('lucent.config_path'));
$configJson = file_get_contents($configDir . "/lucent.json");
$config = json_decode($configJson, true);
$schemasDirIterator = new DirectoryIterator($configDir . "/Schemas");
$schemas = [];
foreach ($schemasDirIterator as $file) {
if ($file->getExtension() !== "json") {
continue;
}
$schemaJson = file_get_contents($configDir . "/Schemas/" . $file->getFilename());
$schema = json_decode($schemaJson, true);
if (empty($schema)) {
$this->error("Invalid JSON " . $file->getFilename());
return 0;
}
$schemas[] = $schema;
}
$config["schemas"] = collect($schemas)->sortBy("label")->values()->toArray();
$configOuputJson = json_encode($config);
file_put_contents(storage_path("lucent.config.json"), $configOuputJson);
$this->info("Lucent JSON update");
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace Lucent\Commands;
use DirectoryIterator;
use Illuminate\Console\Command;
class CompileSchemas extends Command
{
protected $signature = 'lucent:schemas';
protected $description = 'Compiles schemas';
public function __construct()
{
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;
}
$schemas = collect($schemas)->sortBy("label")->values()->toArray();
if(!file_exists(storage_path("lucent"))){
mkdir(storage_path("lucent"));
}
file_put_contents(schemas_path(), json_encode($schemas));
$this->info("Lucent Schemas were updated");
}
}
+6 -2
View File
@@ -1,6 +1,10 @@
<?php <?php
return [ return [
"config_path" => env("LUCENT_CONFIG_PATH", "app/Lucent") "schemas_path" => env("LUCENT_SCHEMAS_PATH", "app/Lucent"),
"name" => env("LUCENT_NAME", "Lucent"),
"url" => env("LUCENT_URL", "http://localhost:8000/"),
"previewTarget" => env("LUCENT_PREVIEW_TARGET", "previewTarget"),
"generateCommand" => env("LUCENT_GENERATE_COMMAND", "generate:static"),
"imageFilters" => [],
]; ];
+8 -3
View File
@@ -7,7 +7,7 @@ use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Intervention\Image\ImageManager; use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService; use Lucent\Channel\ChannelService;
use Lucent\Commands\CompileConfig; use Lucent\Commands\CompileSchemas;
use Lucent\Commands\RebuildThumbnails; use Lucent\Commands\RebuildThumbnails;
use Lucent\File\FileService; use Lucent\File\FileService;
use Lucent\File\ImageService; use Lucent\File\ImageService;
@@ -36,7 +36,12 @@ class LucentServiceProvider extends ServiceProvider
public function boot(Router $router): void public function boot(Router $router): void
{ {
$manifest = json_decode(file_get_contents(public_path('vendor/lucent/dist/manifest.json')),true); $manifestPath = public_path('vendor/lucent/dist/manifest.json');
$manifest = null;
if(file_exists($manifestPath)){
$manifest = json_decode(file_get_contents(public_path('vendor/lucent/dist/manifest.json')),true);
}
$router->aliasMiddleware('lucent.auth', \Lucent\Http\Middleware\AuthMiddleware::class); $router->aliasMiddleware('lucent.auth', \Lucent\Http\Middleware\AuthMiddleware::class);
$router->aliasMiddleware('lucent.guest', \Lucent\Http\Middleware\GuestMiddleware::class); $router->aliasMiddleware('lucent.guest', \Lucent\Http\Middleware\GuestMiddleware::class);
@@ -49,7 +54,7 @@ class LucentServiceProvider extends ServiceProvider
if ($this->app->runningInConsole()) { if ($this->app->runningInConsole()) {
$this->commands([ $this->commands([
CompileConfig::class, CompileSchemas::class,
RebuildThumbnails::class, RebuildThumbnails::class,
]); ]);
} }
+8
View File
@@ -37,3 +37,11 @@ if (!function_exists('make_dir_r')) {
is_dir($path) || mkdir($path, 0777, true); is_dir($path) || mkdir($path, 0777, true);
} }
} }
if (!function_exists('schemas_path')) {
function schemas_path(): string
{
return storage_path("lucent/lucent.schemas.json");
}
}