From 21d8e8bbb8cd8913e60c1a9fa288fe52ece7a43d Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Thu, 11 Jun 2020 01:26:57 +0200 Subject: [PATCH] improve configurations for usage in multi-environment setups (#1775) --- .gitignore | 1 + bin/console | 20 ++++--------- config/packages/dev/jms_serializer.yaml | 7 ----- config/packages/jms_serializer.yaml | 4 +++ config/packages/kimai.yaml | 3 +- config/packages/prod/jms_serializer.yaml | 6 ---- config/packages/prod/webpack_encore.yaml | 4 --- config/packages/webpack_encore.yaml | 2 +- public/index.php | 5 +--- src/Command/InstallCommand.php | 8 +++++- src/Command/ReloadCommand.php | 11 +++++--- src/Command/UpdateCommand.php | 9 ++++-- src/DependencyInjection/AppExtension.php | 2 +- src/DependencyInjection/Configuration.php | 8 +----- src/Kernel.php | 28 ++++++++++++++----- .../DependencyInjection/AppExtensionTest.php | 5 ++-- .../DependencyInjection/ConfigurationTest.php | 8 ++++-- 17 files changed, 66 insertions(+), 65 deletions(-) delete mode 100644 config/packages/dev/jms_serializer.yaml delete mode 100644 config/packages/prod/jms_serializer.yaml delete mode 100644 config/packages/prod/webpack_encore.yaml diff --git a/.gitignore b/.gitignore index 34ac16ab..a60a90f8 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ php.ini /config/packages/*-local.yaml /config/packages/*/local.yaml /config/packages/*/*-local.yaml +/config/bundles-local.php public/avatars/*.png diff --git a/bin/console b/bin/console index 8b7e217f..8ccf63f3 100755 --- a/bin/console +++ b/bin/console @@ -1,31 +1,23 @@ #!/usr/bin/env php load(__DIR__.'/../.env'); } $input = new ArgvInput(); -$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'dev'); -$debug = ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)) && !$input->hasParameterOption(['--no-debug', '']); +$env = $input->getParameterOption(['--env', '-e'], $_SERVER['APP_ENV'] ?? 'prod'); +$debug = (bool) ($_SERVER['APP_DEBUG'] ?? (in_array($env, ['dev', 'test']))) && !$input->hasParameterOption(['--no-debug', '']); if ($debug) { umask(0000); diff --git a/config/packages/dev/jms_serializer.yaml b/config/packages/dev/jms_serializer.yaml deleted file mode 100644 index f9460410..00000000 --- a/config/packages/dev/jms_serializer.yaml +++ /dev/null @@ -1,7 +0,0 @@ -jms_serializer: - visitors: - json_serialization: - options: - - JSON_PRETTY_PRINT - - JSON_UNESCAPED_SLASHES - - JSON_PRESERVE_ZERO_FRACTION diff --git a/config/packages/jms_serializer.yaml b/config/packages/jms_serializer.yaml index b7ee3022..8a81ed87 100644 --- a/config/packages/jms_serializer.yaml +++ b/config/packages/jms_serializer.yaml @@ -3,6 +3,10 @@ jms_serializer: datetime: default_format: 'Y-m-d\TH:i:sO' # DATE_ISO8601 visitors: + json_serialization: + options: + - JSON_UNESCAPED_SLASHES + - JSON_PRESERVE_ZERO_FRACTION xml_serialization: format_output: '%kernel.debug%' metadata: diff --git a/config/packages/kimai.yaml b/config/packages/kimai.yaml index 7648a99d..eb19ecbb 100644 --- a/config/packages/kimai.yaml +++ b/config/packages/kimai.yaml @@ -302,8 +302,7 @@ kimai: # -------------------------------------------------------------------------------- # STORAGE -# If you want to use directories outside the Kimai directory, change these +# That's the place where plugins can store static content # -------------------------------------------------------------------------------- data_dir: '%kernel.project_dir%/var/data' - plugin_dir: '%kernel.project_dir%/var/plugins' # -------------------------------------------------------------------------------- diff --git a/config/packages/prod/jms_serializer.yaml b/config/packages/prod/jms_serializer.yaml deleted file mode 100644 index 89c86c89..00000000 --- a/config/packages/prod/jms_serializer.yaml +++ /dev/null @@ -1,6 +0,0 @@ -jms_serializer: - visitors: - json_serialization: - options: - - JSON_UNESCAPED_SLASHES - - JSON_PRESERVE_ZERO_FRACTION diff --git a/config/packages/prod/webpack_encore.yaml b/config/packages/prod/webpack_encore.yaml deleted file mode 100644 index 42969165..00000000 --- a/config/packages/prod/webpack_encore.yaml +++ /dev/null @@ -1,4 +0,0 @@ -webpack_encore: - # Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes) - # Available in version 1.2 - cache: true diff --git a/config/packages/webpack_encore.yaml b/config/packages/webpack_encore.yaml index 729f59ea..6c1bc8a4 100644 --- a/config/packages/webpack_encore.yaml +++ b/config/packages/webpack_encore.yaml @@ -10,4 +10,4 @@ webpack_encore: # Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes) # Available in version 1.2 - #cache: '%kernel.debug%' + cache: '%kernel.debug%' diff --git a/public/index.php b/public/index.php index 476a13fd..400c0ce6 100644 --- a/public/index.php +++ b/public/index.php @@ -9,14 +9,11 @@ require __DIR__.'/../vendor/autoload.php'; // The check is to ensure we don't use .env in production if (!isset($_SERVER['APP_ENV'])) { - if (!class_exists(Dotenv::class)) { - throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.'); - } (new Dotenv(true))->load(__DIR__.'/../.env'); } $env = $_SERVER['APP_ENV'] ?? 'prod'; -$debug = $_SERVER['APP_DEBUG'] ?? ('prod' !== $env); +$debug = (bool) ($_SERVER['APP_DEBUG'] ?? (in_array($env, ['dev', 'test']))); if ($debug) { umask(0000); diff --git a/src/Command/InstallCommand.php b/src/Command/InstallCommand.php index 25ae1fcf..84bd95c8 100644 --- a/src/Command/InstallCommand.php +++ b/src/Command/InstallCommand.php @@ -11,6 +11,7 @@ namespace App\Command; use App\Constants; use Doctrine\DBAL\Connection; +use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\ArrayInput; @@ -18,6 +19,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\HttpKernel\KernelInterface; /** * Command used to do the basic installation steps for Kimai. @@ -69,7 +71,11 @@ final class InstallCommand extends Command $io->title('Kimai installation running ...'); - $environment = getenv('APP_ENV'); + /** @var Application $application */ + $application = $this->getApplication(); + /** @var KernelInterface $kernel */ + $kernel = $application->getKernel(); + $environment = $kernel->getEnvironment(); // create the database, in case it is not yet existing try { diff --git a/src/Command/ReloadCommand.php b/src/Command/ReloadCommand.php index 713a52f5..22653e8c 100644 --- a/src/Command/ReloadCommand.php +++ b/src/Command/ReloadCommand.php @@ -9,12 +9,14 @@ namespace App\Command; +use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\HttpKernel\KernelInterface; /** * Command used to update a Kimai installation. @@ -81,10 +83,11 @@ final class ReloadCommand extends Command return self::ERROR_LINT_TRANSLATIONS; } - $environment = getenv('APP_ENV'); - if ($input->hasArgument('env')) { - $environment = $input->getArgument('env'); - } + /** @var Application $application */ + $application = $this->getApplication(); + /** @var KernelInterface $kernel */ + $kernel = $application->getKernel(); + $environment = $kernel->getEnvironment(); // flush the cache, in case values from the database are cached $cacheResult = $this->rebuildCaches($environment, $io, $input, $output); diff --git a/src/Command/UpdateCommand.php b/src/Command/UpdateCommand.php index 6037cd4d..71746a80 100644 --- a/src/Command/UpdateCommand.php +++ b/src/Command/UpdateCommand.php @@ -11,11 +11,13 @@ namespace App\Command; use App\Constants; use Doctrine\DBAL\Connection; +use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\HttpKernel\KernelInterface; /** * Command used to update a Kimai installation. @@ -66,8 +68,11 @@ final class UpdateCommand extends Command $io->title('Kimai updates running ...'); - // we cannot change the environment here, as it needs to be configured in the .env file before this command is started - $environment = getenv('APP_ENV'); + /** @var Application $application */ + $application = $this->getApplication(); + /** @var KernelInterface $kernel */ + $kernel = $application->getKernel(); + $environment = $kernel->getEnvironment(); // make sure database is available, Kimai running and installed try { diff --git a/src/DependencyInjection/AppExtension.php b/src/DependencyInjection/AppExtension.php index 3ebfeae5..80a3c1db 100644 --- a/src/DependencyInjection/AppExtension.php +++ b/src/DependencyInjection/AppExtension.php @@ -54,7 +54,7 @@ class AppExtension extends Extension // safe alternatives to %kernel.project_dir% $container->setParameter('kimai.data_dir', $config['data_dir']); - $container->setParameter('kimai.plugin_dir', $config['plugin_dir']); + $container->setParameter('kimai.plugin_dir', $container->getParameter('kernel.project_dir') . '/var/plugins'); $this->setLanguageFormats($config['languages'], $container); unset($config['languages']); diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index c3136360..59b01f93 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -46,13 +46,7 @@ class Configuration implements ConfigurationInterface ->end() ->end() ->scalarNode('plugin_dir') - ->isRequired() - ->validate() - ->ifTrue(function ($value) { - return !file_exists($value); - }) - ->thenInvalid('Plugin directory does not exist') - ->end() + ->setDeprecated('Changing the plugin directory via "kimai.plugin_dir" is not supported since 1.9') ->end() ->append($this->getUserNode()) ->append($this->getTimesheetNode()) diff --git a/src/Kernel.php b/src/Kernel.php index 693af9cc..fd48242f 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -98,6 +98,23 @@ class Kernel extends BaseKernel } } + if ($this->environment === 'test' && getenv('TEST_WITH_BUNDLES') === false) { + return; + } + + // we can either define all kimai bundles hardcoded ... + if (is_file($this->getProjectDir() . '/config/bundles-local.php')) { + $contents = require $this->getProjectDir() . '/config/bundles-local.php'; + foreach ($contents as $class => $envs) { + if (isset($envs['all']) || isset($envs[$this->environment])) { + yield new $class(); + } + } + + return; + } + + // ... or we load them dynamically from the plugins directory foreach ($this->getBundleDirectories() as $bundleDir) { $bundleName = $bundleDir->getRelativePathname(); $pluginClass = 'KimaiPlugin\\' . $bundleName . '\\' . $bundleName; @@ -112,10 +129,6 @@ class Kernel extends BaseKernel return []; } - if ($this->environment === 'test' && getenv('TEST_WITH_BUNDLES') === false) { - return []; - } - $directories = []; $finder = new Finder(); $finder->ignoreUnreadableDirs()->directories()->name('*Bundle'); @@ -207,9 +220,10 @@ class Kernel extends BaseKernel // load application routes $routes->import($confDir . '/routes' . self::CONFIG_EXTS, '/', 'glob'); - /** @var SplFileInfo $bundleDir */ - foreach ($this->getBundleDirectories() as $bundleDir) { - $routes->import($bundleDir->getRealPath() . '/Resources/config/routes' . self::CONFIG_EXTS, '/', 'glob'); + foreach ($this->bundles as $bundle) { + if (strpos(\get_class($bundle), 'KimaiPlugin\\') !== false) { + $routes->import($bundle->getPath() . '/Resources/config/routes' . self::CONFIG_EXTS, '/', 'glob'); + } } } diff --git a/tests/DependencyInjection/AppExtensionTest.php b/tests/DependencyInjection/AppExtensionTest.php index 91cb0006..de021a45 100644 --- a/tests/DependencyInjection/AppExtensionTest.php +++ b/tests/DependencyInjection/AppExtensionTest.php @@ -37,6 +37,7 @@ class AppExtensionTest extends TestCase { $container = new ContainerBuilder(); $container->setParameter('app_locales', 'de|en|tr|zh_CN'); + $container->setParameter('kernel.project_dir', realpath(__DIR__ . '/../../')); return $container; } @@ -55,7 +56,7 @@ class AppExtensionTest extends TestCase ], ], 'data_dir' => '/tmp/', - 'plugin_dir' => '/tmp/', + 'plugin_dir' => '/tmp/', // still here, to make sure that this value is NOT applied! 'timesheet' => [], 'saml' => [ 'connection' => [] @@ -72,7 +73,7 @@ class AppExtensionTest extends TestCase $expected = [ 'kimai.data_dir' => '/tmp/', - 'kimai.plugin_dir' => '/tmp/', + 'kimai.plugin_dir' => realpath(__DIR__ . '/../../') . '/var/plugins', 'kimai.languages' => [ 'en' => [ 'date_time_type' => 'yyyy-MM-dd HH:mm', diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index bfce5418..5791d12b 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -54,10 +54,12 @@ class ConfigurationTest extends TestCase public function testValidatePluginDir() { - $this->expectException(InvalidConfigurationException::class); - $this->expectExceptionMessage('Invalid configuration for path "kimai.plugin_dir": Plugin directory does not exist'); + $finalizedConfig = $this->getCompiledConfig($this->getMinConfig()); + $finalizedConfig['plugin_dir'] = 'sdfsdfs'; - $this->assertConfig($this->getMinConfig('/tmp/', 'sdfsdfs'), []); + $config = $this->getMinConfig('/tmp/', 'sdfsdfs'); + + $this->assertConfig($config, $finalizedConfig); } public function testValidateLdapConfigUserBaseDn()