Browse Source

feature #262 Allow to specify path to .php_cs file using --config-file option (localheinz)

This PR was squashed before being merged into the 0.4.x-dev branch (closes #262).

Discussion
----------

Allow to specify path to .php_cs file using --config-file option

This PR adds the possibility to specify the path to  the `.php_cs` file using a newly added `--config-file` option.

This is very useful if you have a Git `pre-commit` hook similar to the one suggested by [ZF2](https://github.com/zendframework/zf2/blob/master/README-GIT.md#pre-commit-hook-optional), but want to keep the configuration separate from the hook:

```php
#!/usr/bin/env php
<?php

$changes = [];

exec(
    'git diff --cached --name-status --diff-filter=ACM',
    $changes
);

$exit = 0;

array_walk($changes, function ($change) use (&$exit) {

    if ('D' === substr($change, 0, 1)) {
        return;
    }

    $name = trim(substr(
        $change,
        1
    ));

    $extension = pathinfo(
        $name,
        PATHINFO_EXTENSION
    );

    if (!preg_match('/^ph(p|tml)$/', $extension)) {
        return;
    }

    $output = [];
    $return = 0;

    exec(
        sprintf(
            'php -l %s',
            escapeshellarg($name)
        ),
        $output,
        $return
    );

    if (0 != $return) {

        echo sprintf(
            'Failed parsing: %s: ' . PHP_EOL,
            $name
        );

        echo implode(PHP_EOL, $output) . PHP_EOL;

        $exit = 1;
        return;
    }

    $output = [];
    $return = 0;

    exec(
        sprintf(
            'vendor/bin/php-cs-fixer fix --verbose --config-file=.php_cs %s',
            escapeshellarg($name)
        ),
        $output,
        $return
    );

    if (0 != $return) {

        echo sprintf(
            'Fixed coding style issues in : %s' . PHP_EOL,
            $name
        );

        echo implode(PHP_EOL, $output) . PHP_EOL;

        $exit = 1;
        return;
    }
});

exit($exit);
```

Fixes #251.

Commits
-------

e4ceb86 Allow to specify path to .php_cs file using --config-file option
Fabien Potencier 11 years ago
parent
commit
99051c3aa5
2 changed files with 7 additions and 1 deletions
  1. 0 1
      .gitignore
  2. 7 0
      Symfony/CS/Console/Command/FixCommand.php

+ 0 - 1
.gitignore

@@ -1,4 +1,3 @@
 composer.lock
 vendor
 phpunit.xml
-

+ 7 - 0
Symfony/CS/Console/Command/FixCommand.php

@@ -55,6 +55,7 @@ class FixCommand extends Command
             ->setDefinition(array(
                 new InputArgument('path', InputArgument::REQUIRED, 'The path'),
                 new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The configuration name', null),
+                new InputOption('config-file', '', InputOption::VALUE_OPTIONAL, 'The path to a .php_cs file ', null),
                 new InputOption('dry-run', '', InputOption::VALUE_NONE, 'Only shows which files would have been modified'),
                 new InputOption('level', '', InputOption::VALUE_REQUIRED, 'The level of fixes (can be psr0, psr1, psr2, or all)', null),
                 new InputOption('fixers', '', InputOption::VALUE_REQUIRED, 'A list of fixers to run'),
@@ -148,6 +149,9 @@ Note the additional <comment>-</comment> in front of the Fixer name.
         ->fixers(array('-Psr0Fixer'))
         ->finder(\$finder)
     ;
+
+With the <comment>--config-file</comment> option you can specify the path to the
+<comment>.php_cs</comment> file.
 EOF
             );
     }
@@ -187,6 +191,9 @@ EOF
             if (null === $config) {
                 throw new \InvalidArgumentException(sprintf('The configuration "%s" is not defined', $input->getOption('config')));
             }
+        } elseif ($input->getOption('config-file')) {
+            $file = $input->getOption('config-file');
+            $config = include $file;
         } elseif (file_exists($file = $path.'/.php_cs')) {
             $config = include $file;
             $addSuppliedPathFromCli = false;