diff --git a/.env b/.env new file mode 100644 index 0000000..5e46cfc --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +APP_ENV=dev +APP_DEBUG=true \ No newline at end of file diff --git a/common.php b/common.php index 52e6e1e..cc83d84 100644 --- a/common.php +++ b/common.php @@ -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); diff --git a/composer.json b/composer.json index e7a67c3..6b23fa6 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/services.php b/config/services.php index 16fd9bd..c66aa92 100644 --- a/config/services.php +++ b/config/services.php @@ -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(); @@ -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']); diff --git a/src/App/Kernel.php b/src/App/Kernel.php index 323bf91..137788a 100644 --- a/src/App/Kernel.php +++ b/src/App/Kernel.php @@ -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(); diff --git a/src/SmsConnector/AbstractConnector.php b/src/SmsConnector/AbstractConnector.php index 1ea0af6..de828e4 100644 --- a/src/SmsConnector/AbstractConnector.php +++ b/src/SmsConnector/AbstractConnector.php @@ -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); }