mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-06 06:44:59 +00:00
apply more cs rules for php-cs
This commit is contained in:
@@ -13,20 +13,11 @@ namespace Chill\MainBundle\DependencyInjection\Widget;
|
||||
|
||||
use Chill\MainBundle\DependencyInjection\Widget\Factory\WidgetFactoryInterface;
|
||||
use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
|
||||
use LengthException;
|
||||
use LogicException;
|
||||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use UnexpectedValueException;
|
||||
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
use function get_class;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
|
||||
/**
|
||||
* Compile the configurations and inject required service into container.
|
||||
@@ -122,11 +113,11 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
/**
|
||||
* process the configuration and the container to add the widget available.
|
||||
*
|
||||
* @param string $extension the extension of your bundle
|
||||
* @param string $extension the extension of your bundle
|
||||
* @param string $containerWidgetConfigParameterName the key under which we can use the widget configuration
|
||||
*
|
||||
* @throws LogicException
|
||||
* @throws UnexpectedValueException if the given extension does not implement HasWidgetExtensionInterface
|
||||
* @throws \LogicException
|
||||
* @throws \UnexpectedValueException if the given extension does not implement HasWidgetExtensionInterface
|
||||
*/
|
||||
public function doProcess(
|
||||
ContainerBuilder $container,
|
||||
@@ -134,8 +125,7 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
$containerWidgetConfigParameterName
|
||||
) {
|
||||
if (!$container->hasDefinition(self::WIDGET_MANAGER)) {
|
||||
throw new LogicException('the service ' . self::WIDGET_MANAGER . ' should' .
|
||||
' be present. It is required by ' . self::class);
|
||||
throw new \LogicException('the service '.self::WIDGET_MANAGER.' should be present. It is required by '.self::class);
|
||||
}
|
||||
|
||||
$managerDefinition = $container->getDefinition(self::WIDGET_MANAGER);
|
||||
@@ -145,14 +135,7 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
$extensionClass = $container->getExtension($extension);
|
||||
// throw an error if extension does not implement HasWidgetFactoriesExtensionInterface
|
||||
if (!$extensionClass instanceof HasWidgetFactoriesExtensionInterface) {
|
||||
throw new UnexpectedValueException(sprintf(
|
||||
'The extension for %s '
|
||||
. 'do not implements %s. You should implement %s on %s',
|
||||
$extension,
|
||||
HasWidgetFactoriesExtensionInterface::class,
|
||||
HasWidgetFactoriesExtensionInterface::class,
|
||||
$extensionClass::class
|
||||
));
|
||||
throw new \UnexpectedValueException(sprintf('The extension for %s do not implements %s. You should implement %s on %s', $extension, HasWidgetFactoriesExtensionInterface::class, HasWidgetFactoriesExtensionInterface::class, $extensionClass::class));
|
||||
}
|
||||
|
||||
$this->widgetFactories = $extensionClass->getWidgetFactories();
|
||||
@@ -168,18 +151,13 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
foreach ($widgets as $param) {
|
||||
$alias = $param[self::WIDGET_CONFIG_ALIAS];
|
||||
// check that the service exists
|
||||
if (!array_key_exists($alias, $this->widgetServices)) {
|
||||
throw new InvalidConfigurationException(sprintf('The alias %s' .
|
||||
' is not defined.', $alias));
|
||||
if (!\array_key_exists($alias, $this->widgetServices)) {
|
||||
throw new InvalidConfigurationException(sprintf('The alias %s is not defined.', $alias));
|
||||
}
|
||||
|
||||
// check that the widget is allowed at this place
|
||||
if (!$this->isPlaceAllowedForWidget($place, $alias, $container)) {
|
||||
throw new InvalidConfigurationException(sprintf(
|
||||
'The widget with alias %s is not allowed at place %s',
|
||||
$alias,
|
||||
$place
|
||||
));
|
||||
throw new InvalidConfigurationException(sprintf('The widget with alias %s is not allowed at place %s', $alias, $place));
|
||||
}
|
||||
|
||||
// get the order, eventually corrected
|
||||
@@ -242,23 +220,17 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
foreach ($attrs as $attr) {
|
||||
// check the alias is set
|
||||
if (!isset($attr[self::WIDGET_SERVICE_TAG_ALIAS])) {
|
||||
throw new InvalidConfigurationException('you should add an ' . self::WIDGET_SERVICE_TAG_ALIAS .
|
||||
' key on the service ' . $id);
|
||||
throw new InvalidConfigurationException('you should add an '.self::WIDGET_SERVICE_TAG_ALIAS.' key on the service '.$id);
|
||||
}
|
||||
|
||||
// check the place is set
|
||||
if (!isset($attr[self::WIDGET_SERVICE_TAG_PLACES])) {
|
||||
throw new InvalidConfigurationException(sprintf(
|
||||
'You should add a %s key on the service %s',
|
||||
self::WIDGET_SERVICE_TAG_PLACES,
|
||||
$id
|
||||
));
|
||||
throw new InvalidConfigurationException(sprintf('You should add a %s key on the service %s', self::WIDGET_SERVICE_TAG_PLACES, $id));
|
||||
}
|
||||
|
||||
// check the alias does not exists yet
|
||||
if (array_key_exists($attr[self::WIDGET_SERVICE_TAG_ALIAS], $this->widgetServices)) {
|
||||
throw new InvalidArgumentException('a service has already be defined with the ' .
|
||||
self::WIDGET_SERVICE_TAG_ALIAS . ' ' . $attr[self::WIDGET_SERVICE_TAG_ALIAS]);
|
||||
if (\array_key_exists($attr[self::WIDGET_SERVICE_TAG_ALIAS], $this->widgetServices)) {
|
||||
throw new InvalidArgumentException('a service has already be defined with the '.self::WIDGET_SERVICE_TAG_ALIAS.' '.$attr[self::WIDGET_SERVICE_TAG_ALIAS]);
|
||||
}
|
||||
|
||||
// register the service as available
|
||||
@@ -273,29 +245,21 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
|
||||
// check the alias is not empty
|
||||
if (empty($alias)) {
|
||||
throw new LogicException(sprintf(
|
||||
'the widget factory %s returns an empty alias',
|
||||
$factory::class
|
||||
));
|
||||
throw new \LogicException(sprintf('the widget factory %s returns an empty alias', $factory::class));
|
||||
}
|
||||
|
||||
// check the places are not empty
|
||||
if (!is_array($factory->getAllowedPlaces())) {
|
||||
throw new UnexpectedValueException("the method 'getAllowedPlaces' "
|
||||
. 'should return a non-empty array. Unexpected value on ' .
|
||||
$factory::class);
|
||||
if (!\is_array($factory->getAllowedPlaces())) {
|
||||
throw new \UnexpectedValueException("the method 'getAllowedPlaces' ".'should return a non-empty array. Unexpected value on '.$factory::class);
|
||||
}
|
||||
|
||||
if (count($factory->getAllowedPlaces()) === 0) {
|
||||
throw new LengthException("The method 'getAllowedPlaces' should "
|
||||
. 'return a non-empty array, but returned 0 elements on ' .
|
||||
$factory::class . '::getAllowedPlaces()');
|
||||
if (0 === \count($factory->getAllowedPlaces())) {
|
||||
throw new \LengthException("The method 'getAllowedPlaces' should ".'return a non-empty array, but returned 0 elements on '.$factory::class.'::getAllowedPlaces()');
|
||||
}
|
||||
|
||||
// check the alias does not exists yet
|
||||
if (array_key_exists($alias, $this->widgetServices)) {
|
||||
throw new InvalidArgumentException('a service has already be defined with the ' .
|
||||
self::WIDGET_SERVICE_TAG_ALIAS . ' ' . $alias);
|
||||
if (\array_key_exists($alias, $this->widgetServices)) {
|
||||
throw new InvalidArgumentException('a service has already be defined with the '.self::WIDGET_SERVICE_TAG_ALIAS.' '.$alias);
|
||||
}
|
||||
|
||||
// register the factory as available
|
||||
@@ -307,7 +271,7 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
* register the service into container.
|
||||
*
|
||||
* @param string $place
|
||||
* @param float $order
|
||||
* @param float $order
|
||||
*
|
||||
* @return string the id of the new service
|
||||
*/
|
||||
@@ -338,14 +302,14 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
* recursive method.
|
||||
*
|
||||
* @param string $place
|
||||
* @param float $ordering
|
||||
* @param float $ordering
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
private function cacheAndGetOrdering($place, $ordering)
|
||||
{
|
||||
// create a key in the cache array if not exists
|
||||
if (!array_key_exists($place, $this->cacheOrdering)) {
|
||||
if (!\array_key_exists($place, $this->cacheOrdering)) {
|
||||
$this->cacheOrdering[$place] = [];
|
||||
}
|
||||
|
||||
@@ -363,14 +327,13 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
|
||||
/**
|
||||
* get the places where the service is allowed.
|
||||
*
|
||||
*
|
||||
* @return unknown
|
||||
*/
|
||||
private function isPlaceAllowedForWidget(mixed $place, mixed $widgetAlias, ContainerBuilder $container)
|
||||
{
|
||||
if ($this->widgetServices[$widgetAlias] instanceof WidgetFactoryInterface) {
|
||||
if (
|
||||
in_array($place, $this->widgetServices[$widgetAlias]
|
||||
\in_array($place, $this->widgetServices[$widgetAlias]
|
||||
->getAllowedPlaces(), true)
|
||||
) {
|
||||
return true;
|
||||
|
@@ -16,10 +16,6 @@ use Generator;
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
use function implode;
|
||||
use function in_array;
|
||||
|
||||
/**
|
||||
* This trait allow to add automatic configuration for widget inside your config.
|
||||
@@ -141,10 +137,10 @@ trait AddWidgetConfigurationTrait
|
||||
$treeBuilder = new TreeBuilder($place);
|
||||
$root = $treeBuilder->getRootNode()
|
||||
->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) {
|
||||
if (0 === \count(iterator_to_array($this->filterWidgetByPlace($place)))) {
|
||||
return $root;
|
||||
}
|
||||
|
||||
@@ -162,8 +158,8 @@ trait AddWidgetConfigurationTrait
|
||||
// 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)))
|
||||
.'Possible values are (maybe incomplete) : '.
|
||||
\implode(', ', $this->getWidgetAliasesbyPlace($place, $containerBuilder)))
|
||||
->isRequired()
|
||||
->end();
|
||||
|
||||
@@ -188,12 +184,12 @@ trait AddWidgetConfigurationTrait
|
||||
*
|
||||
* @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) {
|
||||
if (in_array($place, $factory->getAllowedPlaces(), true)) {
|
||||
if (\in_array($place, $factory->getAllowedPlaces(), true)) {
|
||||
yield $factory;
|
||||
}
|
||||
}
|
||||
@@ -209,9 +205,9 @@ trait AddWidgetConfigurationTrait
|
||||
*
|
||||
* @param type $place
|
||||
*
|
||||
* @throws InvalidConfigurationException if a service's tag does not have the "alias" key
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @throws InvalidConfigurationException if a service's tag does not have the "alias" key
|
||||
*/
|
||||
protected function getWidgetAliasesbyPlace($place, ContainerBuilder $containerBuilder)
|
||||
{
|
||||
@@ -223,16 +219,11 @@ trait AddWidgetConfigurationTrait
|
||||
|
||||
// append the aliases added without factory
|
||||
foreach ($containerBuilder
|
||||
->findTaggedServiceIds(WidgetsCompilerPass::WIDGET_SERVICE_TAG_NAME)
|
||||
as $serviceId => $tags) {
|
||||
->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
|
||||
));
|
||||
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];
|
||||
|
@@ -72,7 +72,7 @@ interface WidgetFactoryInterface
|
||||
* return the service id to build the widget.
|
||||
*
|
||||
* @param string $place
|
||||
* @param float $order
|
||||
* @param float $order
|
||||
*
|
||||
* @return string the service definition
|
||||
*/
|
||||
|
Reference in New Issue
Block a user