From a6c5125da401b42222216eade52ea953fe69b10c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sun, 12 Nov 2017 22:58:47 +0000 Subject: [PATCH] Fix usage when installed with GCR --- bin/box | 84 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 33 deletions(-) diff --git a/bin/box b/bin/box index 33385fce1..9af389311 100755 --- a/bin/box +++ b/bin/box @@ -1,11 +1,26 @@ #!/usr/bin/env php + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/* * The project (or Phar) base path. * * @var string */ + +use Composer\Autoload\ClassLoader; + define('BOX_PATH', dirname(__DIR__)); if (extension_loaded('phar') && ($uri = Phar::running())) { @@ -13,7 +28,7 @@ if (extension_loaded('phar') && ($uri = Phar::running())) { } elseif (class_exists('Extract')) { require __DIR__.'/../src/vendors/autoload.php'; } else { - loadComposerClassloader(realpath($_SERVER['argv'][0])); + load_composer_class_loader(realpath($_SERVER['argv'][0])); } $app = new KevinGH\Box\Application(); @@ -22,38 +37,56 @@ $app->run(); /** * Finds the Composer autoloader and returns it. * - * @param null $dir the starting directory - * @param int $skip the number of occurrences to skip + * @param null|string $dir the starting directory * * @throws RuntimeException if the loader could not be loaded * - * @return Composer\Autoload\ClassLoader the class loader + * @return ClassLoader the class loader */ -function loadComposerClassloader($dir = null, $skip = 0) +function load_composer_class_loader(?string $dir): ClassLoader { $up = $dir; - $skips = 0; + $jsonPath = ''; + $autoloaderPath = ''; do { $dir = $up; if (file_exists("$dir/composer.json")) { - if ($skip > $skips) { - ++$skips; - - continue; - } - - $path = realpath("$dir/composer.json"); + $jsonPath = realpath("$dir/composer.json"); + $vendorPath = $dir.DIRECTORY_SEPARATOR.get_vendor_directory_name($jsonPath); + $autoloaderPath = $vendorPath.DIRECTORY_SEPARATOR.'autoload.php'; } - } while ($dir !== ($up = dirname($dir))); - if (empty($path)) { + $up = dirname($dir); + } while ($dir !== $up && (!file_exists($autoloaderPath))); + + if (empty($jsonPath)) { throw new RuntimeException( 'The composer.json file could not be found.' ); } + if (empty($autoloaderPath)) { + throw new RuntimeException( + 'The composer autoload.php file could not be found.' + ); + } + + if (false === file_exists($autoloaderPath)) { + throw new RuntimeException( + sprintf( + 'The Composer class loader "%s" could not be found.', + $autoloaderPath + ) + ); + } + + return include $autoloaderPath; +} + +function get_vendor_directory_name(string $path): string +{ if (false === ($json = file_get_contents($path))) { throw new RuntimeException( sprintf( @@ -75,24 +108,9 @@ function loadComposerClassloader($dir = null, $skip = 0) ); } - $path = dirname($path); - if (isset($json->config, $json->config->{'vendor-dir'})) { - $path .= DIRECTORY_SEPARATOR.$json->config->{'vendor-dir'}; - } else { - $path .= DIRECTORY_SEPARATOR.'vendor'; - } - - $path .= DIRECTORY_SEPARATOR.'autoload.php'; - - if (false === file_exists($path)) { - throw new RuntimeException( - sprintf( - 'The Composer class loader "%s" could not be found.', - $path - ) - ); + return $json->config->{'vendor-dir'}; } - return include $path; + return 'vendor'; }