Skip to content

Commit

Permalink
Merge pull request #21 from arubacao/master
Browse files Browse the repository at this point in the history
Prevent errors in middleware using fullnamespace from facade
  • Loading branch information
lsrur authored Oct 13, 2016
2 parents ecfe9f7 + 50bf5c1 commit 0983a4f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 52 deletions.
86 changes: 55 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
[comment]:$
## <a name="installation"></a>Installation

**This package was tested under PHP 5.6, PHP 7, Laravel 5.2 and Laravel 5.3-Dev**
**This package was tested under PHP 5.6, PHP 7, Laravel 5.2 and Laravel 5.3-Dev**

Installing the package via composer:

Expand All @@ -68,13 +68,26 @@ Next, add InspectorServiceProvider to the providers array in `config/app.php`:
```php
Lsrur\Inspector\InspectorServiceProvider::class,
```

And this Facade in the same configuration file:

```php
'Inspector' => Lsrur\Inspector\Facade\Inspector::class,
```

For usage only during development and not during production,
do **not** edit the `config/app.php` and add the following to your `AppServiceProvider` :

```php
public function register()
{
// ...
if ($this->app->environment() == 'development') {
$this->app->register(\Lsrur\Inspector\InspectorServiceProvider::class);
}
// ...
}
```

## <a name="configuration"></a>Configuration

Expand All @@ -88,39 +101,50 @@ public function render($request, Exception $exception)
}
```


For usage only during development:
```
public function render($request, Exception $exception)
{
if (\App::environment() == 'development')
{
\Lsrur\Inspector\Facade\Inspector::renderException($exception);
}
return parent::render($request, $exception);
}
```

## <a name="usage"></a>Usage
Laravel inspector can be invoked using the Facade, the provided helper functions and a Blade directive:

```php
//Using Facade
\Inspector::log(...);
\Inspector::info(...);

//Using the "inspector" helper function
inspector()->info(...);
inspector()->warning($myVar);

// "li" is an alias of "inspector"
li()->error(...);
li()->group('myGroup');

// "inspect" function makes an "Inspector::log($v)" for each passed argument
inspect($var1, $var2, $var3, ...);

// Dump and die using Laravel Inspector magic
idd();
idd($var1, $var2);

// Inside Blade
@li(cmd,param1,param2,...)

// samples
@li('log', 'My comment', $myVar)
@li(log, 'My comment', $myVar) //also works without command quotes
@li(group,"myGroup")
@li(endGroup)
```
```

**Laravel inspector will only be active if the config variable `app.debug` is true.**
Anyway, you can temporarily turn Inspector off (just for the current request) with:
Expand All @@ -145,21 +169,21 @@ You can inspect objects and variables with the following methods, each of which

Examples:

```php
```php
li()->log("MyData", $myData);
li()->info($myData);
li()->table("clients", $myClientsCollection);
li()->table("clients", $myClientsCollection);
```
Additionally, you can use the "inspect" helper function to quickly inspect objects and variables.

```php
```php
inspect($var1, $var2, $var3,...);
```

#### Grouping Messages
Laravel Inspector allows you to group messages into nodes and subnodes:

```php
```php
li()->group('MyGroup');
li()->info($data);
li()->group('MySubGroup');
Expand All @@ -182,56 +206,56 @@ In addition to the ability to group information, each group and subgroup excecut

Examples:

```php
```php
li()->time("MyTimer");
// ...
li()->timeEnd("MyTimer");
li()->timeStamp('Elapsed time from LARAVEL_START here');

li()->timeStamp('Elapsed time from LARAVEL_START here');
```

## <a name="redirects"></a>Redirects
Laravel Inspector handles redirects smoothly; showing the collectors bag for both, the original and the target views.

## <a name="dump"></a>Dump and die

The <code>dd()</code> method (or <code>idd()</code> helper) will dump the entire collectors bag and terminates the script:
The <code>dd()</code> method (or <code>idd()</code> helper) will dump the entire collectors bag and terminates the script:

```php
```php
\Inspector::dd();
li()->dd();

// or simply
idd();

// adding last minute data
idd($var1, $var2,...)
```

As the rest of the package, this feature intelligently determines how will be the format of the output, even if the call was performed from CLI.
As the rest of the package, this feature intelligently determines how will be the format of the output, even if the call was performed from CLI.

Another way to make an inspection, but without interrupting the flow of the request/response, is by adding the parameter <code>laravel_inspector=dump</code> to the URL:

