Skip to content

Commit

Permalink
fix: allows user to cache the agent's configuration (#82)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: add a new configuration file config/forest.php and move the previous configuration to a new template file agentTemplate/forest_admin.php
  • Loading branch information
matthv authored Jan 16, 2024
1 parent 5ad8678 commit 8d39c44
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
22 changes: 22 additions & 0 deletions agentTemplate/forest_admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use ForestAdmin\AgentPHP\Agent\Builder\AgentFactory;
use ForestAdmin\AgentPHP\DatasourceEloquent\EloquentDatasource;

return static function () {
$defaultDB = config('database.default');
app()->make(AgentFactory::class)->addDatasource(
new EloquentDatasource(
[
'driver' => config('database.connections.' . $defaultDB . '.driver'),
'host' => config('database.connections.' . $defaultDB . '.host'),
'port' => config('database.connections.' . $defaultDB . '.port'),
'database' => config('database.connections.' . $defaultDB . '.database'),
'username' => config('database.connections.' . $defaultDB . '.username'),
'password' => config('database.connections.' . $defaultDB . '.password'),
// OR
// 'url' => config('database.connections.' . $defaultDB . '.url'),
]
),
);
};
14 changes: 14 additions & 0 deletions config/forest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return [
'debug' => env('FOREST_DEBUG', true),
'authSecret' => env('FOREST_AUTH_SECRET'),
'envSecret' => env('FOREST_ENV_SECRET'),
'forestServerUrl' => env('FOREST_SERVER_URL', 'https://api.forestadmin.com'),
'isProduction' => env('FOREST_ENVIRONMENT', 'dev') === 'prod',
'prefix' => env('FOREST_PREFIX', 'forest'),
'permissionExpiration' => env('FOREST_PERMISSIONS_EXPIRATION_IN_SECONDS', 300),
'cacheDir' => storage_path('framework/cache/data/forest'),
'schemaPath' => base_path() . '/.forestadmin-schema.json',
'projectDir' => base_path(),
];
21 changes: 0 additions & 21 deletions config/forest_admin.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Commands/ForestInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function handle(): void

$this->call('vendor:publish', [
'--provider' => ForestServiceProvider::class,
'--tag' => 'forest',
'--tag' => 'config',
]);
}
Expand Down
21 changes: 19 additions & 2 deletions src/ForestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ public function boot(Kernel $kernel): void

$this->publishes(
[
$this->configFile() => $this->app['path.config'] . DIRECTORY_SEPARATOR . 'forest_admin.php',
$this->configFile() => config_path('forest.php'),
],
'config'
);

$this->publishes(
[
$this->agentTemplateFile() => $this->appForestPath() . DIRECTORY_SEPARATOR . 'forest_admin.php',
],
'forest'
);

$kernel->pushMiddleware(ForestCors::class);
}

Expand All @@ -41,6 +48,16 @@ public function boot(Kernel $kernel): void
*/
protected function configFile(): string
{
return realpath(__DIR__ . '/../config/forest_admin.php');
return realpath(__DIR__ . '/../config/forest.php');
}

protected function agentTemplateFile(): string
{
return realpath(__DIR__ . '/../agentTemplate/forest_admin.php');
}

protected function appForestPath(): string
{
return base_path() . DIRECTORY_SEPARATOR . 'forest';
}
}
23 changes: 6 additions & 17 deletions src/Providers/AgentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AgentProvider extends ServiceProvider
public function boot()
{
if ($this->forestIsPlanted()) {
$this->app->instance(AgentFactory::class, new AgentFactory($this->loadOptions()));
$this->app->instance(AgentFactory::class, new AgentFactory(config('forest')));
$this->loadConfiguration();
}
}
Expand All @@ -34,7 +34,7 @@ public function loadRoutes()

private function forestIsPlanted(): bool
{
return env('FOREST_AUTH_SECRET') && env('FOREST_ENV_SECRET');
return config('forest.authSecret') && config('forest.envSecret');
}

private function transformMethodsValuesToUpper(array|string $methods): array
Expand All @@ -48,28 +48,17 @@ private function transformMethodsValuesToUpper(array|string $methods): array

private function loadConfiguration(): void
{
if (file_exists($this->app['path.config'] . DIRECTORY_SEPARATOR . 'forest_admin.php')) {
$callback = require $this->app['path.config'] . DIRECTORY_SEPARATOR . 'forest_admin.php';
if (file_exists($this->appForestConfig())) {
$callback = require $this->appForestConfig();
$callback($this);

$this->app->make(AgentFactory::class)->build();
$this->loadRoutes();
}
}

private function loadOptions(): array
private function appForestConfig(): string
{
return [
'debug' => env('FOREST_DEBUG', true),
'authSecret' => env('FOREST_AUTH_SECRET'),
'envSecret' => env('FOREST_ENV_SECRET'),
'forestServerUrl' => env('FOREST_SERVER_URL', 'https://api.forestadmin.com'),
'isProduction' => env('FOREST_ENVIRONMENT', 'dev') === 'prod',
'prefix' => env('FOREST_PREFIX', 'forest'),
'permissionExpiration' => env('FOREST_PERMISSIONS_EXPIRATION_IN_SECONDS', 300),
'cacheDir' => storage_path('framework/cache/data/forest'),
'schemaPath' => base_path() . '/.forestadmin-schema.json',
'projectDir' => base_path(),
];
return base_path() . DIRECTORY_SEPARATOR . 'forest' . DIRECTORY_SEPARATOR . 'forest_admin.php';
}
}

0 comments on commit 8d39c44

Please sign in to comment.