Skip to content

Commit

Permalink
feat(agent): add proxy methods to improve the agent customization (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasalexandre9 committed Apr 14, 2023
1 parent ef4aa1d commit 2984207
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions src/Service/ForestAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ForestAdmin\AgentPHP\Agent\Http\Router;
use ForestAdmin\AgentPHP\Agent\Utils\Env;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Charts\Chart;
use ForestAdmin\AgentPHP\DatasourceToolkit\Components\Contracts\DatasourceContract;
use ForestAdmin\SymfonyForestAdmin\Controller\ForestController;
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
use Symfony\Component\HttpKernel\KernelInterface;
Expand All @@ -20,7 +21,8 @@ class ForestAgent implements RouteLoaderInterface
public AgentFactory $agent;

/**
* @throws \ReflectionException
* @param KernelInterface $appKernel
* @param EntityManagerInterface $entityManager
*/
public function __construct(private KernelInterface $appKernel, private EntityManagerInterface $entityManager)
{
Expand All @@ -29,27 +31,30 @@ public function __construct(private KernelInterface $appKernel, private EntityMa
$this->loadConfiguration();
}

private function loadConfiguration(): void
public function addDatasource(DatasourceContract $datasource, array $options = []): self
{
if (file_exists($this->appKernel->getProjectDir() . '/forest/symfony_forest_admin.php')) {
$callback = require $this->appKernel->getProjectDir() . '/forest/symfony_forest_admin.php';
$callback($this);
}
$this->agent->addDatasource($datasource, $options);

return $this;
}

private function loadOptions(): array
public function addChart(string $name, \Closure $definition): self
{
return [
'debug' => Env::get('FOREST_DEBUG', true),
'authSecret' => Env::get('FOREST_AUTH_SECRET'),
'envSecret' => Env::get('FOREST_ENV_SECRET'),
'forestServerUrl' => Env::get('FOREST_SERVER_URL', 'https://api.forestadmin.com'),
'isProduction' => Env::get('APP_ENV', 'dev') === 'prod',
'prefix' => Env::get('FOREST_PREFIX', 'forest'),
'cacheDir' => $this->appKernel->getContainer()->getParameter('kernel.cache_dir') . '/forest',
'schemaPath' => $this->appKernel->getProjectDir() . '/.forestadmin-schema.json',
'projectDir' => $this->appKernel->getProjectDir(),
];
$this->agent->addChart($name, $definition);

return $this;
}

public function build(): void
{
$this->agent->build();
}

public function customizeCollection(string $name, \Closure $handle): self
{
$this->agent->customizeCollection($name, $handle);

return $this;
}

/**
Expand Down Expand Up @@ -80,4 +85,27 @@ public function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}

private function loadConfiguration(): void
{
if (file_exists($this->appKernel->getProjectDir() . '/forest/symfony_forest_admin.php')) {
$callback = require $this->appKernel->getProjectDir() . '/forest/symfony_forest_admin.php';
$callback($this);
}
}

private function loadOptions(): array
{
return [
'debug' => Env::get('FOREST_DEBUG', true),
'authSecret' => Env::get('FOREST_AUTH_SECRET'),
'envSecret' => Env::get('FOREST_ENV_SECRET'),
'forestServerUrl' => Env::get('FOREST_SERVER_URL', 'https://api.forestadmin.com'),
'isProduction' => Env::get('APP_ENV', 'dev') === 'prod',
'prefix' => Env::get('FOREST_PREFIX', 'forest'),
'cacheDir' => $this->appKernel->getContainer()->getParameter('kernel.cache_dir') . '/forest',
'schemaPath' => $this->appKernel->getProjectDir() . '/.forestadmin-schema.json',
'projectDir' => $this->appKernel->getProjectDir(),
];
}
}

0 comments on commit 2984207

Please sign in to comment.