cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,90 +1,81 @@
<?php
/*
* Copyright (C) 2016 Julien Fastré <julien.fastre@champs-libres.coop>
/**
* Chill is a software for social workers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\DependencyInjection\Widget;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Chill\MainBundle\DependencyInjection\Widget\AbstractWidgetsCompilerPass as WidgetsCompilerPass;
use Generator;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use function implode;
/**
* This trait allow to add automatic configuration for widget inside your config.
*
* Usage
*
* Usage
* ======
*
*
* 1. Register widget factories
* ----------------------------
*
*
* Add widget factories, using `setWidgetFactories`
*
* Example :
*
*
* Example :
*
* ```
* use Symfony\Component\DependencyInjection\ContainerBuilder;
* use Chill\MainBundle\DependencyInjection\Widget\AddWidgetConfigurationTrait;
*
* class MyConfig
*
* class MyConfig
* {
*
*
* use addWidgetConfigurationTrait;
*
*
* public function __construct(array $widgetFactories = array(), ContainerBuilder $container)
* {
* $this->setWidgetFactories($widgetFactories);
* // will be used on next step
* $this->container = $container;
* }
*
*
* }
* ```
*
*
*
*
*
*
* 2. add widget config to your config
* -----------------------------------
*
*
* ```
* use Symfony\Component\DependencyInjection\ContainerBuilder;
* use Chill\MainBundle\DependencyInjection\Widget\AddWidgetConfigurationTrait;
* use Symfony\Component\Config\Definition\Builder\TreeBuilder;
*
* class MyConfig
*
* class MyConfig
* {
*
*
* use addWidgetConfigurationTrait;
*
*
* private $container;
*
*
* public function __construct(array $widgetFactories = array(), ContainerBuilder $container)
* {
* $this->setWidgetFactories($widgetFactories);
* $this->container;
* }
*
*
* public function getConfigTreeBuilder()
* {
* $treeBuilder = new TreeBuilder();
* $root = $treeBuilder->root('my_root');
*
*
* $root->children()
* ->arrayNode('widgets')
* ->canBeDisabled()
@@ -93,15 +84,15 @@ use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
* ->end()
* ->end()
* ;
*
*
* return $treeBuilder;
* }
*
*
* }
* ```
*
* the above code will add to the config :
*
*
* the above code will add to the config :
*
* ```
* widgets:
* enabled: true
@@ -113,8 +104,6 @@ use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
* person_list:
* # options for the person_list
* ```
*
*
*/
trait AddWidgetConfigurationTrait
{
@@ -122,16 +111,7 @@ trait AddWidgetConfigurationTrait
* @param WidgetFactoryInterface[]
*/
private $widgetFactories;
/**
*
* @param WidgetFactoryInterface[] $widgetFactories
*/
public function setWidgetFactories(array $widgetFactories)
{
$this->widgetFactories = $widgetFactories;
}
/**
* @return WidgetFactoryInterface[]
*/
@@ -139,12 +119,20 @@ trait AddWidgetConfigurationTrait
{
return $this->widgetFactories;
}
/**
* @param WidgetFactoryInterface[] $widgetFactories
*/
public function setWidgetFactories(array $widgetFactories)
{
$this->widgetFactories = $widgetFactories;
}
/**
* add configuration nodes for the widget at the given place.
*
*
* @param type $place
* @param ContainerBuilder $containerBuilder
*
* @return type
*/
protected function addWidgetsConfiguration($place, ContainerBuilder $containerBuilder)
@@ -152,101 +140,104 @@ trait AddWidgetConfigurationTrait
$treeBuilder = new TreeBuilder($place);
$root = $treeBuilder->getRootNode($place)
->canBeUnset()
->info('register widgets on place "'.$place.'"');
->info('register widgets on place "' . $place . '"');
// if no childen, return the root
if (count(iterator_to_array($this->filterWidgetByPlace($place))) === 0) {
return $root;
}
$prototypeChildren = $root->prototype('array')->children();
$prototypeChildren
->floatNode(WidgetsCompilerPass::WIDGET_CONFIG_ORDER)
->isRequired()
->info("the ordering of the widget. May be a number with decimal")
->example("10.58")
->end()
->isRequired()
->info('the ordering of the widget. May be a number with decimal')
->example('10.58')
->end()
->scalarNode(WidgetsCompilerPass::WIDGET_CONFIG_ALIAS)
// this node is scalar: when the configuration is build, the
// tagged services are not available. But when the config reference
// is build, the services are avaialble => we add the possible aliases
// in the info.
->info("the widget alias (see your installed bundles config). "
. "Possible values are (maybe incomplete) : ".
\implode(", ", $this->getWidgetAliasesbyPlace($place, $containerBuilder)))
->isRequired()
->end()
;
->info('the widget alias (see your installed bundles config). '
. 'Possible values are (maybe incomplete) : ' .
implode(', ', $this->getWidgetAliasesbyPlace($place, $containerBuilder)))
->isRequired()
->end();
// adding the possible config on each widget under the widget_alias
foreach ($this->filterWidgetByPlace($place) as $factory) {
$builder = new TreeBuilder($factory->getWidgetAlias());
$widgetOptionsRoot = $builder->getRootNode($factory->getWidgetAlias());
$widgetOptionsRoot->canBeUnset()
->info(sprintf('the configuration for the widget "%s" (only required if this widget is set in widget_alias)',
$factory->getWidgetAlias()));
->info(sprintf(
'the configuration for the widget "%s" (only required if this widget is set in widget_alias)',
$factory->getWidgetAlias()
));
$factory->configureOptions($place, $widgetOptionsRoot->children());
$prototypeChildren->append($widgetOptionsRoot);
}
return $root;
}
/**
* get all widget factories for the given place.
*
* @param string $place
* @return \Generator a generator containing a widget factory
*
* @return Generator a generator containing a widget factory
*/
protected function filterWidgetByPlace($place)
{
foreach($this->widgetFactories as $factory) {
foreach ($this->widgetFactories as $factory) {
if (in_array($place, $factory->getAllowedPlaces())) {
yield $factory;
}
}
}
/**
* get the all possible aliases for the given place. This method
* search within service tags and widget factories
*
* **Note** that services are not available when the config is build: the whole
* search within service tags and widget factories.
*
* **Note** that services are not available when the config is build: the whole
* aliases will be checked in compiler pass, or when the command
* `config:dump-reference` is runned.
*
*
* @param type $place
* @param ContainerBuilder $containerBuilder
* @return type
*
* @throws InvalidConfigurationException if a service's tag does not have the "alias" key
*
* @return type
*/
protected function getWidgetAliasesbyPlace($place, ContainerBuilder $containerBuilder)
{
$result = array();
$result = [];
foreach ($this->filterWidgetByPlace($place) as $factory) {
$result[] = $factory->getWidgetAlias();
}
// append the aliases added without factory
foreach ($containerBuilder
->findTaggedServiceIds(WidgetsCompilerPass::WIDGET_SERVICE_TAG_NAME)
->findTaggedServiceIds(WidgetsCompilerPass::WIDGET_SERVICE_TAG_NAME)
as $serviceId => $tags) {
foreach ($tags as $tag) {
// throw an error if no alias in definition
if (!array_key_exists(WidgetsCompilerPass::WIDGET_SERVICE_TAG_ALIAS, $tag)) {
throw new InvalidConfigurationException(sprintf(
"The service with id %s does not have any %d key",
$serviceId,
WidgetsCompilerPass::WIDGET_SERVICE_TAG_ALIAS
));
}
// add the key to the possible results
$result[] = $tag[WidgetsCompilerPass::WIDGET_SERVICE_TAG_ALIAS];
foreach ($tags as $tag) {
// throw an error if no alias in definition
if (!array_key_exists(WidgetsCompilerPass::WIDGET_SERVICE_TAG_ALIAS, $tag)) {
throw new InvalidConfigurationException(sprintf(
'The service with id %s does not have any %d key',
$serviceId,
WidgetsCompilerPass::WIDGET_SERVICE_TAG_ALIAS
));
}
// add the key to the possible results
$result[] = $tag[WidgetsCompilerPass::WIDGET_SERVICE_TAG_ALIAS];
}
}
return $result;
}
}
}