From c85e24602dabfd1dd9b7628c6eee0ca09c8dcbb0 Mon Sep 17 00:00:00 2001 From: dana Date: Sun, 31 Jul 2016 19:03:27 -0500 Subject: [PATCH] Improve auto-loader resolution --- src/bootstrap.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/bootstrap.php b/src/bootstrap.php index fd0a130..da8f7d1 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -31,17 +31,34 @@ function twigc_puts_error($string = '') { } } +// Find Composer's auto-loader +$autoloaders = [ + // Phar/repo path + __DIR__ . '/../vendor/autoload.php', + // Composer path + __DIR__ . '/../../../autoload.php', +]; + +foreach ( $autoloaders as $autoloader ) { + if ( file_exists($autoloader) ) { + define('TWIGC_AUTOLOADER', $autoloader); + break; + } +} + +unset($autoloaders, $autoloader); + // Disallow running from non-CLI SAPIs if ( \PHP_SAPI !== 'cli' ) { twigc_puts_error("This tool must be invoked via PHP's CLI SAPI."); exit(1); } -// Give a meaningful error if we don't have our vendor dependencies -if ( ! file_exists(__DIR__ . '/../vendor/autoload.php') ) { +// Give a meaningful error if we don't have the auto-loader +if ( ! defined('TWIGC_AUTOLOADER') ) { twigc_puts_error('Auto-loader is missing — try running `composer install`.'); exit(1); } -require_once __DIR__ . '/../vendor/autoload.php'; +require_once TWIGC_AUTOLOADER;