Skip to content

Commit

Permalink
Commit concerns issues #5 and #6 ,#8 and #9
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NorthernSeaCharting authored and JAC - Frederic Bauer committed Jan 7, 2021
1 parent 21b863c commit 13c6ee6
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 20 deletions.
133 changes: 133 additions & 0 deletions Command/ConfigLocationOutputCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?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 is a command to display all recorded locations from which parameters of the
* configuration have been set.
*
* @package CJW\CJWConfigProcessor\Command
*/
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 self::SUCCESS;
}

/**
* 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;
}

}
43 changes: 33 additions & 10 deletions Command/ProcessedConfigOutputCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;

Expand Down Expand Up @@ -60,9 +61,18 @@ protected function configure(): void
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 fltered for.
// TODO: Turn paramname into an array, so that multiple branches can be filtered for.
->addOption(
"paramname",
"p",
Expand All @@ -80,9 +90,10 @@ protected function configure(): void
}

/**
* @override
* Controls the commands execution.
*
* @param InputInterface $input The input the user can give to the command.
* @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.
Expand All @@ -91,6 +102,7 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$ioStyler = new SymfonyStyle($input, $output);
$siteAccessContext = $input->getOption("siteaccess-context");
$filterParameters = $input->getOption("paramname");

Expand All @@ -106,15 +118,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!$filterParameters) {
$processedParameters = ConfigProcessCoordinator::getParametersForSiteAccess($siteAccess);
} else {
$this->customParameterProcessor->setSiteAccessList([$siteAccess]);
$siteAccess = ConfigProcessCoordinator::getSiteAccessListForController($siteAccess);
$this->customParameterProcessor->setSiteAccessList($siteAccess);
$processedParameters = $this->customParameterProcessor->scanAndEditForSiteAccessDependency($processedParameters);
}
}

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

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

return self::SUCCESS;
}

/**
Expand All @@ -123,12 +145,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* @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): void
private function outputArray(OutputInterface $output, array $parameters, int $indents = 0): bool
{
if (count($parameters) === 0) {
$output->writeln("No parameters could be found for these options.");
return;
return false;
}

foreach ($parameters as $key => $parameter) {
Expand All @@ -137,10 +160,8 @@ private function outputArray(OutputInterface $output, array $parameters, int $in
$output->write($key.": ");
if (is_array($parameter)) {
if ( count($parameter) > 0) {
// $output->write("\n".str_repeat(" ", $indents)."["."\n");
$output->write(str_repeat(" ", $indents)."\n");
$this->outputArray($output,$parameter, $indents+4);
// $output->write(str_repeat(" ", $indents)."]"."\n");
$output->write(str_repeat(" ", $indents)."\n");
} else {
$output->writeln(" ");
Expand All @@ -149,5 +170,7 @@ private function outputArray(OutputInterface $output, array $parameters, int $in
$output->writeln($parameter);
}
}

return true;
}
}
8 changes: 1 addition & 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
2 changes: 1 addition & 1 deletion DependencyInjection/CJWConfigProcessorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function load(array $configs, ContainerBuilder $container)
if (isset($config["env_variables"]["allow"])) {
$container->setParameter("cjw.env_variables.allow",$config["env_variables"]["allow"]);
} else {
$container->setParameter("cjw.env_variables.allow", false);
$container->setParameter("cjw.env_variables.allow", true);
}
}

Expand Down
5 changes: 5 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ services:
[ "@service_container", "@ezpublish.config.resolver", "@request_stack" ]
tags:
- { name: console.command }

cjw_config_processing.command.output_locations:
class: CJW\CJWConfigProcessor\Command\ConfigLocationOutputCommand
tags:
- { name: console.command }
4 changes: 2 additions & 2 deletions src/ConfigProcessorBundle/CustomParamProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getCustomParameters (array $customParameterKeys, array $processe

/**
* Takes a list of parameter keys (as strings) and checks them for any potential site access segments within the
* keys. If such segments are found, then the parameter and that segment will be redone for every possible
* keys. If such segments are found, then the parameter and that segment will be recreated for every possible
* site access of the installation and added to the original list of keys.
*
* @param array $keysToBeProcessed Array of (string) keys of parameters.
Expand Down Expand Up @@ -136,7 +136,7 @@ public function replacePotentialSiteAccessParts (array $keysToBeProcessed): arra
*
* <br>Example: When searching for test.admin.parameter, there might not be a value for site access admin, then it could
* be set under default, global, admin_group or any other site access from that hierarchy and so the value of
* the highest site access in that hierarchy is determined (global before any other, then the group and lasty default)
* the highest site access in that hierarchy is determined (global before any other, then the group and lastly default).
*
* @param array $parametersToBeProcessed Associative, hierarchical array of parameter keys for which to determine the values.
*
Expand Down

0 comments on commit 13c6ee6

Please sign in to comment.