|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JakubOnderka\PhpParallelLint; |
| 4 | + |
| 5 | +/* |
| 6 | +Copyright (c) 2014, Jakub Onderka |
| 7 | +All rights reserved. |
| 8 | +
|
| 9 | +Redistribution and use in source and binary forms, with or without |
| 10 | +modification, are permitted provided that the following conditions are met: |
| 11 | +
|
| 12 | +1. Redistributions of source code must retain the above copyright notice, this |
| 13 | + list of conditions and the following disclaimer. |
| 14 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + this list of conditions and the following disclaimer in the documentation |
| 16 | + and/or other materials provided with the distribution. |
| 17 | +
|
| 18 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 22 | +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +
|
| 29 | +The views and conclusions contained in the software and documentation are those |
| 30 | +of the authors and should not be interpreted as representing official policies, |
| 31 | +either expressed or implied, of the FreeBSD Project. |
| 32 | + */ |
| 33 | + |
| 34 | +/** |
| 35 | + * Class Application |
| 36 | + * @package JakubOnderka\PhpParallelLint |
| 37 | + */ |
| 38 | +class Application |
| 39 | +{ |
| 40 | + const VERSION = '1.0.0'; |
| 41 | + const SUCCESS = 0; |
| 42 | + const WITH_ERRORS = 1; |
| 43 | + const FAILED = 255; |
| 44 | + |
| 45 | + /** |
| 46 | + * Run the application |
| 47 | + */ |
| 48 | + public function run() |
| 49 | + { |
| 50 | + if (in_array('proc_open', explode(',', ini_get('disable_functions')))) { |
| 51 | + echo "Function 'proc_open' is required, but it is disabled by disable_functions setting.", PHP_EOL; |
| 52 | + die(self::FAILED); |
| 53 | + } |
| 54 | + if (in_array('-h', $_SERVER['argv']) || in_array('--help', $_SERVER['argv'])) { |
| 55 | + $this->showUsage(); |
| 56 | + } |
| 57 | + if (in_array('-V', $_SERVER['argv']) || in_array('--version', $_SERVER['argv'])) { |
| 58 | + $this->showVersion(); |
| 59 | + die(); |
| 60 | + } |
| 61 | + try { |
| 62 | + $settings = Settings::parseArguments($_SERVER['argv']); |
| 63 | + if ($settings->stdin) { |
| 64 | + $settings->addPaths(Settings::getPathsFromStdIn()); |
| 65 | + } |
| 66 | + if (empty($settings->paths)) { |
| 67 | + $this->showUsage(); |
| 68 | + } |
| 69 | + $manager = new Manager; |
| 70 | + $result = $manager->run($settings); |
| 71 | + if ($settings->ignoreFails) { |
| 72 | + die($result->hasSyntaxError() ? self::WITH_ERRORS : self::SUCCESS); |
| 73 | + } else { |
| 74 | + die($result->hasError() ? self::WITH_ERRORS : self::SUCCESS); |
| 75 | + } |
| 76 | + } catch (InvalidArgumentException $e) { |
| 77 | + echo "Invalid option {$e->getArgument()}", PHP_EOL, PHP_EOL; |
| 78 | + $this->showOptions(); |
| 79 | + die(self::FAILED); |
| 80 | + } catch (Exception $e) { |
| 81 | + if (isset($settings) && $settings->format === Settings::FORMAT_JSON) { |
| 82 | + echo json_encode($e); |
| 83 | + } else { |
| 84 | + echo $e->getMessage(), PHP_EOL; |
| 85 | + } |
| 86 | + die(self::FAILED); |
| 87 | + } catch (Exception $e) { |
| 88 | + echo $e->getMessage(), PHP_EOL; |
| 89 | + die(self::FAILED); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Outputs the options |
| 95 | + */ |
| 96 | + private function showOptions() |
| 97 | + { |
| 98 | + echo <<<HELP |
| 99 | +Options: |
| 100 | + -p <php> Specify PHP-CGI executable to run (default: 'php'). |
| 101 | + -s, --short Set short_open_tag to On (default: Off). |
| 102 | + -a, -asp Set asp_tags to On (default: Off). |
| 103 | + -e <ext> Check only files with selected extensions separated by comma. |
| 104 | + (default: php,php3,php4,php5,phtml,phpt) |
| 105 | + --exclude Exclude a file or directory. If you want exclude multiple items, |
| 106 | + use multiple exclude parameters. |
| 107 | + -j <num> Run <num> jobs in parallel (default: 10). |
| 108 | + --colors Enable colors in console output. (disables auto detection of color support) |
| 109 | + --no-colors Disable colors in console output. |
| 110 | + --no-progress Disable progress in console output. |
| 111 | + --json Output results as JSON string. |
| 112 | + --checkstyle Output results as Checkstyle XML. |
| 113 | + --blame Try to show git blame for row with error. |
| 114 | + --git <git> Path to Git executable to show blame message (default: 'git'). |
| 115 | + --stdin Load files and folder to test from standard input. |
| 116 | + --ignore-fails Ignore failed tests. |
| 117 | + -h, --help Print this help. |
| 118 | + -V, --version Display this application version |
| 119 | +
|
| 120 | +HELP; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Outputs the current version |
| 125 | + */ |
| 126 | + private function showVersion() |
| 127 | + { |
| 128 | + echo 'PHP Parallel Lint version ' . self::VERSION.PHP_EOL; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Shows usage |
| 133 | + */ |
| 134 | + private function showUsage() |
| 135 | + { |
| 136 | + $this->showVersion(); |
| 137 | + echo <<<USAGE |
| 138 | +------------------------------- |
| 139 | +Usage: |
| 140 | +parallel-lint [sa] [-p php] [-e ext] [-j num] [--exclude dir] [files or directories] |
| 141 | +
|
| 142 | +USAGE; |
| 143 | + $this->showOptions(); |
| 144 | + die(); |
| 145 | + } |
| 146 | +} |
0 commit comments