Skip to content

Commit

Permalink
Add support for dist config (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 20, 2024
1 parent 9400431 commit 5e5323d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ By default, the twig-cs-fixer standard is enabled with the twig coding standard
- `TrailingSpaceRule`: ensures that files have no trailing spaces.

If you want to use the basic Twig standard, another standard and/or add/disable a rule, you can provide
your own configuration with a `.twig-cs-fixer.php` file which returns a `TwigCsFixer\Config\Config` class:
your own configuration with a `.twig-cs-fixer.php` or `.twig-cs-fixer.dist.php` file which returns
a `TwigCsFixer\Config\Config` class:

```php
<?php
Expand All @@ -38,7 +39,7 @@ vendor/bin/twig-cs-fixer lint --config=dir/.twig-cs-fixer.php /path/to/code
By default, all `.twig` files in the current directory are linted, except the ones in the `vendor` directory.

If you want to lint specific files or directories you can pass them as argument. If you want a more sophisticated
rule, you can configure it in the `.twig-cs-fixer.php` file:
rule, you can configure it in the config file:

```php
<?php
Expand All @@ -57,7 +58,7 @@ return $config;
By default, cache is enabled and stored in `.twig-cs-fixer.cache`. Further runs are therefore much
faster. Cache is invalidated when a different PHP version, twig-cs-fixer version or ruleset is used.

If you want a custom cache location you can configure it in `.twig-cs-fixer.php`:
If you want a custom cache location you can configure it in your config file:

```php
<?php
Expand Down
1 change: 1 addition & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
final class Config
{
public const DEFAULT_PATH = '.twig-cs-fixer.php';
public const DEFAULT_DIST_PATH = '.twig-cs-fixer.dist.php';
public const DEFAULT_CACHE_PATH = '.twig-cs-fixer.cache';

private Ruleset $ruleset;
Expand Down
5 changes: 5 additions & 0 deletions src/Config/ConfigResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ private function getConfig(?string $configPath = null): Config
return $this->getConfigFromPath($defaultPath);
}

$defaultDistPath = $this->getAbsolutePath(Config::DEFAULT_DIST_PATH);
if (file_exists($defaultDistPath)) {
return $this->getConfigFromPath($defaultDistPath);
}

return new Config();
}

Expand Down
1 change: 1 addition & 0 deletions tests/Config/ConfigResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function resolveConfigDataProvider(): iterable
{
yield [__DIR__.'/Fixtures/directoryWithoutConfig', null, 'Default'];
yield [__DIR__.'/Fixtures/directoryWithCustomRuleset', null, 'Custom'];
yield [__DIR__.'/Fixtures/directoryWithCustomRuleset2', null, 'CustomDist'];
yield [__DIR__, 'Fixtures/directoryWithCustomRuleset/.twig-cs-fixer.php', 'Custom'];
yield ['/tmp', __DIR__.'/Fixtures/directoryWithCustomRuleset/.twig-cs-fixer.php', 'Custom'];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use TwigCsFixer\Config\Config;
use TwigCsFixer\Rules\Delimiter\DelimiterSpacingRule;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Standard\TwigCsFixer;

$ruleset = new Ruleset();
$ruleset->addStandard(new TwigCsFixer());
$ruleset->removeRule(DelimiterSpacingRule::class);

$config = new Config('CustomDist');
$config->setRuleset($ruleset);

return $config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use TwigCsFixer\Config\Config;
use TwigCsFixer\Rules\Delimiter\DelimiterSpacingRule;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Standard\TwigCsFixer;

$ruleset = new Ruleset();
$ruleset->addStandard(new TwigCsFixer());
$ruleset->removeRule(DelimiterSpacingRule::class);

$config = new Config('CustomDist');
$config->setRuleset($ruleset);

return $config;

0 comments on commit 5e5323d

Please sign in to comment.