Skip to content

Commit

Permalink
Adaptations to allow location retrieval for Ibexa Platform 2.5 and Sy…
Browse files Browse the repository at this point in the history
…mfony 3.4

PHP:

- TwigTestDisplayService.php: Removed unused comment

- ConfigPathUtility.php: Merged two catch blocks into one, since the other one does not serve any purpose any more (which makes the code shorter and easier to read)

- CustomContainerBuilder.php: Removed all type hints in function signatures for scalar / fundamental types (like string, bool etc.), to make the function definitions compatible with its parent's functions (to allow the boot process to commence)

- CustomDelegatingLoader.php: Removed scalar type hints from function signatures for compatibility with the parent class

- CustomGlobLoader.php: Refactored comments for more readability and removed scalar type hints from function signatures

- LoadInitializer.php:

1. Added and refactored comments to make the code more readable (and the comments as well)

2. Added an overwrite for the getContainerClass-function to make sure the custom kernel is able to boot up (since in Symfony 3.4 the the boot process does not take the class of the kernel calling the process into account, leading to the same cached container for every kernel, prohibiting the custom kernel to boot)

- LocationAwareParameterBag.php: Removed scalar type hints from function signature (for the same reason as before)

- LocationRetrievalCoordinator.php:

1. Small refactorings for a better code structure and readability

2. Added boolean which determines whether the value of the cached item will be set even if it is present in the cache (this was done to prevent a deleted cached item to trigger a false cache hit and move past setting the cache item (which had already been deleted) again, leading to empty cached items)

- Utility.php:

1. Small refactorings for better code readability

2. Added documentation to the cacheContract replacement function of the Utility and added optional boolean to state whether the cached item will be overwritten no matter what
  • Loading branch information
JAC - Frederic Bauer committed Dec 15, 2020
1 parent 2fd1506 commit 418d8d6
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 37 deletions.
7 changes: 7 additions & 0 deletions Resources/doc/changelogs/CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CJW-Network ConfigProcessor Bundle 2.x changelog

## 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

* Improved some internal documentation

## 2.0 (11.12.2020)

* This changelog has been created to ship with the first full version of the bundle
Expand Down
10 changes: 0 additions & 10 deletions Services/TwigTestDisplayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,4 @@ public function getGlobals(): array
"cjw_param_location" => $this->parametersAndLocations?? [],
];
}

