From d1bb339914ee4db6578c3745a6e5090df2dae985 Mon Sep 17 00:00:00 2001 From: Owen Voke Date: Wed, 25 Sep 2024 14:51:07 +0100 Subject: [PATCH] feat: add config builder class --- README.md | 37 +++++------ phpunit.xml | 4 +- src/Config/GraphlintConfig.php | 15 +++++ src/Configuration/GraphlintConfigBuilder.php | 65 ++++++++++++++++++++ 4 files changed, 98 insertions(+), 23 deletions(-) create mode 100644 src/Config/GraphlintConfig.php create mode 100644 src/Configuration/GraphlintConfigBuilder.php diff --git a/README.md b/README.md index 97b6d3b..613c350 100644 --- a/README.md +++ b/README.md @@ -50,16 +50,13 @@ Create a file in the root called `graphlint.php` with the following configuratio ```php services(); - - $services->set(CamelCaseFieldDefinitionInspection::class) - ->tag(Visitor::COMPILED); -}; +return GraphlintConfig::configure() + ->withInspections([ + CamelCaseFieldDefinitionInspection::class, + ]); ``` To use the Worksome GraphQL standard: @@ -67,12 +64,10 @@ To use the Worksome GraphQL standard: ```php import(GraphlintSet::Standard->value); -}; +return GraphlintConfig::configure() + ->withPreparedSets(standard: true); ``` The tool can have a configuration for schemas before compiling and after. @@ -93,14 +88,14 @@ In some cases, it is not possible to add a comment because the schema is auto ge those cases, the error can be ignored by adding the following in the configuration file. ```php -return function (ContainerConfigurator $config): void { - $config->services() - ->set(IgnoreByNameSuppressorInspection::class) - ->call('configure', [ - 'TEST', - 'AccountInput.name' // Dotted value for only applying on some fields - ]); -}; +use Worksome\Graphlint\Config\GraphlintConfig; + +return GraphlintConfig::configure() + // ... + ->ignoring([ + 'TEST', + 'AccountInput.name' // Dotted value for only applying on some fields + ]); ``` ## Testing diff --git a/phpunit.xml b/phpunit.xml index f368d59..6d03a8b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -10,9 +10,9 @@ ./tests - + ./src - + diff --git a/src/Config/GraphlintConfig.php b/src/Config/GraphlintConfig.php new file mode 100644 index 0000000..151aec2 --- /dev/null +++ b/src/Config/GraphlintConfig.php @@ -0,0 +1,15 @@ + */ + private array $sets = []; + + /** @var array> */ + private array $inspections = []; + + /** @var array */ + private array $ignored = []; + + public function __invoke(ContainerConfigurator $config): void + { + $services = $config->services(); + + foreach ($this->sets as $set) { + $config->import($set->value); + } + + foreach ($this->inspections as $inspection) { + $services->set($inspection)->tag(Visitor::COMPILED); + } + + $services + ->set(IgnoreByNameSuppressorInspection::class) + ->call('configure', $this->ignored); + } + + public function withPreparedSets( + bool $standard = false, + ): self { + if ($standard) { + $this->sets[] = GraphlintSet::Standard; + } + + return $this; + } + + /** @param array> $inspections */ + public function withInspections(array $inspections): self + { + $this->inspections = [...$this->inspections, ...$inspections]; + + return $this; + } + + /** @param array $ignored */ + public function ignoring(array $ignored): self + { + $this->ignored = [...$this->ignored, ...$ignored]; + + return $this; + } +}