Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
Moved application logic to src/Application.php (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
janvernieuwe authored and JakubOnderka committed Sep 29, 2018
1 parent 465183b commit f536d0d
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 130 deletions.
61 changes: 60 additions & 1 deletion parallel-lint
Original file line number Diff line number Diff line change
@@ -1,4 +1,63 @@
#!/usr/bin/env php
<?php

require __DIR__ . '/parallel-lint.php';
/*
Copyright (c) 2014, Jakub Onderka
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/

if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50400) {
echo "PHP Parallel Lint require PHP 5.4.0 or newer.", PHP_EOL;
die(255);
}

$autoloadLocations = [
getcwd() . '/vendor/autoload.php',
getcwd() . '/../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php',
];

$loaded = false;
foreach ($autoloadLocations as $autoload) {
if (is_file($autoload)) {
require_once($autoload);
$loaded = true;
}
}

if (!$loaded) {
fwrite(STDERR,
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
exit(255);
}

$app = new JakubOnderka\PhpParallelLint\Application();
$app->run();
129 changes: 0 additions & 129 deletions parallel-lint.php

This file was deleted.

146 changes: 146 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

namespace JakubOnderka\PhpParallelLint;

/*
Copyright (c) 2014, Jakub Onderka
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/

/**
* Class Application
* @package JakubOnderka\PhpParallelLint
*/
class Application
{
const VERSION = '1.0.0';
const SUCCESS = 0;
const WITH_ERRORS = 1;
const FAILED = 255;

/**
* Run the application
*/
public function run()
{
if (in_array('proc_open', explode(',', ini_get('disable_functions')))) {
echo "Function 'proc_open' is required, but it is disabled by disable_functions setting.", PHP_EOL;
die(self::FAILED);
}
if (in_array('-h', $_SERVER['argv']) || in_array('--help', $_SERVER['argv'])) {
$this->showUsage();
}
if (in_array('-V', $_SERVER['argv']) || in_array('--version', $_SERVER['argv'])) {
$this->showVersion();
die();
}
try {
$settings = Settings::parseArguments($_SERVER['argv']);
if ($settings->stdin) {
$settings->addPaths(Settings::getPathsFromStdIn());
}
if (empty($settings->paths)) {
$this->showUsage();
}
$manager = new Manager;
$result = $manager->run($settings);
if ($settings->ignoreFails) {
die($result->hasSyntaxError() ? self::WITH_ERRORS : self::SUCCESS);
} else {
die($result->hasError() ? self::WITH_ERRORS : self::SUCCESS);
}
} catch (InvalidArgumentException $e) {
echo "Invalid option {$e->getArgument()}", PHP_EOL, PHP_EOL;
$this->showOptions();
die(self::FAILED);
} catch (Exception $e) {
if (isset($settings) && $settings->format === Settings::FORMAT_JSON) {
echo json_encode($e);
} else {
echo $e->getMessage(), PHP_EOL;
}
die(self::FAILED);
} catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
die(self::FAILED);
}
}

/**
* Outputs the options
*/
private function showOptions()
{
echo <<<HELP
Options:
-p <php> Specify PHP-CGI executable to run (default: 'php').
-s, --short Set short_open_tag to On (default: Off).
-a, -asp Set asp_tags to On (default: Off).
-e <ext> Check only files with selected extensions separated by comma.
(default: php,php3,php4,php5,phtml,phpt)
--exclude Exclude a file or directory. If you want exclude multiple items,
use multiple exclude parameters.
-j <num> Run <num> jobs in parallel (default: 10).
--colors Enable colors in console output. (disables auto detection of color support)
--no-colors Disable colors in console output.
--no-progress Disable progress in console output.
--json Output results as JSON string.
--checkstyle Output results as Checkstyle XML.
--blame Try to show git blame for row with error.
--git <git> Path to Git executable to show blame message (default: 'git').
--stdin Load files and folder to test from standard input.
--ignore-fails Ignore failed tests.
-h, --help Print this help.
-V, --version Display this application version
HELP;
}

/**
* Outputs the current version
*/
private function showVersion()
{
echo 'PHP Parallel Lint version ' . self::VERSION.PHP_EOL;
}

/**
* Shows usage
*/
private function showUsage()
{
$this->showVersion();
echo <<<USAGE
-------------------------------
Usage:
parallel-lint [sa] [-p php] [-e ext] [-j num] [--exclude dir] [files or directories]
USAGE;
$this->showOptions();
die();
}
}

0 comments on commit f536d0d

Please sign in to comment.