phpro / grumphp

A PHP code-quality tool
MIT License
4.11k stars 429 forks source link

Config options resolver #1090

Closed veewee closed 1 year ago

veewee commented 1 year ago
Q A
Branch v2
Bug fix? yes
New feature? yes
BC breaks? yes
Deprecations? no
Documented? no
Fixed tickets

Fixes https://github.com/phpro/grumphp-shim/issues/10

This PR makes sure that the GrumPHP TaskInterface only has a dependency to the GrumPHP\\ namespace. Symfony OptionsResolver will not be a part of the task interface anymore.

New task interface:

namespace GrumPHP\Task;

use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Config\ConfigOptionsResolver;
use GrumPHP\Task\Config\TaskConfigInterface;
use GrumPHP\Task\Context\ContextInterface;

interface TaskInterface
{
    public static function getConfigurableOptions(): ConfigOptionsResolver;

    public function canRunInContext(ContextInterface $context): bool;

    public function run(ContextInterface $context): TaskResultInterface;

    public function getConfig(): TaskConfigInterface;

    public function withConfig(TaskConfigInterface $config): TaskInterface;
}

This makes it possible for extensions to provide tasks that are compatible with the grumphp-shim release. In order to make the task compatible, you'll need to wrap the symfony options-resolver with GrumPHP's own options-resolver:

    public static function getConfigurableOptions(): ConfigOptionsResolver
    {
        $resolver = new OptionsResolver();

        // ..... your config

        return ConfigOptionsResolver::fromClosure(
            static fn (array $options): array => $resolver->resolve($options)
        );
    }

❗ In extensions, you do not want to use \GrumPHP\Task\Config\ConfigOptionsResolver::fromOptionsResolver() - since Symfony's options resolver will be scoped (prefixed) in grumphp-shim's phar distrubition.