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 a0c1f41
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 19 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
use Worksome\Graphlint\Configuration\Visitor;
use Worksome\Graphlint\Inspections\CamelCaseFieldDefinitionInspection;

return function (ContainerConfigurator $config): void {
$services = $config->services();
use Worksome\Graphlint\Configuration\GraphlintConfig;
use Worksome\Graphlint\GraphlintSet;

$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\Configuration\GraphlintConfig;
use Worksome\Graphlint\GraphlintSet;

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 +93,15 @@ 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\Configuration\GraphlintConfig;
use Worksome\Graphlint\GraphlintSet;

return GraphlintConfig::configure()
// ...
->ignoring([
'TEST',
'AccountInput.name' // Dotted value for only applying on some fields
]);
```

## Testing
Expand Down
4 changes: 2 additions & 2 deletions config/Sets/standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Worksome\Graphlint\Configuration\GraphlintConfig;
use Worksome\Graphlint\Configuration\Visitor;
use Worksome\Graphlint\Inspections\CamelCaseFieldDefinitionInspection;
use Worksome\Graphlint\Inspections\DescriptionRequiredInspection;
Expand All @@ -14,7 +14,7 @@
use Worksome\Graphlint\Inspections\PascalCaseObjectTypeDefinitionInspection;
use Worksome\Graphlint\Inspections\UpperSnakeCaseEnumCaseDefinitionInspection;

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

$inspections = [
Expand Down
15 changes: 15 additions & 0 deletions src/Configuration/GraphlintConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Worksome\Graphlint\Configuration;

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

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

declare(strict_types=1);

namespace Worksome\Graphlint\Configuration;

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(GraphlintConfig $graphlintConfig): void
{
$services = $graphlintConfig->services();

foreach ($this->sets as $set) {
$graphlintConfig->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 = array_merge($this->inspections, $inspections);

return $this;
}

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

return $this;
}
}

0 comments on commit a0c1f41

Please sign in to comment.