Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DX] Add configure() method to service configuration to make configuration easy again #1276

Merged
merged 3 commits into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"symplify/skipper": "^10.0",
"symplify/smart-file-system": "^10.0",
"symplify/symfony-php-config": "^10.0",
"symplify/vendor-patches": "^10.0",
"tracy/tracy": "^2.8",
"webmozart/assert": "^1.10"
},
Expand Down Expand Up @@ -142,6 +143,11 @@
"release": "vendor/bin/monorepo-builder release patch --ansi"
},
"extra": {
"patches": {
"symfony/dependency-injection": [
"patches/symfony-dependency-injection-loader-configurator-servicesconfigurator-php.patch"
]
},
"branch-alias": {
"dev-main": "0.12-dev"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--- /dev/null
+++ ../Loader/Configurator/ServicesConfigurator.php
@@ -70,7 +70,7 @@
* @param string|null $id The service id, or null to create an anonymous service
* @param string|null $class The class of the service, or null when $id is also the class name
*/
- final public function set(?string $id, string $class = null): ServiceConfigurator
+ final public function set(?string $id, string $class = null): \Rector\Core\DependencyInjection\Loader\Configurator\RectorServiceConfigurator
{
$defaults = $this->defaults;
$definition = new Definition();
@@ -91,7 +91,7 @@
$definition->setBindings(unserialize(serialize($defaults->getBindings())));
$definition->setChanges([]);

- $configurator = new ServiceConfigurator($this->container, $this->instanceof, true, $this, $definition, $id, $defaults->getTags(), $this->path);
+ $configurator = new \Rector\Core\DependencyInjection\Loader\Configurator\RectorServiceConfigurator($this->container, $this->instanceof, true, $this, $definition, $id, $defaults->getTags(), $this->path);

return null !== $class ? $configurator->class($class) : $configurator;
}
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,11 @@ parameters:
paths:
- packages/StaticTypeMapper/PhpDocParser/IntersectionTypeMapper.php
- packages/StaticTypeMapper/PhpParser/IntersectionTypeNodeMapper.php

-
message: '#Use separate function calls with readable variable names#'
path: src/DependencyInjection/Loader/Configurator/RectorServiceConfigurator.php

-
message: '#\$service\-\>call\("configure", \[\[ \.\.\. \]\]\) must be followed by exact array#'
path: src/DependencyInjection/Loader/Configurator/RectorServiceConfigurator.php
15 changes: 7 additions & 8 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
// include the latest PHP version + all bellow in one config!
Expand All @@ -38,18 +37,18 @@

// phpunit
$services->set(PreferThisOrSelfMethodCallRector::class)
->call('configure', [[
->configure([
PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => [
TestCase::class => ValueObjectInliner::inline(PreferenceSelfThis::PREFER_THIS()),
TestCase::class => PreferenceSelfThis::PREFER_THIS(),
],
]]);
]);

$services->set(ReturnArrayClassMethodToYieldRector::class)
->call('configure', [[
ReturnArrayClassMethodToYieldRector::METHODS_TO_YIELDS => ValueObjectInliner::inline([
->configure([
ReturnArrayClassMethodToYieldRector::METHODS_TO_YIELDS => [
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', '*provide*'),
]),
]]);
],
]);

$parameters = $containerConfigurator->parameters();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Rector\Core\DependencyInjection\Loader\Configurator;

use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServiceConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

/**
* @api
* Same as Symfony service configurator, with extra "configure()" method for easier DX
*/
final class RectorServiceConfigurator extends ServiceConfigurator
{
/**
* @param array<string, string|bool|object|string[]|object[]> $configuration
*/
public function configure(array $configuration): self
{
$this->ensureClassIsConfigurable($this->id);

// decorate with value object inliner so Symfony understands, see https://getrector.org/blog/2020/09/07/how-to-inline-value-object-in-symfony-php-config
array_walk_recursive($configuration, function (&$value) {
if (is_object($value)) {
$value = ValueObjectInliner::inline($value);
}

return $value;
});

$this->call('configure', [$configuration]);

return $this;
}

private function ensureClassIsConfigurable(?string $class): void
{
if ($class === null) {
throw new InvalidConfigurationException('The class is missing');
}

if (! is_a($class, ConfigurableRectorInterface::class, true)) {
$errorMessage = sprintf(
'The service "%s" is not configurable. Make it implement "%s" or remove "configure()" call.',
$class,
ConfigurableRectorInterface::class,
);
throw new InvalidConfigurationException($errorMessage);
}
}
}