Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(onboarding): set forest_server_url into .env during install #8

Merged
merged 3 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@
$services
->set(InstallCommand::class)
->public()
->arg('$projectDir', '%kernel.project_dir%')
->tag('console.command');
};
49 changes: 43 additions & 6 deletions src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace ForestAdmin\SymfonyForestAdmin\Command;

use ForestAdmin\AgentPHP\Agent\Facades\Cache;
use Illuminate\Support\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;

class InstallCommand extends Command
{
public function __construct(private string $projectDir)
public function __construct(private KernelInterface $appKernel)
{
parent::__construct();
}
Expand All @@ -28,39 +29,75 @@ protected function configure()
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws ExceptionInterface
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->deleteDirectory($this->appKernel->getContainer()->getParameter('kernel.cache_dir') . '/forest');

$keys = [
'FOREST_AUTH_SECRET' => Str::random(32),
'FOREST_ENV_SECRET' => $input->getArgument('secretKey'),
];

if (isset($_SERVER['FOREST_SERVER_URL'])) {
$keys['FOREST_SERVER_URL'] = $_SERVER['FOREST_SERVER_URL'];
}

$this->addKeysToEnvFile($output, $keys, $input->getArgument('envFileName'));

$this->publishConfig($output);

Cache::forget('config');

return Command::SUCCESS;
}

private function addKeysToEnvFile(OutputInterface $output, array $keys, string $envFileName): void
{
foreach ($keys as $key => $value) {
file_put_contents($this->projectDir . '/' . $envFileName, PHP_EOL . "$key=$value", FILE_APPEND);
file_put_contents($this->appKernel->getProjectDir() . '/' . $envFileName, PHP_EOL . "$key=$value", FILE_APPEND);
}
$output->writeln('<info>✅ Env keys correctly set</info>');
}

private function publishConfig(OutputInterface $output)
{
$defaultConfigFile = __DIR__ . '/../../config/default.config';
$publishFileName = $this->projectDir . '/config/packages/symfony_forest_admin.php';
$publishFileName = $this->appKernel->getProjectDir() . '/config/packages/symfony_forest_admin.php';
if (! file_exists($publishFileName)) {
copy($defaultConfigFile, $publishFileName);
$output->writeln('<info>✅ Config file set</info>');
} else {
$output->writeln('<info>⚠️ Forest Admin config file already setup</info>');
}
}

private function deleteDirectory($dirname)
{
// Sanity check
if (! file_exists($dirname)) {
return false;
}

// Simple delete for a file
if (is_file($dirname) || is_link($dirname)) {
return unlink($dirname);
}

// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}

// Recurse
$this->deleteDirectory($dirname . DIRECTORY_SEPARATOR . $entry);
}

// Clean up
$dir->close();

return rmdir($dirname);
}
}
3 changes: 2 additions & 1 deletion src/Command/SendApimapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ForestAdmin\SymfonyForestAdmin\Command;

use ForestAdmin\AgentPHP\Agent\Builder\AgentFactory;
use ForestAdmin\SymfonyForestAdmin\Service\ForestAgent;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -22,7 +23,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->forestAgent->agent->sendSchema();
AgentFactory::sendSchema(true);

$output->writeln('<info>✅ Apimap sent</info>');

Expand Down