Skip to content

Commit

Permalink
feat: add config builder class
Browse files Browse the repository at this point in the history
  • Loading branch information
owenvoke committed Sep 25, 2024
1 parent 84dcec7 commit 3a42e24
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 26 deletions.
38 changes: 16 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,23 @@ Create a file in the root called `graphlint.php` with the following configuratio
```php
<?php declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Worksome\Graphlint\Configuration\Visitor;
use Worksome\Graphlint\Inspections\CamelCaseFieldDefinitionInspection;
use Worksome\Graphlint\Config\GraphlintConfig;use Worksome\Graphlint\Inspections\CamelCaseFieldDefinitionInspection;

return function (ContainerConfigurator $config): void {
$services = $config->services();

$services->set(CamelCaseFieldDefinitionInspection::class)
->tag(Visitor::COMPILED);
};
return GraphlintConfig::configure()
->withInspections([
CamelCaseFieldDefinitionInspection::class,
]);
```

To use the Worksome GraphQL standard:

```php
<?php declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Worksome\Graphlint\GraphlintSet;
use Worksome\Graphlint\Config\GraphlintConfig;

return function (ContainerConfigurator $config): void {
$config->import(GraphlintSet::Standard->value);
};
return GraphlintConfig::configure()
->withPreparedSets(standard: true);
```

The tool can have a configuration for schemas before compiling and after.
Expand All @@ -93,14 +87,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
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</source>
</phpunit>
15 changes: 15 additions & 0 deletions src/Config/GraphlintConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Worksome\Graphlint\Config;

use Worksome\Graphlint\Configuration\GraphlintConfigBuilder;

final class GraphlintConfig
{
public static function configure(): GraphlintConfigBuilder
{
return new GraphlintConfigBuilder();
}
}
65 changes: 65 additions & 0 deletions src/Configuration/GraphlintConfigBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Worksome\Graphlint\Configuration;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Worksome\Graphlint\GraphlintSet;
use Worksome\Graphlint\Inspections\IgnoreByNameSuppressorInspection;
use Worksome\Graphlint\Inspections\Inspection;

final class GraphlintConfigBuilder
{
/** @var array<GraphlintSet> */
private array $sets = [];

/** @var array<class-string<Inspection>> */
private array $inspections = [];

/** @var array<string> */
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<class-string<Inspection>> $inspections */
public function withInspections(array $inspections): self
{
$this->inspections = [...$this->inspections, ...$inspections];

return $this;
}

/** @param array<non-empty-string> $ignored */
public function ignoring(array $ignored): self
{
$this->ignored = [...$this->ignored, ...$ignored];

return $this;
}
}
4 changes: 2 additions & 2 deletions tests/Feature/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $configurator) {
$services = $configurator->services();
return function (ContainerConfigurator $config) {
$services = $config->services();

$services->load(
'Worksome\\Graphlint\\Inspections\\',
Expand Down

0 comments on commit 3a42e24

Please sign in to comment.