Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions lib/Boris/CLIOptionsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
});
}
}