Skip to content

Commit

Permalink
Commit to 2.x version of the bundle based on the equivalent commits t…
Browse files Browse the repository at this point in the history
…o the 3.x version:

Commit concerns issues #5 and #6 ,#8 and #9

PHP:

- ConfigLocationOutputCommand.php: Added Symfony console command to display the configuration paths (as determined by the bundle) of the parameters.

- ProcessedConfigOutputCommand.php: Added styling to the command output to provide more information and feedback to the user and updated its documentation.

- ConfigProcessController.php: Removed unnecessary comments from the controller.

- CJWConfigProcessorExtension.php: Modified config so that the environmental variables are turned on by default.

- CustomParamProcessor.php: Small documentation fix.

Config:

- services.yml: Added new console command as Symfony service.

And the other commit:

PHP:

- ProcessedConfigOutputCommand.php: Added this new console command to enable the processed config to also be accessed via the console and not only the frontend of the bundle

- ConfigProcessController.php: Added the environmental parameters view route and controller action and fixed bug with the bundle config not working properly, when the favourites feature was turned off

- CJWConfigProcessorExtension.php and Configuration.php: Added new configuration for the environmental variable display

- LeftSideBarMenuBuilder.php: Added new left sidebar button to access the dedicated env view

- CustomParamProcessor.php: Added setter for the active site accesses to filter for to allow more dynamic work with the processor

Config:

- routing.yaml and services.yml: Added the env view as route and the console command as a service

Documentation:

Updates to the documentation across the board for more readability and more detailed information. Also added documentation for the new view

CSS:

- parameter_display.css: Small tweak for better display of inline values

Twig:

- param_view_environment.html.twig: Added dedicated template for env parameters view
  • Loading branch information
NorthernSeaCharting authored and JAC - Frederic Bauer committed Jan 7, 2021
1 parent d410e72 commit 6557f4d
Show file tree
Hide file tree
Showing 17 changed files with 655 additions and 24 deletions.
122 changes: 122 additions & 0 deletions Command/ConfigLocationOutputCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php


namespace CJW\CJWConfigProcessor\Command;


use CJW\CJWConfigProcessor\src\LocationAwareConfigLoadBundle\LocationRetrievalCoordinator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class ConfigLocationOutputCommand extends Command
{
protected static $defaultName = "cjw:output-locations";

/**
* @override
*
* Configures the command and the parameters / options that can be set for it.
*/
protected function configure()
{
$this
->setName(self::$defaultName)
->setDescription("Displays the determined config paths (parameter origins) for the Symfony application.")
->setHelp(<<<'EOD'
This console command allows a user to access the a list of all paths (leading to files where config parameters have
either been set or overwritten) for the configuration of the Symfony application the bundle was able to determine.
The following options can be set for the command, but these are purely optional:
--paramname or -p: If a specific parameter name is given (i.e. "ezsettings.default.api_keys"), only paths for that
specific parameter are displayed (excluding every other parametername). The name does have to be
exact and if the option is omitted, then every found path is displayed.
To better read and format the output it is advised to pipe the output of this command to "LESS", if you are using an
ubuntu operating system.
Example: "php bin/console cjw:output-locations | less"
Then you can scroll more easily through the output and the output is present in an other context that can be quitted
with "q", so that the console is not spammed with the command output. Then you can also search something by typing "/"
and then the search word + enter key.
EOD
)
->addOption(
"paramname",
"p",
InputOption::VALUE_OPTIONAL,
"Giving a parametername will filter the list for that specific parameter and only display paths belonging to that parameter",
false
);

}

/**
* @override
* Controls the command execution.
*
* @param InputInterface $input The input the user can provide to the command.
* @param OutputInterface $output Controls the output that is supposed to be written out to the user.
*
* @return int Returns the execution status code.
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$ioStyler = new SymfonyStyle($input, $output);
$filterParameters = $input->getOption("paramname");

if ($filterParameters) {
$parametersAndPaths = LocationRetrievalCoordinator::getParameterLocations($filterParameters);
} else {
$parametersAndPaths = LocationRetrievalCoordinator::getParametersAndLocations();
}

$ioStyler->note([
"This command will now run with the following options:",
"Parameter Filter: " . $filterParameters ?? "none",
]);

if ($parametersAndPaths && $this->outputArray($output, $parametersAndPaths)) {
$ioStyler->newLine();
$ioStyler->success("Command ran successfully.");
} else {
$ioStyler->error("No parameters could be found for this option.");
}

return 0;
}

/**
* Builds the string output for the command. Handles hierarchical, multi dimensional arrays.
*
* @param OutputInterface $output The interface used to output the contents of the array.
* @param array $parameters The array to be output.
* @param int $indents The number of indents to be added in front of the output lines.
*
* @return bool Returns boolean stating whether parameters could successfully be found and output or not.
*/
private function outputArray(OutputInterface $output, array $parameters, int $indents = 0): bool
{
if (count($parameters) === 0) {
return false;
}

foreach ($parameters as $key => $parameter) {
$key = str_pad($key, $indents + strlen($key), " ", STR_PAD_LEFT);

$output->write($key . ": ");
if (is_array($parameter)) {
if (count($parameter) > 0) {
$output->write(str_repeat(" ", $indents) . "\n");
$this->outputArray($output, $parameter, $indents + 4);
$output->write(str_repeat(" ", $indents) . "\n");
} else {
$output->writeln(" ");
}
} else {
$output->writeln($parameter);
}
}

return true;
}
}
177 changes: 177 additions & 0 deletions Command/ProcessedConfigOutputCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace CJW\CJWConfigProcessor\Command;

