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-2.x.md: Added the new improvement introduced by this commit.

- README.md: Missing feature "Location Retrieval" entered back into README, since the feature does now work again

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.

- 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 66e038e commit 386f1ca
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 99 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ with the following functionality (excerpt):
- **Limit** the comparison to common or uncommon parameters of the lists
- **Search** for specific keys or values in the parameter list
- **Mark** parameters as favourites and view them in a dedicated view
- **Get** location info about the parameters (which files do they appear in and with what value)
- **Download** a file representation of the parameter lists
- **And** more

Expand Down
16 changes: 14 additions & 2 deletions Resources/doc/changelogs/CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

## 2.0.1 (xx.12.2020)

* Adapted the custom kernel boot process to make the location retrieval functionality
available in Symfony 3.4 and Ibexa Platform 2.5
* Adapted the custom kernel boot process to make the location retrieval functionality
available in Symfony 3.4 and Ibexa Platform 2.5

* Fixed that changes to the favourite parameter list via the frontend would be ignored

* Fixed error in synchronous scrolling, where unique nodes would not be added to the other list (when they were
the first node of the list)

* Changed differences highlighting via the url, to only start highlighting, when the entire page is already loaded and done
with the other javascripts

* Improved some internal documentation

* 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.

## 2.0 (11.12.2020)

* This changelog has been created to ship with the first full version of the bundle
Expand Down
75 changes: 49 additions & 26 deletions src/LocationAwareConfigLoadBundle/ConfigPathUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,47 @@
class ConfigPathUtility
{

/** @var string The assortment of file extensions which can be used to configure symfony. */
/**
* @var string The assortment of file extensions which can be used to configure symfony.
*/
private static $configExtensions = "";

/** @var array Stores all added configuration paths */
/**
* @var array Stores all added configuration paths
*/
private static $configPaths = [];

/** @var PhpFilesAdapter A cache for the routes that have been determined throughout the previous loading processes. */
/**
* @var PhpFilesAdapter A cache for the routes that have been determined throughout the previous loading processes.
*/
private static $configPathCache;

/** @var bool States whether it has been tried to retrieve the existing paths from the cache already / the cache has been initialised. */
/**
* @var bool States whether it has been tried to retrieve the existing paths from the cache already / the cache has been initialised.
*/
private static $cacheInitialized = false;

/** @var bool States whether there has been a change in paths (this only occurs through adding a path (for now)). */
/**
* @var bool States whether there has been a change in paths (this only occurs through adding a path (for now)).
*/
private static $pathsChanged = false;

/** @var bool This boolean states whether there has been a change to the paths that warrants the kernel and thereby load process to be restarted to include the newly found paths. */
/**
* @var bool This boolean states whether there has been a change to the paths that warrants the kernel and thereby load process to be restarted to include the newly found paths.
*/
private static $restartLoadProcess = false;

/** @var string The directory in which to cache all the routes (in order to prevent the cache from being stored only temporarily) */
/**
* @var string The directory in which to cache all the routes (in order to prevent the cache from being stored only temporarily)
*/
private static $cacheDir = null;

/**
* Serves to set up and initialise all major internal attributes in order to allow the class to function properly.
* It initiates the cache (if it hasn't already), retrieves the routes from the cache, parses the manually defined routes
* and sets the internal boolean attributes to their initial value.
*/
public static function initializePathUtility(): void
public static function initializePathUtility()
{
if (!self::$cacheInitialized) {
// If the cache has not yet been instantiated
Expand Down Expand Up @@ -75,7 +89,7 @@ function () {

// Parse the manual path_config-file
self::getUserDefinedPaths();
} catch (Exception $e) {
} catch (InvalidArgumentException | Exception $e) {
self::$configPaths = [];
}
}
Expand All @@ -90,27 +104,30 @@ function () {
* <br> But, paths which point to a file / directory which does not exist, are not added to the paths list.
*
* @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)
{
$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 @@ -120,7 +137,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 @@ -139,7 +157,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->deleteItem("cjw_config_paths");
Expand Down Expand Up @@ -197,7 +216,9 @@ public static function setConfigExtensions(string $configExtensions): void
}

/**
* @param string $cacheDir
* Sets the cache directory for this class of the bundle, based on the general cache path of the installation.
*
* @param string $cacheDir The blank, standard cache path of the project.
*/
public static function setCacheDir(string $cacheDir): void
{
Expand Down Expand Up @@ -238,9 +259,11 @@ private static function getUserDefinedPaths(): void
* Checks the user defined paths for any kind of errors with regards to the definition of said paths.
*
* @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
37 changes: 35 additions & 2 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 All @@ -19,6 +20,9 @@
*/
class CustomContainerBuilder extends ContainerBuilder
{
/**
* @var bool Boolean which determines whether the bundle configuration mode is active or not.
*/
private $isBundleConfigMode;

public function __construct()
Expand All @@ -33,14 +37,23 @@ 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($resolveEnvPlaceholders = false)
{
Expand All @@ -61,6 +74,7 @@ public function compile($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($name)
Expand Down Expand Up @@ -90,6 +104,8 @@ public function getExtensionConfig($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,6 +118,11 @@ 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($id, $class = null)
{
Expand All @@ -116,6 +137,11 @@ public function register($id, $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($id, Definition $definition)
{
Expand All @@ -130,6 +156,11 @@ public function setDefinition($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($alias, $id)
{
Expand All @@ -139,11 +170,13 @@ public function setAlias($alias, $id)

return null;
}

/**
* @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)
{
Expand Down
9 changes: 7 additions & 2 deletions 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 All @@ -16,7 +17,9 @@
class CustomDelegatingLoader extends DelegatingLoader
{

/** @var CustomContainerBuilder A container builder which serves to build the container while keeping track of the files used to do so. */
/**
* @var CustomContainerBuilder A container builder which serves to build the container while keeping track of the files used to do so.
*/
private $container;

public function __construct(LoaderResolverInterface $resolver, CustomContainerBuilder $containerBuilder)
Expand All @@ -32,9 +35,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, $type = null)
{
Expand Down
Loading

0 comments on commit 386f1ca

Please sign in to comment.