Skip to content

Commit

Permalink
Commit regarding issues #3 , #5 and #6 (this commit is meant to impro…
Browse files Browse the repository at this point in the history
…ve the config path determination, the documentation and the adherence to Symfony's coding guidelines).

Markdown:

- CHANGELOG-3.x.md: Added the new improvement introduced by this commit.

PHP:

- ConfigPathUtility.php

1. Improved config path retrieval: This was done in order catch more configuration files especially of the bundles, this new way is now less reliant on the strict adherence to Symfony's recommended bundle structure.

2. Refactored the code to adhere more closely to Symfony's recommended coding style.

- CustomContainerBuilder.php:

1. Added and refactored documentation for functions and methods within the Container Builder in order to improve readability of the class and its methods.

- CustomDelegatingLoader.php: Also small refactorings to the documentation for improved readability.

- CustomGlobLoader.php: Same as before, more small refactorings to the documentation for better readability.

- CustomValueStorage.php: Refactorings of the code and the documentation to be more inline with the Symfony coding guidelines and also to improve readability.

- LoadInitializer.php: Added and refactored documentation for more readability and clarity on the class.

- LocationAwareParameterBag.php: Added documentation and refactored the existing code to be more inline with the aforementioned guidelines and for more clarity on the class and its methods.

- LocationRetrievalCoordinator.php: Also added and refactored the existing documentation with the same thinking as before.

- Utility.php: Added documentation on every function and the class and also refactored the functions a bit, for more information on the class and its methods.
  • Loading branch information
JAC - Frederic Bauer committed Dec 17, 2020
1 parent fb12c84 commit e61d9ae
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 103 deletions.
4 changes: 4 additions & 0 deletions Resources/doc/changelogs/CHANGELOG-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
* Fixed an issue with synchronous scrolling, where when the first node of a list was unique
to said list, the synchronous scrolling would throw an error and never complete

* Improved config path retrieval: Now the process is able to find configuration files more effectively
and easily and should be aware of every used file for configuration except for the custom bundle config
which is conducted by the bundles themselves.

## 3.0 (11.12.2020)

* This changelog has been created to ship with the first full version of the bundle
Expand Down
41 changes: 22 additions & 19 deletions src/LocationAwareConfigLoadBundle/ConfigPathUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public static function initializePathUtility(): void