use CJW\CJWConfigProcessor\src\ConfigProcessorBundle\ConfigProcessCoordinator;
use CJW\CJWConfigProcessor\src\ConfigProcessorBundle\CustomParamProcessor;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Class ProcessedConfigOutputCommand serves as a Symfony console command to display the processed configuration both
* on its own but also within a site access context and or filtered to specific branches.
*
* @package CJW\CJWConfigProcessor\Command
*/
class ProcessedConfigOutputCommand extends Command
{
protected static $defaultName = "cjw:output-config";

/**
* @var CustomParamProcessor Required to filter the configuration for specific, given parameters.
*/
private $customParameterProcessor;

public function __construct(ContainerInterface $symContainer, ConfigResolverInterface $ezConfigResolver, RequestStack $symRequestStack)
{
ConfigProcessCoordinator::initializeCoordinator($symContainer,$ezConfigResolver,$symRequestStack);
$this->customParameterProcessor = new CustomParamProcessor($symContainer);

parent::__construct();
}

/**
* @override
*
* Used to configure the command.
*/
protected function configure()
{
$this
->setName(self::$defaultName)
->setDescription("Displays the processed config of the symfony application.")
->setHelp(<<<'EOD'
This console command allows outputting the configuration made by the bundle to the console with a few options
that can be used to customize the output. The following options can be set, but they are purely optional:
--paramname or -p: If a specific parameter name or segment is given (i.e. "ezpublish" or "ezpublish.default.siteaccess"),
only the corresponding section of the processed configuration will be displayed. To input a specific
parameter name, simply add it after the option with a "=".
(i.e. "php bin/console cjw:output-config --paramname=param_name").
--siteaccess-context or -s:
To specify a specific site access context under which to view the parameter, simply add the context after
the option itself (i.e. "-s admin")
If the site access and the parameter name option are given at the same time, the filtered and narrowed list will be
viewed under site access context (not the complete list).
To better read and format the output it is advised to pipe the output of this command to "LESS", if you are using an
ubuntu operating system.
Example: "php bin/console cjw:output-config | less"
Then you can scroll more easily through the output and the output is present in an other context that can be quitted
with "q", so that the console is not spammed with the command output. Then you can also search something by typing "/"
and then the search word + enter key.
EOD
)
// TODO: Turn paramname into an array, so that multiple branches can be filtered for.
->addOption(
"paramname",
"p",
InputOption::VALUE_OPTIONAL,
"Narrow the list down to a specific branch or parameter. Simply state the parameter key or segment to filter for.",
false,
)
->addOption(
"siteaccess-context",
"s",
InputOption::VALUE_OPTIONAL,
"Define the site access context under which the config should be displayed.",
false,
);
}

/**
* @override
* Controls the commands execution.
*
* @param InputInterface $input The input the user can provide to the command.
* @param OutputInterface $output Controls the output that is supposed to be written out to the user.
*
* @return int Returns the execution status code.
*
* @throws InvalidArgumentException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$ioStyler = new SymfonyStyle($input, $output);
$siteAccessContext = $input->getOption("siteaccess-context");
$filterParameters = $input->getOption("paramname");

$processedParameters = ConfigProcessCoordinator::getProcessedParameters();

if ($filterParameters) {
$processedParameters = $this->customParameterProcessor->getCustomParameters([$filterParameters], $processedParameters);
}

if ($siteAccessContext) {
$siteAccess = $siteAccessContext;

if (!$filterParameters) {
$processedParameters = ConfigProcessCoordinator::getParametersForSiteAccess($siteAccess);
} else {
$siteAccess = ConfigProcessCoordinator::getSiteAccessListForController($siteAccess);
$this->customParameterProcessor->setSiteAccessList($siteAccess);
$processedParameters = $this->customParameterProcessor->scanAndEditForSiteAccessDependency($processedParameters);
}
}

$ioStyler->note([
"The command will run with the following options:",
"SiteAccess: ". $siteAccessContext,
"Parameter filter: ". $filterParameters,
]);

if ($processedParameters && $this->outputArray($output,$processedParameters)) {
$ioStyler->success("Command ran successfully.");
} else {
$ioStyler->error("No parameters could be found for these options.");
}

return 0;
}

/**
* Builds the string output for the command. Handles hierarchical, multi dimensional arrays.
*
* @param OutputInterface $output The interface used to output the contents of the array.
* @param array $parameters The array to be output.
* @param int $indents The number of indents to be added in front of the output lines.
*
* @return bool Returns boolean stating whether parameters could successfully be found and output or not.
*/
private function outputArray(OutputInterface $output, array $parameters, $indents = 0)
{
if (count($parameters) === 0) {
return false;
}

foreach ($parameters as $key => $parameter) {
$key = str_pad($key,$indents+strlen($key), " ", STR_PAD_LEFT);

$output->write($key.": ");
if (is_array($parameter)) {
if ( count($parameter) > 0) {
$output->write(str_repeat(" ", $indents)."\n");
$this->outputArray($output,$parameter, $indents+4);
$output->write(str_repeat(" ", $indents)."\n");
} else {
$output->writeln(" ");
}
} else {
$output->writeln($parameter);
}
}

return true;
}
}
31 changes: 24 additions & 7 deletions Controller/ConfigProcessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ public function __construct (ContainerInterface $symContainer, ConfigResolverInt
{
$this->container = $symContainer;
ConfigProcessCoordinator::initializeCoordinator($symContainer,$ezConfigResolver,$symRequestStack);

if (
$this->container->getParameter("cjw.favourite_parameters.allow") === true ||
$this->container->getParameter("cjw.custom_site_access_parameters.active") === true
) {
FavouritesParamCoordinator::initialize($this->container);
}
FavouritesParamCoordinator::initialize($this->container);

$this->showFavouritesOutsideDedicatedView =
$this->container->getParameter("cjw.favourite_parameters.display_everywhere");
Expand Down Expand Up @@ -372,6 +366,29 @@ public function downloadParameterListAsTextFile($downloadDescriptor): BinaryFile
return $response;
}

public function getEnvironmentalVariables ()
{
try {
$lastUpdated = ConfigProcessCoordinator::getTimeOfLastUpdate();

$envVar = [];

if ($this->container->getParameter("cjw.env_variables.allow") === true) {
$envVar = $_ENV;
}

return $this->render(
"@CJWConfigProcessor/full/param_view_environment.html.twig",
[
"parameterList" => $envVar,
"lastUpdated" => $lastUpdated,
]
);
} catch (Exception $error) {
throw new HttpException(500, "Something went wrong while trying to gather the required parameters.");
}
}

/**
* Helper function to determine the specific parameters for both given site access contexts at the same time.
* It returns the found parameters in an array in which the first entry marks all the parameters for the first
Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/CJWConfigProcessorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function load(array $configs, ContainerBuilder $container)

$this->handleCustomParamConfig($config, $container);
$this->handleFavouriteParamConfig($config, $container);

if (isset($config["env_variables"]["allow"])) {
$container->setParameter("cjw.env_variables.allow",$config["env_variables"]["allow"]);
} else {
$container->setParameter("cjw.env_variables.allow", true);
}
}

/**
Expand Down
15 changes: 13 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->arrayNode("custom_site_access_parameters")
->info("For configuring the custom parameters to be added to the site access view")
->children()
->booleanNode("allow")
->info("Describes whether custom parameters are allowed and active in the bundle or not.")
->defaultFalse()
->end()
->booleanNode("scan_parameters")
Expand All @@ -41,11 +43,11 @@ public function getConfigTreeBuilder()
->info("Handles all things regarding favourite parameters.")
->children()
->booleanNode("allow")
->info("Are favourite parameters allowed or shouldn't there be any mechanisms for it.")
->info("Configures whether favourite parameters are allowed at all.")
->defaultFalse()
->end()
->booleanNode("display_everywhere")
->info("Should the favourites be displayed outside of the dedicated view too or not.")
->info("Describes whether the favourites should be displayed outside of their dedicated view, currently of no use.")
->defaultFalse()
->end()
->booleanNode("scan_parameters")
Expand All @@ -61,6 +63,15 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode("env_variables")
->info("For configuring the environment variable display")
->children()
->booleanNode("allow")
->info("Determines whether the feature is enabled in the bundle or not.")
->defaultTrue()
->end()
->end()
->end()
->end();

return $treeBuilder;
Expand Down
Loading

0 comments on commit 6557f4d

Please sign in to comment.