Skip to content

Commit a1b2ac7

Browse files
committed
Fix #7 potential never-ending loop looking for autoload file.
See: #7
1 parent 96dfc01 commit a1b2ac7

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

bin/capture-lookups

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,57 @@
11
#!/usr/bin/env php
22
<?php
3+
34
set_time_limit(0);
45

5-
$vendor = __DIR__;
6-
while (!file_exists($vendor.'/vendor')) {
7-
$vendor = dirname($vendor);
6+
//START - Autoloader inclusion
7+
8+
//Determine if autoloader can be found.
9+
$possibleAutoloaderPaths = array(
10+
__DIR__.'/../../../../vendor/autoload.php', //for when package bin is in vendor directory
11+
__DIR__.'/../vendor/autoload.php', //for when standalone bin is in root
12+
);
13+
14+
$autoloaderFound = FALSE;
15+
foreach ($possibleAutoloaderPaths as $autoloaderFile) {
16+
if (file_exists($autoloaderFile)) {
17+
$autoloaderFound = TRUE;
18+
break;
19+
}
20+
}
21+
22+
//Either require autoloader.php or gracefully report error.
23+
if ($autoloaderFound){
24+
require_once $autoloaderFile;
25+
} else {
26+
//Tell the user in a friendly way:
27+
28+
//Compose the messages.
29+
$feedbackLines = array(
30+
'The autoload.php was not found.',
31+
'The script searched for it at:',
32+
);
33+
34+
foreach ($possibleAutoloaderPaths as $autoloaderFile) {
35+
$feedbackLines[] = '[' . $autoloaderFile . ']';
36+
}
37+
38+
//Extra spaces to highlight the suggestion.
39+
$feedbackLines[] =
40+
PHP_EOL
41+
. 'Maybe you forgot to run \'composer install\'?'
42+
. PHP_EOL;
43+
44+
//Write them out.
45+
foreach($feedbackLines as $line){
46+
fwrite(STDERR, $line . PHP_EOL);
47+
}
48+
49+
50+
//Exit with non-zero (i.e error) code.
51+
exit(1);
852
}
9-
require $vendor.'/vendor/autoload.php';
53+
//END - Autoloader inclusion
54+
1055

1156
$command = new \XmlSquad\CaptureLookups\Command\CaptureLookupsCommand();
1257
$application = new \Symfony\Component\Console\Application();

0 commit comments

Comments
 (0)