// /**
// * @inheritDoc
// */
// public function getFunctions()
// {
// return [
// new TwigFunction("getLocations",[LocationRetrievalCoordinator::class,"getParametersAndLocations"]),
// ];
// }
}
4 changes: 1 addition & 3 deletions src/LocationAwareConfigLoadBundle/ConfigPathUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ function () {

// Parse the manual path_config-file
self::getUserDefinedPaths();
} catch (InvalidArgumentException $e) {
self::$configPaths = [];
} catch (Exception $error) {
} catch (Exception $e) {
self::$configPaths = [];
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/LocationAwareConfigLoadBundle/CustomContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function setCurrentLocation(string $location) {
/**
* @override
*/
public function compile(bool $resolveEnvPlaceholders = false)
public function compile($resolveEnvPlaceholders = false)
{
// prior to the compilation starting, set the current location to a blanket "bundles", so that if no other paths can be found, it is at least
// known, that the parameters come from the bundle process.
Expand All @@ -63,7 +63,7 @@ public function compile(bool $resolveEnvPlaceholders = false)
* @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)
public function getExtensionConfig($name)
{
try {
// Get the class of the extension, then generate a Reflection class out of that, which allows finding the path to the file, then set that path
Expand Down Expand Up @@ -103,7 +103,7 @@ public function addDefinitions(array $definitions)
* This override ensures, that no service will be registered while loading the config files of the bundles
* outside of the bundle configuration phase.
*/
public function register(string $id, string $class = null)
public function register($id, $class = null)
{
if (!$this->isBundleConfigMode) {
return parent::register($id, $class); // TODO: Change the autogenerated stub
Expand All @@ -117,7 +117,7 @@ public function register(string $id, string $class = null)
* This override ensures, that no service definition will be added while loading the config files of the bundles
* outside of the bundle configuration phase.
*/
public function setDefinition(string $id, Definition $definition)
public function setDefinition($id, Definition $definition)
{
if (!$this->isBundleConfigMode) {
return parent::setDefinition($id, $definition); // TODO: Change the autogenerated stub
Expand All @@ -131,7 +131,7 @@ public function setDefinition(string $id, Definition $definition)
* This override ensures, that no service alias will be set while loading the config files of the bundles
* outside of the bundle configuration phase.
*/
public function setAlias(string $alias, $id)
public function setAlias($alias, $id)
{
if (!$this->isBundleConfigMode) {
return parent::setAlias($alias, $id); // TODO: Change the autogenerated stub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(LoaderResolverInterface $resolver, CustomContainerBu
* @param string|null $type
* @throws \Exception
*/
public function load($resource, string $type = null)
public function load($resource, $type = null)
{
// If the given resource is no glob pattern but instead something else and the resource is a string, the path will be given to the container.
if ($type !== "glob" && !is_object($type) && is_string($resource)) {
Expand Down
4 changes: 3 additions & 1 deletion src/LocationAwareConfigLoadBundle/CustomGlobLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ 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
*/
public function load($resource, string $type = null)
public function load($resource, $type = null)
{
// Typical load function of the GlobLoader as of Symfony 5.1.5
foreach ($this->glob($resource, false, $globResource) as $path => $info) {
Expand Down
28 changes: 26 additions & 2 deletions src/LocationAwareConfigLoadBundle/LoadInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getCacheDir(): string
*
* @return string Returns the determined project directory.
*/
public function getProjectDir()
public function getProjectDir(): string
{
if ($this->kernel) {
return $this->kernel->getProjectDir();
Expand All @@ -110,11 +110,25 @@ public function getProjectDir()
return $projectDir;
}

/**
* Since Symfony 3.4 does not take the kernel calling the container builder into account when booting up,
* this function is overwritten, so the container which is created during the boot process is unique to
* this custom kernel (so that it does boot in the first place).
*
* @return string Returns the name of the container to save
*/
protected function getContainerClass()
{
return "CJW_LoadInitializer_" . parent::getContainerClass();
}

/**
* @override
* Overrides the standard function of the kernel in order to ensure, that the right CustomContainerBuilder and CustomLoaders
* are being used for the config loading process.
*
* @param ContainerInterface $container
*
* @return CustomDelegatingLoader
*/
protected function getContainerLoader(ContainerInterface $container)
Expand Down Expand Up @@ -146,10 +160,20 @@ protected function getContainerBuilder()
return $customContainerBuilder;
}

/**
* @override
*
* Overrides the standard kernel function to configure the container in order to incorporate the custom paths
* that have been gathered during the custom load process.
*
* @param ContainerBuilder $container
* @param LoaderInterface $loader
*/
protected function configureContainer(
ContainerBuilder $container,
LoaderInterface $loader
): void {
): void
{
parent::configureContainer($container, $loader);

// After the original Symfony-Loading of specific routes, the custom routes, added in the configuration, are being parsed through
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function setCurrentLocation(string $location) {
* @param string $name The name of the parameter.
* @param mixed $value The value being set to the parameter.
*/
public function set(string $name, $value)
public function set($name, $value)
{
// Give the parameter, the value and the current location
CustomValueStorage::addParameterOrLocation($name,$value,$this->currentLocation);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php


namespace CJW\CJWConfigProcessor\src\LocationAwareConfigLoadBundle;


use CJW\CJWConfigProcessor\src\Utility\Utility;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
use Symfony\Component\Cache\Exception\CacheException;
use Symfony\Contracts\Cache\ItemInterface;

class LocationRetrievalCoordinator
{
Expand Down Expand Up @@ -62,6 +60,8 @@ public static function initializeCoordinator(): void {
}

try {
$enforceCacheOverwrite = false;

// If parameters are returned (meaning that the kernel has booted and thus new parameters could have entered), delete the parameters present
// also delete the processed parameters based on the previous parameters
if (
Expand All @@ -72,14 +72,19 @@ public static function initializeCoordinator(): void {
self::$cache->deleteItem("cjw_processed_param_objects");
self::$cache->deleteItem("cjw_processed_params");
self::$cache->deleteItem("cjw_processing_timestamp");
self::$cache->prune();
$enforceCacheOverwrite = true;
}

// Then store the presumably "new" parameters
self::$parametersAndLocations =
Utility::cacheContractGetOrSet("cjw_parameters_and_locations", self::$cache,
Utility::cacheContractGetOrSet(
"cjw_parameters_and_locations",
self::$cache,
function () {
return self::$parametersAndLocations;
}
},
$enforceCacheOverwrite
);
}catch (InvalidArgumentException $e) {
}
Expand Down
41 changes: 31 additions & 10 deletions src/Utility/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


use Exception;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\CacheItem;

class Utility
{
Expand All @@ -15,7 +15,8 @@ public static function removeUncommonParameters (
array $firstParameterList,
array $secondParameterList,
int $level = 0
) {
): array
{
$firstListKeys = array_keys($firstParameterList);
$secondListKeys = array_keys($secondParameterList);

Expand Down Expand Up @@ -54,7 +55,8 @@ public static function removeCommonParameters (
array $firstParameterList,
array $secondParameterList,
int $level = 0
) {
): array
{
$firstListKeys = array_keys($firstParameterList);
$secondListKeys = array_keys($secondParameterList);

Expand Down Expand Up @@ -94,7 +96,7 @@ public static function removeCommonParameters (
* @param array $array
* @return bool
*/
public static function has_string_keys(array $array)
public static function has_string_keys(array $array): bool
{
return count(
array_filter(
Expand All @@ -106,7 +108,8 @@ public static function has_string_keys(array $array)

public static function determinePureSiteAccesses(
array $processedParameterArray
): array {
): array
{
try {
$results =
$processedParameterArray["ezpublish"]["siteaccess"]["list"]["parameter_value"];
Expand All @@ -120,7 +123,8 @@ public static function determinePureSiteAccesses(

public static function determinePureSiteAccessGroups (
array $processedParameterArray
): array {
): array
{
try {
return $processedParameterArray["ezpublish"]["siteaccess"]["groups"]["parameter_value"];
} catch (Exception $error) {
Expand All @@ -131,7 +135,8 @@ public static function determinePureSiteAccessGroups (
public static function removeEntryThroughKeyList (
array $parameters,
array $keyList
): array {
): array
{
$key = reset($keyList);
array_splice($keyList,0,1);

Expand All @@ -155,7 +160,8 @@ public static function removeEntryThroughKeyList (
public static function removeSpecificKeySegment (
string $keySegment,
array $parametersToRemoveFrom
) {
): array
{
$result = $parametersToRemoveFrom;

foreach ($parametersToRemoveFrom as $key => $value) {
Expand All @@ -170,12 +176,27 @@ public static function removeSpecificKeySegment (
return $result;
}

/**
* Since Symfony 3.4 does not feature cache contracts, but instead only PSR-6-cache but the original
* functionality was built with cache contracts in mind, this is a replacement function for the cache
* contracts in Symfony 5 and above.
*
* @param string $cacheKey The key for the item that is supposed to be retrieved.
* @param AdapterInterface $cachePool The cache adapter used to store the item.
* @param callable $executeWhenItemNotSet A function to call, when the item could not be found in the cache.
* @param false $enforceSet Since the cache did not detect properly that some items had been deleted, here a boolean to enforce the item to be set to the given value.
*
* @return mixed Returns the cached value
*
* @throws InvalidArgumentException
*/
public static function cacheContractGetOrSet (
string $cacheKey,
AdapterInterface $cachePool,
callable $executeWhenItemNotSet
callable $executeWhenItemNotSet,
$enforceSet = false
) {
if (!$cachePool->hasItem($cacheKey)) {
if (!$cachePool->hasItem($cacheKey) || $enforceSet) {
$item = $cachePool->getItem($cacheKey);
$item->set($executeWhenItemNotSet());

Expand Down

0 comments on commit 418d8d6

Please sign in to comment.