<code>http://myapp.dev/contacts?id=1&laravel_inspector=dump</code>

Thus, Laravel Inspector wont be activated until the a terminable middleware is reached.

## <a name="exceptions"></a>Exceptions

The function <code>addException()</code> will inspect our caught exceptions:

```php
```php
try {
...
} catch (Exception $e) {
li()->addException($e);
}
```

Optionally, you can setup LI as the default exception renderer during development time (app.debug=true). Refer to the [configuration](#configuration) to do so.
Optionally, you can setup LI as the default exception renderer during development time (app.debug=true). Refer to the [configuration](#configuration) to do so.

## <a name="requests"></a>VIEW/AJAX/API requests, how it works

## <a name="requests"></a>VIEW/AJAX/API requests, how it works

Laravel Inspector (LI) automatically detects the type of the request/response pair and determines the output format. If a View response is detected, the code needed to elegantly show the collected information in the browser console will be injected as a javascript into that view. Along with this, LI will also add a small piece of pure javascript code that serves as a generic http interceptor, which will examine subsequent AJAX calls looking for information injected by LI
(this interceptor was tested under pure javascript, Angular 1.x ($http) and jQuery ($.ajax) and should work with any js framework). The interceptor also adds a header in each client AJAX call to let LI know that the interceptor is present. Then, from Laravel side, during an AJAX request or a JSON response, LI will send a script to be interpreted (and properly rendered in the browsers console) by the interceptor, OR a pure Json if that header is not present and then assuming that the request was sent from cURL, a REST client app or something else.

Expand Down Expand Up @@ -268,16 +292,16 @@ XHR.prototype.send = function(data) {
if(this.addEventListener) {
this.addEventListener("readystatechange", onReadyStateChange, false);
} else {
oldOnReadyStateChange = this.onreadystatechange;
oldOnReadyStateChange = this.onreadystatechange;
this.onreadystatechange = onReadyStateChange;
}
}
send.call(this, data);
}
})(XMLHttpRequest);
```

[comment]: $

## <a name="license"></a>License
Laravel Inspector is licensed under the [MIT License](https://opensource.org/licenses/MIT).
26 changes: 13 additions & 13 deletions src/InspectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ class InspectorServiceProvider extends ServiceProvider
*/
public function boot()
{



app()->booted(function(){

if(!defined('LARAVEL_BOOTED')) {
define('LARAVEL_BOOTED', microtime(true));
}
});
});

// \View::composer('*', function($view)
// {
// // prifile views?
// });
// });

\Blade::directive('li', function($args) {

$args = explode(',',str_replace(["(", ")"],'', $args));
$cmd = str_replace(["'", '"'], '', $args[0]);
array_shift($args);
$args = implode(',',$args);
return "<?php li()->$cmd($args); ?>";
});


if(\DB::connection()->getDatabaseName())
{
{
\DB::listen(function($sql) {
\Inspector::addSql($sql);
\Lsrur\Inspector\Facade\Inspector::addSql($sql);
});
}

Expand All @@ -51,15 +51,15 @@ public function boot()
// The package views have not been published. Use the defaults.
$this->loadViewsFrom(__DIR__.'/views', 'inspector');
}

$kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');
$kernel->pushMiddleware('Lsrur\Inspector\Middleware\Inspector');

$this->publishes([
__DIR__.'/config/inspector.php' => config_path('inspector.php')], 'config');

$this->mergeConfigFrom(__DIR__.'/config/inspector.php', 'inspector');

$this->mergeConfigFrom(__DIR__.'/config/inspector.php', 'inspector');

}

Expand Down
16 changes: 8 additions & 8 deletions src/Middleware/Inspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ public function handle($request, Closure $next)
} catch (Exception $e) {
$response = $this->handleException($request, $e);
}
if(\Inspector::isOn())
{

if(\Lsrur\Inspector\Facade\Inspector::isOn())
{
if($request->get('laravel_inspector')=='dd')
{
\inspector::dd();
\Lsrur\Inspector\Facade\Inspector::dd();
} elseif($request->get('laravel_inspector')=='off') {
// do nothing
} elseif($request->get('laravel_inspector')=='dump') {
\inspector::analize($request, $response);

\Lsrur\Inspector\Facade\Inspector::analize($request, $response);

} else {
\Inspector::injectInspection($request, $response);
\Lsrur\Inspector\Facade\Inspector::injectInspection($request, $response);
}
}
return $response;
}

}
}

0 comments on commit 0983a4f

Please sign in to comment.