Skip to content

Commit dd4b1f4

Browse files
committed
Tidied up hook binary
1 parent 3839041 commit dd4b1f4

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

bin/dredd-hooks-laravel

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
use Dredd\Server;
99
use Dredd\Hooks;
10+
use PhpParser\ParserFactory;
1011

1112
ini_set('implicit_flush', 'on');
1213
ini_set('output_buffering', 'off');
1314

15+
$basePath = __DIR__ . '/../../../..';
1416

1517
$loaded = false;
16-
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
18+
foreach ([$basePath . '/vendor/autoload.php'] as $file) {
1719
if (file_exists($file)) {
1820
require $file;
1921
$loaded = true;
@@ -29,7 +31,7 @@ if (!$loaded) {
2931
}
3032

3133
// Load Laravel
32-
$app = require $devPath . '/bootstrap/app.php';
34+
$app = require $basePath . '/bootstrap/app.php';
3335
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
3436

3537
// Get options from the command line
@@ -40,32 +42,31 @@ $options = getopt('', [
4042
]);
4143

4244
// Second argument is the single kernel file
43-
$dreddKernel = $argv[1];
44-
$dreddKernelFile = file_get_contents($dreddKernel);
45-
46-
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7);
45+
$dreddKernelPath = $argv[1];
46+
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
4747

4848
try {
49-
$stmts = $parser->parse($dreddKernelFile);
50-
// $stmts is an array of statement nodes
49+
$statements = $parser->parse(file_get_contents($dreddKernelPath));
5150
} catch (PhpParser\Error $e) {
52-
echo 'Parse Error: ', $e->getMessage();
51+
echo "Failed parsing {$dreddKernelPath} - " . $e->getMessage();
52+
die();
5353
}
5454

55-
$namespace = '';
56-
$class = '';
57-
58-
if ($stmts[0] instanceof PhpParser\Node\Stmt\Namespace_) {
59-
$namespace = implode('\\', $stmts[0]->name->parts);
60-
foreach ($stmts[0]->stmts as $statement) {
61-
if ($statement instanceof PhpParser\Node\Stmt\Class_) {
62-
$class = $statement->name;
55+
try {
56+
if ($statements[0] instanceof PhpParser\Node\Stmt\Namespace_) {
57+
$kernelClass = implode('\\', $statements[0]->name->parts);
58+
foreach ($statements[0]->stmts as $statement) {
59+
if ($statement instanceof PhpParser\Node\Stmt\Class_) {
60+
$kernelClass .= '\\' . $statement->name;
61+
}
6362
}
6463
}
65-
}
6664

67-
$kernelClass = $namespace . '\\' . $class;
68-
$kernel = new $kernelClass;
65+
$kernel = new $kernelClass;
66+
} catch (\Exception $e) {
67+
echo "Failed parsing {$dreddKernelPath}. Ensure the file has both a namespace and class name. [{$e->getMessage()}]";
68+
die();
69+
}
6970

7071
$kernel->handle(new \Netsells\Dredd\Hook());
7172

0 commit comments

Comments
 (0)