Skip to content

Commit

Permalink
Make it better
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo FIDRY committed Oct 7, 2017
1 parent f83aed6 commit 79eff9f
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 2,582 deletions.
161 changes: 19 additions & 142 deletions bin/php-scoper
Original file line number Diff line number Diff line change
Expand Up @@ -35,153 +35,30 @@ $autoload = (function () {
throw new RuntimeException('Unable to find the Composer or PHP-Scoper autoloader.');
})();

$autoloader = require_once $autoload;
require_once $autoload;

// Exposes the finder used by PHP-Scoper PHAR to allow its usage in the configuration file.
if (false === class_exists('Isolated\Symfony\Component\Finder\Finder')) {
class_alias('Symfony\Component\Finder\Finder', 'Isolated\Symfony\Component\Finder\Finder');
}

$lineSize = 70;
$symfonyRequirements = new Requirements(dirname(dirname(realpath($autoload))));
$iniPath = $symfonyRequirements->getPhpIniPath();

echo_title('Symfony Requirements Checker');

echo '> PHP is using the following php.ini file:'.PHP_EOL;
if ($iniPath) {
echo_style('green', ' '.$iniPath);
} else {
echo_style('yellow', ' WARNING: No configuration file (php.ini) used by PHP!');
}

echo PHP_EOL.PHP_EOL;

echo '> Checking Symfony requirements:'.PHP_EOL.' ';

$messages = array();
foreach ($symfonyRequirements->getRequirements() as $req) {
if ($helpText = get_error_message($req, $lineSize)) {
echo_style('red', 'E');
$messages['error'][] = $helpText;
} else {
echo_style('green', '.');
}
}

$checkPassed = empty($messages['error']);

foreach ($symfonyRequirements->getRecommendations() as $req) {
if ($helpText = get_error_message($req, $lineSize)) {
echo_style('yellow', 'W');
$messages['warning'][] = $helpText;
} else {
echo_style('green', '.');
}
}

if ($checkPassed) {
echo_block('success', 'OK', 'Your system is ready to run Symfony projects');
} else {
echo_block('error', 'ERROR', 'Your system is not ready to run Symfony projects');

echo_title('Fix the following mandatory requirements', 'red');

foreach ($messages['error'] as $helpText) {
echo ' * '.$helpText.PHP_EOL;
}
}

if (!empty($messages['warning'])) {
echo_title('Optional recommendations to improve your setup', 'yellow');

foreach ($messages['warning'] as $helpText) {
echo ' * '.$helpText.PHP_EOL;
}
}

echo PHP_EOL;
echo_style('title', 'Note');
echo ' The command console could use a different php.ini file'.PHP_EOL;
echo_style('title', '~~~~');
echo ' than the one used with your web server. To be on the'.PHP_EOL;
echo ' safe side, please check the requirements from your web'.PHP_EOL;
echo ' server using the ';
echo_style('yellow', 'public/check.php');
echo ' script.'.PHP_EOL;
echo PHP_EOL;

exit($checkPassed ? 0 : 1);

function get_error_message(Requirement $requirement, $lineSize)
{
if ($requirement->isFulfilled()) {
return;
}

$errorMessage = wordwrap($requirement->getTestMessage(), $lineSize - 3, PHP_EOL.' ').PHP_EOL;
$errorMessage .= ' > '.wordwrap($requirement->getHelpText(), $lineSize - 5, PHP_EOL.' > ').PHP_EOL;

return $errorMessage;
}

function echo_title($title, $style = null)
{
$style = $style ?: 'title';

echo PHP_EOL;
echo_style($style, $title.PHP_EOL);
echo_style($style, str_repeat('~', strlen($title)).PHP_EOL);
echo PHP_EOL;
}

function echo_style($style, $message)
{
// ANSI color codes
$styles = array(
'reset' => "\033[0m",
'red' => "\033[31m",
'green' => "\033[32m",
'yellow' => "\033[33m",
'error' => "\033[37;41m",
'success' => "\033[37;42m",
'title' => "\033[34m",
);
$supports = has_color_support();

echo($supports ? $styles[$style] : '').$message.($supports ? $styles['reset'] : '');
}

function echo_block($style, $title, $message)
{
$message = ' '.trim($message).' ';
$width = strlen($message);

echo PHP_EOL.PHP_EOL;

echo_style($style, str_repeat(' ', $width));
echo PHP_EOL;
echo_style($style, str_pad(' ['.$title.']', $width, ' ', STR_PAD_RIGHT));
echo PHP_EOL;
echo_style($style, $message);
echo PHP_EOL;
echo_style($style, str_repeat(' ', $width));
echo PHP_EOL;
}

function has_color_support()
{
static $support;

if (null === $support) {
if (DIRECTORY_SEPARATOR == '\\') {
$support = false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
} else {
$support = function_exists('posix_isatty') && @posix_isatty(STDOUT);
}
}

return $support;
}
$isVeryVerbose = array_reduce(
$argv,
/**
* @param bool $isVeryVerbose
* @param string $param
*
* @return bool
*/
function ($isVeryVerbose, $arg) {
return $isVeryVerbose || '-vv' === $arg || '-vvv' === $arg;
},
false
);

//if ('phar:' === substr(__FILE__, 0, 5)) {
check_requirements($autoload, $isVeryVerbose);
//}

$app = create_application();

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

"require": {
"php": "^7.1",
"composer/semver": "^1.4",
"nikic/php-parser": "^3.0",
"ocramius/package-versions": "^1.1",
"padraic/phar-updater": "^1.0",
"symfony/console": "^3.2",
"symfony/filesystem": "^3.2",
"symfony/finder": "^3.2"
"symfony/finder": "^3.2",
"symfony/requirements-checker": "^1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.1",
Expand All @@ -37,6 +39,7 @@
"Humbug\\PhpScoper\\": "src/"
},
"files": [
"src/check.php",
"src/functions.php"
]
},
Expand Down
Loading

0 comments on commit 79eff9f

Please sign in to comment.