Skip to content

Commit

Permalink
added DebugMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
basuke committed Apr 21, 2017
1 parent 4b053a9 commit e117ff8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
20 changes: 18 additions & 2 deletions Clim/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class App
/** @var Spec */
private $spec;

/** @var bool */
private $handle_errors = true;

/**
* Constructor of Clim\App
* @param ContainerInterface|array|null $container
Expand Down Expand Up @@ -146,13 +149,26 @@ public function run()
$context = $this->runner()->run(array_slice($argv, 1));
return $context;
} catch (Exception $e) {
$this->handleException($e);
if ($this->handle_errors) {
$this->handleException($e);
} else {
throw $e;
}
} catch (Throwable $e) {
$this->handlePhpError($e);
if ($this->handle_errors) {
$this->handlePhpError($e);
} else {
throw $e;
}
}
return null;
}

public function disableErrorHandling()
{
$this->handle_errors = false;
}

/**
* Call relevant handler from the Container if needed. If it doesn't exist,
* then just print error
Expand Down
52 changes: 52 additions & 0 deletions Clim/Middleware/DebugMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Clim\Middleware;

use Clim\Context;
use Clim\Helper\Hash;
use Psr\Container\ContainerInterface;
use Symfony\Component\Debug\Debug;

/**
* Created by PhpStorm.
* User: basuke
* Date: 4/20/17
* Time: 5:57 PM
*/

class DebugMiddleware
{
private $display_error = true;
private $error_reporting_level = E_ALL;

public function __construct(ContainerInterface $container)
{
if (!class_exists('\Symfony\Component\Debug\Debug')) {
throw new \Clim\Exception\DefinitionException("symfony/debug is required for " . static::class);
}

$setting = new Hash($container['settings']['debug']);
$this->display_error = $setting['display_error'] ?: true;
$this->error_reporting_level = $setting['error_reporting_level'] ?: E_ALL;
}

public function __invoke(Context $context, callable $next)
{
$app = $context->getApp();

$display_error = $this->display_error;
$error_reporting_level = $this->error_reporting_level;

$app->opt('-d|--debug', function () use (
$app,
$display_error,
$error_reporting_level
) {
$app->disableErrorHandling();

Debug::enable($error_reporting_level, $display_error);
});

return $next($context);
}
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
}
},
"require-dev": {
"codeception/codeception": "^2.2"
"codeception/codeception": "^2.2",
"symfony/debug": "^3.2",
"symfony/var-dumper": "^3.2"
},
"scripts": {
"test": [
Expand Down
9 changes: 7 additions & 2 deletions examples/hello.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

use Clim\App;
use Clim\Middleware\DebugMiddleware;

require dirname(__DIR__). '/vendor/autoload.php';

$app = new Clim\App();
$app = new App();

$app->opt('-u|--upper');
$app->arg('name')->default('unknown');
Expand All @@ -12,7 +15,9 @@

if ($opts['u']) $name = strtoupper($name);

echo "Welcome, {$name}\n";
echo "Welcome, {$name} {$unknown}\n"; // <-- this will invoke notice.
});

$app->add(DebugMiddleware::class);

$app->run();

0 comments on commit e117ff8

Please sign in to comment.