Skip to content

Commit

Permalink
[FEATURE] Bundle configuration file
Browse files Browse the repository at this point in the history
Also adapt the PHPMD configuration file to allow static access to the
YAML class.
  • Loading branch information
Oliver Klee committed Sep 1, 2017
1 parent e9168ef commit 4725d49
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
66 changes: 44 additions & 22 deletions Classes/Core/ApplicationKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

Expand All @@ -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
{
Expand Down Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion Classes/Core/ApplicationStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion Configuration/PHPMD/rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<rule ref="rulesets/cleancode.xml/StaticAccess">
<properties>
<property name="exceptions"
value="\Doctrine\ORM\EntityManager,\Doctrine\ORM\Tools\Setup,\Symfony\Component\Debug\Debug,PhpList\PhpList4\Core\Environment"/>
value="\Doctrine\ORM\EntityManager,\Doctrine\ORM\Tools\Setup,\Symfony\Component\Debug\Debug,PhpList\PhpList4\Core\Environment,Symfony\Component\Yaml\Yaml"/>
</properties>
</rule>

Expand Down
6 changes: 6 additions & 0 deletions Configuration/bundles.yml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 4725d49

Please sign in to comment.