From 4725d4935828c12aeca73a9907b2dd94f27b8007 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Fri, 1 Sep 2017 15:44:59 +0200 Subject: [PATCH] [FEATURE] Bundle configuration file Also adapt the PHPMD configuration file to allow static access to the YAML class. --- Classes/Core/ApplicationKernel.php | 66 ++++++++++++++++++--------- Classes/Core/ApplicationStructure.php | 2 +- Configuration/PHPMD/rules.xml | 2 +- Configuration/bundles.yml | 6 +++ 4 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 Configuration/bundles.yml diff --git a/Classes/Core/ApplicationKernel.php b/Classes/Core/ApplicationKernel.php index 04499fa6..ef13d88b 100644 --- a/Classes/Core/ApplicationKernel.php +++ b/Classes/Core/ApplicationKernel.php @@ -3,13 +3,13 @@ namespace PhpList\PhpList4\Core; -use PhpList\PhpList4\ApplicationBundle\PhpListApplicationBundle; -use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\WebServerBundle\WebServerBundle; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\HttpKernel\Bundle\BundleInterface; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Yaml\Yaml; /** * This class takes care of processing HTTP requests using Symfony. @@ -28,21 +28,11 @@ class ApplicationKernel extends Kernel */ public function registerBundles(): array { - $bundles = [ - new FrameworkBundle(), - new PhpListApplicationBundle(), - ]; - + $bundles = $this->bundlesFromConfiguration(); if ($this->shouldHaveDevelopmentBundles()) { $bundles[] = new WebServerBundle(); } - // This will later be changed so that the REST API package can register itself to the core. - if ($this->isRestBundleInstalled()) { - $className = $this->getRestBundleClassName(); - $bundles[] = new $className(); - } - return $bundles; } @@ -65,7 +55,15 @@ public function getRootDir() } /** - * @return string + * Returns the absolute path to the application root. + * + * When phplist4-core is installed as a dependency (library) of an application, this method will return + * the application's package path. + * + * When phpList4-core is installed stand-alone (i.e., as an application - usually only for testing), + * this method will be the phpList4-core package path. + * + * @return string the absolute path without the trailing slash */ private function getApplicationDir(): string { @@ -129,24 +127,48 @@ public function registerContainerConfiguration(LoaderInterface $loader) /** * @return bool */ - private function isRestBundleInstalled(): bool + private function shouldHaveDevelopmentBundles(): bool { - return class_exists($this->getRestBundleClassName()); + return $this->environment !== Environment::PRODUCTION; } /** - * @return string + * Reads the bundles from the budnle configuration file and instantiates them. + * + * @return Bundle[] */ - private function getRestBundleClassName(): string + private function bundlesFromConfiguration(): array { - return 'PhpList\\RestBundle\\PhpListRestBundle'; + $bundles = []; + + /** @var string[] $packageBundles */ + foreach ($this->readBundleConfiguration() as $packageBundles) { + foreach ($packageBundles as $bundleClassName) { + if (class_exists($bundleClassName)) { + $bundles[] = new $bundleClassName(); + } + } + } + + return $bundles; } /** - * @return bool + * Reads the bundle configuration file and returns two-dimensional array: + * + * 'package name' => [0 => 'bundle class name'] + * + * @return string[][] + * + * @throws \RuntimeException if the configuration file cannot be read */ - private function shouldHaveDevelopmentBundles(): bool + private function readBundleConfiguration(): array { - return $this->environment !== Environment::PRODUCTION; + $configurationFilePath = $this->getApplicationDir() . '/Configuration/bundles.yml'; + if (!is_readable($configurationFilePath)) { + throw new \RuntimeException('The file "' . $configurationFilePath . '" could not be read.', 1504272377302); + } + + return Yaml::parse(file_get_contents($configurationFilePath)); } } diff --git a/Classes/Core/ApplicationStructure.php b/Classes/Core/ApplicationStructure.php index 44ca6bb2..1c725daa 100644 --- a/Classes/Core/ApplicationStructure.php +++ b/Classes/Core/ApplicationStructure.php @@ -31,7 +31,7 @@ public function getCorePackageRoot(): string * When phpList4-core is installed stand-alone (i.e., as an application - usually only for testing), * this method will be the phpList4-core package path. * - * @return string the absolute path without the trailing slash. + * @return string the absolute path without the trailing slash * * @throws \RuntimeException if there is no composer.json in the application root */ diff --git a/Configuration/PHPMD/rules.xml b/Configuration/PHPMD/rules.xml index d3bd9435..f546eeb9 100644 --- a/Configuration/PHPMD/rules.xml +++ b/Configuration/PHPMD/rules.xml @@ -9,7 +9,7 @@ + value="\Doctrine\ORM\EntityManager,\Doctrine\ORM\Tools\Setup,\Symfony\Component\Debug\Debug,PhpList\PhpList4\Core\Environment,Symfony\Component\Yaml\Yaml"/> diff --git a/Configuration/bundles.yml b/Configuration/bundles.yml new file mode 100644 index 00000000..293ed0aa --- /dev/null +++ b/Configuration/bundles.yml @@ -0,0 +1,6 @@ +# This file will be removed and automatically generated later. +"phplist/phplist4-core": + - "Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle" + - "PhpList\\PhpList4\\ApplicationBundle\\PhpListApplicationBundle" +"phplist/rest-api": + - "PhpList\\RestBundle\\PhpListRestBundle"