// Parse the manual path_config-file
self::getUserDefinedPaths();
} catch (InvalidArgumentException $e) {
self::$configPaths = [];
} catch (Exception $error) {
} catch (InvalidArgumentException | Exception $e) {
self::$configPaths = [];
}
}
Expand All @@ -90,25 +88,27 @@ public static function initializePathUtility(): void
* @param string $extensionPath The path pointing to a bundle's ExtensionClass.
* @return string|null Returns the converted string or null, if the path does not point to the DependencyInjection or a directory which does not exist.
*/
public static function convertExtensionPathToConfigDirectory(string $extensionPath) {
// Get the index in the string where "DependencyInjection" is present
$diPosition = strpos($extensionPath,"DependencyInjection");
public static function convertExtensionPathToConfigDirectory(string $extensionPath): ?string
{
$configDirPath = preg_match("/\.php$/",$extensionPath)? dirname($extensionPath) : $extensionPath;

if(!$diPosition) {
return null;
if (preg_match("/\/$/", $configDirPath)) {
$configDirPath = substr($configDirPath,0,strlen($configDirPath)-1);
}

// Change it from DependencyInjection to the config directory
$configDirPath = substr($extensionPath,0,$diPosition)."Resources/config/";
while (!preg_match("/.*\/vendor\/?$/", $configDirPath)) {
if (file_exists($configDirPath.DIRECTORY_SEPARATOR."Resources".DIRECTORY_SEPARATOR."config")) {
$configDirPath = $configDirPath.DIRECTORY_SEPARATOR."Resources".DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR;
break;
} else if (file_exists($configDirPath.DIRECTORY_SEPARATOR."config")) {
$configDirPath = $configDirPath.DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR;
break;
}

if (!file_exists($configDirPath)) {
return null;
$configDirPath = dirname($configDirPath);
}

// Since the entire directory is added as a glob resource, the "*" signals that all files within the directory are
// to be looked at (only one level deep) and the extensions signal that only files which end on one of the config
// extensions are considered.
return $configDirPath."*".self::$configExtensions;
return preg_match("/\/$/", $configDirPath)? $configDirPath."*".self::$configExtensions : null;
}

/**
Expand All @@ -118,7 +118,8 @@ public static function convertExtensionPathToConfigDirectory(string $extensionPa
* @param string $configPath The path to be added to the list.
* @param bool $isGlobPattern A boolean stating whether the path is a glob-resource / pattern which will have to be loaded differently from non-glob-pattern.
*/
public static function addPathToPathlist(string $configPath, bool $isGlobPattern = true): void {
public static function addPathToPathlist(string $configPath, bool $isGlobPattern = true): void
{
// If the cache has not been initialised, initialise it.
if (!self::$cacheInitialized) {
self::initializePathUtility();
Expand All @@ -137,7 +138,8 @@ public static function addPathToPathlist(string $configPath, bool $isGlobPattern
*
* <br> Also signals, that a restart of the load process is useful / necessary.
*/
public static function storePaths(): void {
public static function storePaths(): void
{
if (self::$cacheInitialized && self::$pathsChanged) {
try {
self::$configPathCache->delete("cjw_config_paths");
Expand Down Expand Up @@ -234,7 +236,8 @@ private static function getUserDefinedPaths(): void
* @param array $path A path array (hopefully with 3 items under the keys of "path", "glob" and "addConfExt").
* @return bool Boolean which states whether the path at least passes the most basic checks regarding their structure.
*/
private static function checkUserDefinedPath(array $path): bool {
private static function checkUserDefinedPath(array $path): bool
{
if (is_array($path) && count($path) === 3) {
if (!(key_exists("path",$path) && is_string($path["path"]) && !empty($path["path"]))) {
return false;
Expand Down
41 changes: 35 additions & 6 deletions src/LocationAwareConfigLoadBundle/CustomContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use ReflectionClass;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

Expand Down Expand Up @@ -33,14 +34,22 @@ public function __construct()
*
* @param string $location The location to be set.
*/
public function setCurrentLocation(string $location) {

public function setCurrentLocation(string $location)
{
/** The parameterBag is the custom one created to feature such a function */
$this->parameterBag->setCurrentLocation($location);
}

/**
* @override
* In order to be able to actively influence the way locations are read for parameters during the bundle configuration
* process, the compilation of the container is caught through this function and then, after the measures for the
* bundle configuration are set in place, the normal compilation process of the container takes place.
*
* <br>This was done in order to prevent the bundles from constantly adding the same parameters unchanged back into
* the container, which led to dozens of useless entries into the location lists for every parameter.
*
* @param bool $resolveEnvPlaceholders
*/
public function compile(bool $resolveEnvPlaceholders = false)
{
Expand All @@ -61,6 +70,7 @@ public function compile(bool $resolveEnvPlaceholders = false)
* config directories to be tracked.
*
* @param string $name The name of the bundle who's extension config to retrieve.
*
* @return array Returns the found configuration.
*/
public function getExtensionConfig(string $name)
Expand Down Expand Up @@ -90,6 +100,8 @@ public function getExtensionConfig(string $name)
* @override
* This override ensures, that no definition of a service will be added while loading the config files of the bundles
* outside of the bundle configuration phase.
*
* @param array $definitions
*/
public function addDefinitions(array $definitions)
{
Expand All @@ -102,8 +114,13 @@ public function addDefinitions(array $definitions)
* @override
* This override ensures, that no service will be registered while loading the config files of the bundles
* outside of the bundle configuration phase.
*
* @param string $id
* @param string|null $class
*
* @return Definition|null
*/
public function register(string $id, string $class = null)
public function register(string $id, string $class = null): ?Definition
{
if (!$this->isBundleConfigMode) {
return parent::register($id, $class); // TODO: Change the autogenerated stub
Expand All @@ -116,8 +133,13 @@ public function register(string $id, string $class = null)
* @override
* This override ensures, that no service definition will be added while loading the config files of the bundles
* outside of the bundle configuration phase.
*
* @param string $id
* @param Definition $definition
*
* @return Definition|null
*/
public function setDefinition(string $id, Definition $definition)
public function setDefinition(string $id, Definition $definition): ?Definition
{
if (!$this->isBundleConfigMode) {
return parent::setDefinition($id, $definition); // TODO: Change the autogenerated stub
Expand All @@ -130,11 +152,16 @@ public function setDefinition(string $id, Definition $definition)
* @override
* This override ensures, that no service alias will be set while loading the config files of the bundles
* outside of the bundle configuration phase.
*
* @param string $alias
* @param $id
*
* @return string|Alias|null
*/
public function setAlias(string $alias, $id)
{
if (!$this->isBundleConfigMode) {
return parent::setAlias($alias, $id); // TODO: Change the autogenerated stub
return parent::setAlias($alias, $id);
}

return null;
Expand All @@ -144,11 +171,13 @@ public function setAlias(string $alias, $id)
* @override
* This override ensures, that no service definition will be registered while loading the config files of the bundles
* outside of the bundle configuration phase.
*
* @param array $definitions
*/
public function setDefinitions(array $definitions)
{
if (!$this->isBundleConfigMode) {
parent::setDefinitions($definitions); // TODO: Change the autogenerated stub
parent::setDefinitions($definitions);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/LocationAwareConfigLoadBundle/CustomDelegatingLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace CJW\CJWConfigProcessor\src\LocationAwareConfigLoadBundle;


use Exception;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolverInterface;

Expand Down Expand Up @@ -32,9 +33,11 @@ public function __construct(LoaderResolverInterface $resolver, CustomContainerBu
* @override
* This override ensures that everytime a resource is loaded (which is not a global pattern) the path to said resource is set
* in and known by the container.
*
* @param $resource
* @param string|null $type
* @throws \Exception
*
* @throws Exception
*/
public function load($resource, string $type = null)
{
Expand Down
2 changes: 2 additions & 0 deletions src/LocationAwareConfigLoadBundle/CustomGlobLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public function __construct(CustomContainerBuilder $container, FileLocatorInterf
* This override is basically a copy of the {@see GlobLoader} load function just with one key difference:
* It tracks the paths gathered by GlobResources and always relays that path before the loading process
* of the parameters and services begins.
*
* @param $resource
* @param string|null $type
*
* @throws FileLoaderImportCircularReferenceException
* @throws LoaderLoadException
*/
Expand Down
41 changes: 29 additions & 12 deletions src/LocationAwareConfigLoadBundle/CustomValueStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@
class CustomValueStorage
{

/** @var array This is an array of all locations that have been encountered during the loading process. */
/**
* @var array This is an array of all locations that have been encountered during the loading process.
*/
private static $encounteredLocations = [];

/** @var array An array of all parameters being loaded by the config loading process and the values accompanied by the paths they stem from. */
/**
* @var array An array of all parameters being loaded by the config loading process and the values accompanied by the paths they stem from.
*/
private static $parameterAndTheirLocations = [];

/** @var bool States whether new parameter values or paths can be added to the internal arrays or not. */
/**
* @var bool States whether new parameter values or paths can be added to the internal arrays or not.
*/
private static $allowWrite = true;

/** @var bool States whether the bundle config loading process has begun. */
/**
* @var bool States whether the bundle config loading process has begun.
*/
private static $bundleConfig = false;

/**
Expand All @@ -41,7 +49,8 @@ class CustomValueStorage
* @param mixed $value The value attached to both the parametername and then the given path as well. It is going to be added under path-key as an entry of the array.
* @param string $path The path (the origin) of the parameter value that is being set. It serves as a key under the parameter-key of the array.
*/
public static function addParameterOrLocation(string $parameterName, $value, string $path) {
public static function addParameterOrLocation(string $parameterName, $value, string $path): void
{
// Only if it is currently allowed to write, will the process even begin
if (self::$allowWrite) {
if (!in_array($path, self::$encounteredLocations)) {
Expand All @@ -66,7 +75,8 @@ public static function addParameterOrLocation(string $parameterName, $value, str
* It simply sets an internal boolean which then prohibits any parameters or paths / values to be set.
* In order to unlock the writing process, use {@see unlockParameters()}.
*/
public static function lockParameters() {
public static function lockParameters(): void
{
self::$allowWrite = false;
}

Expand All @@ -75,7 +85,8 @@ public static function lockParameters() {
* to the internal parameter-and-path storage. It sets the internal boolean to a different value
* than {@see lockParameters()}.
*/
public static function unlockParameters() {
public static function unlockParameters(): void
{
self::$allowWrite = true;
}

Expand All @@ -91,7 +102,8 @@ public static function unlockParameters() {
*
* @param bool $activate A boolean stating that the mode is either to be active (true) or not (false).
*/
public static function activateBundleConfigMode(bool $activate) {
public static function activateBundleConfigMode(bool $activate): void
{
self::$bundleConfig = $activate;
}

Expand All @@ -100,7 +112,8 @@ public static function activateBundleConfigMode(bool $activate) {
* bundleConfigMode and the lock-status of the class internally. This serves to allow a "fresh" start with the internal
* storage.
*/
public static function reset(): void {
public static function reset(): void
{
self::$parameterAndTheirLocations = [];
self::$bundleConfig = false;
self::$allowWrite = true;
Expand All @@ -112,7 +125,8 @@ public static function reset(): void {
*
* @return array Returns an array of parameters as super-keys, the locations as sub-keys and the values found at the paths as entries.
*/
public static function getParametersAndTheirLocations() {
public static function getParametersAndTheirLocations(): array
{
ksort(self::$parameterAndTheirLocations,SORT_STRING);
return self::$parameterAndTheirLocations;
}
Expand All @@ -123,17 +137,20 @@ public static function getParametersAndTheirLocations() {
*
* @return array Returns an array which is filled with all encountered locations during the configuration-loading-process.
*/
public static function getEncounteredLocations() {
public static function getEncounteredLocations(): array
{
return self::$encounteredLocations;
}

/**
* Allows a specific parameter to be retrieved from the internal storage of the class.
*
* @param string $parameterName The name of the parameter as string.
*
* @return array|null Returns the internal array with `$path => $value` pairs or null if the parameter is not present in the internal array.
*/
public static function getLocationsForSpecificParameter(string $parameterName) {
public static function getLocationsForSpecificParameter(string $parameterName): ?array
{
// Only if that parameter exists as a key in the array, will that parameters paths and values be returned, otherwise null
return isset(self::$parameterAndTheirLocations[$parameterName]) ?
self::$parameterAndTheirLocations[$parameterName] : null;
Expand Down
Loading

0 comments on commit e61d9ae

Please sign in to comment.