Skip to content

Commit

Permalink
using .env
Browse files Browse the repository at this point in the history
configure logger
  • Loading branch information
sveneld committed Jun 1, 2024
1 parent 9425713 commit 5ea7c17
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
APP_ENV=dev
APP_DEBUG=true
8 changes: 6 additions & 2 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
use BikeShare\Sms\SmsSenderInterface;
use BikeShare\SmsConnector\SmsConnectorInterface;
use BikeShare\User\User;
use Symfony\Component\Dotenv\Dotenv;

\Symfony\Component\ErrorHandler\Debug::enable();
$dotenv = new Dotenv();
$dotenv->loadEnv(__DIR__.'/.env', null, 'dev', ['test'], true);

$kernel = new Kernel('dev', true);
$kernel = new Kernel($_ENV['APP_ENV'], (bool) $_ENV['APP_DEBUG']);
$kernel->boot();

$logger = $kernel->getContainer()->get('logger');
Monolog\ErrorHandler::register($logger);

$configuration = $kernel->getContainer()->get(Configuration::class);
$sms = $kernel->getContainer()->get(SmsConnectorInterface::class);
$db = $kernel->getContainer()->get(DbInterface::class);
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"symfony/monolog-bundle": "^3.10",
"symfony/console": "^5.4",
"symfony/runtime": "^5.4",
"symfony/twig-bundle": "^5.4"
"symfony/twig-bundle": "^5.4",
"symfony/dotenv": "^5.4"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.10|^4.0",
Expand Down
9 changes: 8 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use BikeShare\SmsConnector\SmsConnectorFactory;
use BikeShare\SmsConnector\SmsConnectorInterface;
use PHPMailer\PHPMailer\PHPMailer;
use Symfony\Component\Dotenv\Command\DotenvDumpCommand;

return static function (ContainerConfigurator $container): void {
$services = $container->services();
Expand All @@ -32,7 +33,13 @@
$services->instanceof(MailSenderInterface::class)->tag('mailSender');
$services->instanceof(SmsConnectorInterface::class)->tag('smsConnector');

$services->alias('logger','monolog.logger');
$services->alias('logger', 'monolog.logger');

$services->set(DotenvDumpCommand::class)
->args([
param('kernel.project_dir') . '/.env',
param('kernel.environment'),
]);

$services->set(Configuration::class)
->args([__DIR__ . '/../config.php']);
Expand Down
10 changes: 10 additions & 0 deletions src/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
use MicroKernelTrait;

public function boot()
{
if ($this->debug) {
Debug::enable();
}

parent::boot();
}

protected function configureContainer(ContainerConfigurator $container): void
{
$configDir = $this->getConfigDir();
Expand Down
5 changes: 4 additions & 1 deletion src/SmsConnector/AbstractConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public function __construct(
$debugMode = false
) {
$this->debugMode = $debugMode;
$connectorConfig = json_decode($configuration->get('connectors')['config'][static::getType()] ?? '[]', true) ?? [];
$connectorConfig = json_decode(
$configuration->get('connectors')['config'][static::getType()] ?? '[]',
true
) ?? [];
$this->checkConfig($connectorConfig);
}

Expand Down

0 comments on commit 5ea7c17

Please sign in to comment.