Skip to content

Commit 2971dd8

Browse files
committed
Moved application logic to src/Application.php (#109)
1 parent 46261ba commit 2971dd8

File tree

3 files changed

+206
-130
lines changed

3 files changed

+206
-130
lines changed

parallel-lint

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,63 @@
11
#!/usr/bin/env php
22
<?php
33

4-
require __DIR__ . '/parallel-lint.php';
4+
/*
5+
Copyright (c) 2014, Jakub Onderka
6+
All rights reserved.
7+
8+
Redistribution and use in source and binary forms, with or without
9+
modification, are permitted provided that the following conditions are met:
10+
11+
1. Redistributions of source code must retain the above copyright notice, this
12+
list of conditions and the following disclaimer.
13+
2. Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
28+
The views and conclusions contained in the software and documentation are those
29+
of the authors and should not be interpreted as representing official policies,
30+
either expressed or implied, of the FreeBSD Project.
31+
*/
32+
33+
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50400) {
34+
echo "PHP Parallel Lint require PHP 5.4.0 or newer.", PHP_EOL;
35+
die(255);
36+
}
37+
38+
$autoloadLocations = [
39+
getcwd() . '/vendor/autoload.php',
40+
getcwd() . '/../../autoload.php',
41+
__DIR__ . '/../vendor/autoload.php',
42+
__DIR__ . '/../../../autoload.php',
43+
];
44+
45+
$loaded = false;
46+
foreach ($autoloadLocations as $autoload) {
47+
if (is_file($autoload)) {
48+
require_once($autoload);
49+
$loaded = true;
50+
}
51+
}
52+
53+
if (!$loaded) {
54+
fwrite(STDERR,
55+
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
56+
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
57+
'php composer.phar install' . PHP_EOL
58+
);
59+
exit(255);
60+
}
61+
62+
$app = new JakubOnderka\PhpParallelLint\Application();
63+
$app->run();

parallel-lint.php

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/Application.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)