diff --git a/lib/Boris/CLIOptionsHandler.php b/lib/Boris/CLIOptionsHandler.php index ff785f0..b446954 100644 --- a/lib/Boris/CLIOptionsHandler.php +++ b/lib/Boris/CLIOptionsHandler.php @@ -14,14 +14,20 @@ class CLIOptionsHandler */ public function handle($boris) { - $args = getopt('hvr:', array( + $args = getopt('ahvr:', array( + 'autoload', 'help', 'version', - 'require:' + 'require:', )); foreach ($args as $option => $value) { switch ($option) { + case 'a': + case 'autoload': + $this->_handleAutoloadOption($boris); + break; + /* * Sets files to load at startup, may be used multiple times, * i.e: boris -r test.php,foo/bar.php -r ba/foo.php --require hey.php @@ -76,9 +82,10 @@ private function _handleUsageInfo() boris is a tiny REPL for PHP Options: - -h, --help show this help message and exit - -r, --require a comma-separated list of files to require on startup - -v, --version show Boris version + -a, --autoload If in a composer project, automatically finds and requires autoload.php + -h, --help Show this help message and exit + -r, --require A comma-separated list of files to require on startup + -v, --version Show Boris version USAGE; exit(0); @@ -89,4 +96,24 @@ private function _handleVersion() printf("Boris %s\n", Boris::VERSION); exit(0); } + + private function _handleAutoloadOption($boris) + { + $boris->onStart(function() { + $autoload = null; + for ($dir = getenv('PWD'); !$autoload && $dir !== '/'; $dir = dirname($dir)) { + if (file_exists("$dir/autoload.php")) { + $autoload = "$dir/autoload.php"; + } else if (file_exists("$dir/vendor/autoload.php")) { + $autoload = "$dir/vendor/autoload.php"; + } + } + if ($autoload) { + echo 'Requiring autoload file: ' . $autoload . PHP_EOL; + require_once $autoload; + } else { + echo 'No autoload file found.' . PHP_EOL; + } + }); + } }