Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Sep 28, 2016
1 parent d4fd109 commit 5b15bb7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/Debugbar.php
Expand Up @@ -276,4 +276,8 @@ public function dispatch()

return ob_get_clean();
}

/*
*
*/
}
33 changes: 18 additions & 15 deletions src/Middleware/Dispatch.php
Expand Up @@ -52,21 +52,32 @@ public function handle($request, $next)

switch ($tracyBar) {
case 'css':
$mimeType = 'text/css';
$content = $this->debugbar->dispatchAssets();
$headers = [
'content-type' => 'text/css; charset=utf-8',
// 'cache-control' => 'max-age=86400',
];
break;
case 'js':
$mimeType = 'text/javascript';
$content = $this->debugbar->dispatchAssets();
$headers = [
'content-type' => 'text/javascript; charset=utf-8',
// 'cache-control' => 'max-age=86400',
];
break;
default:
$mimeType = 'text/javascript';
$content = $this->debugbar->dispatch();
$headers = [
'content-type' => 'text/javascript; charset=utf-8',
];
break;
}

return $this->sendStreamedResponse($content, $mimeType);
return $this->sendStreamedResponse($content, array_merge($headers, [
'content-length' => strlen($content),
]));
}
$this->debugbar->dispatch();

return $next($request);
}
Expand All @@ -78,18 +89,10 @@ public function handle($request, $next)
*
* @param string $content
*
* @return \Symfony\Component\HttpFoundation\StreamedResponse
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function sendStreamedResponse($content, $mimeType)
protected function sendStreamedResponse($content, $headers)
{
$headers = [
'content-type' => $mimeType.'; charset=utf-8',
'cache-control' => 'max-age=86400',
'content-length' => strlen($content),
];

return $this->responseFactory->stream(function () use ($content) {
echo $content;
}, 200, $headers);
return $this->responseFactory->make($content, 200, $headers);
}
}
37 changes: 36 additions & 1 deletion src/Panels/ViewPanel.php
Expand Up @@ -2,6 +2,8 @@

namespace Recca0120\LaravelTracy\Panels;

use Illuminate\Support\Collection;

class ViewPanel extends AbstractPanel
{
/**
Expand All @@ -11,6 +13,11 @@ class ViewPanel extends AbstractPanel
*/
protected $views = [];

/**
* $limit.
*/
public $limit = 50;

/**
* subscribe.
*
Expand All @@ -20,14 +27,42 @@ public function subscribe()
{
$this->laravel['events']->listen('composing:*', function ($view) {
$name = $view->getName();
$data = array_except($view->getData(), ['__env', 'app']);
$data = $this->limitCollection(array_except($view->getData(), ['__env', 'app']));

$path = self::editorLink($view->getPath());
preg_match('/href=\"(.+)\"/', $path, $m);
$path = (count($m) > 1) ? '(<a href="'.$m[1].'">source</a>)' : '';
$this->views[] = compact('name', 'data', 'path');
});
}

/**
* limitCollection.
*
* @method limitCollection
*
* @param array $data
*
* @return array
*/
protected function limitCollection($data)
{
$results = [];
foreach ($data as $key => $value) {
if (is_array($value) === true && count($value) > $this->limit) {
$value = array_slice($value, 0, $this->limit);
}

if ($value instanceof Collection && $value->count() > $this->limit) {
$value = $value->take($this->limit);
}

$results[$key] = $value;
}

return $results;
}

/**
* getAttributes.
*
Expand Down
23 changes: 11 additions & 12 deletions tests/Middleware/DispatchTest.php
Expand Up @@ -41,8 +41,8 @@ public function test_handle_dispatch_css()
->shouldReceive('has')->with('_tracy_bar')->once()->andReturn(true)
->shouldReceive('get')->with('_tracy_bar')->once()->andReturn('css');

$responseFactory->shouldReceive('stream')->once()->andReturnUsing(function ($closure) {
$closure();
$responseFactory->shouldReceive('make')->once()->andReturnUsing(function ($content) {
return $content;
});

/*
Expand All @@ -51,8 +51,7 @@ public function test_handle_dispatch_css()
|------------------------------------------------------------
*/

$this->expectOutputString('testing tracy css');
$response = $middleware->handle($request, $next);
$this->assertSame('testing tracy css', $middleware->handle($request, $next));
}

public function test_handle_dispatch_js()
Expand Down Expand Up @@ -82,8 +81,8 @@ public function test_handle_dispatch_js()
->shouldReceive('has')->with('_tracy_bar')->once()->andReturn(true)
->shouldReceive('get')->with('_tracy_bar')->once()->andReturn('js');

$responseFactory->shouldReceive('stream')->once()->andReturnUsing(function ($closure) {
$closure();
$responseFactory->shouldReceive('make')->once()->andReturnUsing(function ($content) {
return $content;
});

/*
Expand All @@ -92,8 +91,7 @@ public function test_handle_dispatch_js()
|------------------------------------------------------------
*/

$this->expectOutputString('testing tracy js');
$response = $middleware->handle($request, $next);
$this->assertSame('testing tracy js', $middleware->handle($request, $next));
}

public function test_handle_dispatch()
Expand Down Expand Up @@ -125,8 +123,8 @@ public function test_handle_dispatch()
->shouldReceive('has')->with('_tracy_bar')->once()->andReturn(true)
->shouldReceive('get')->with('_tracy_bar')->once()->andReturn('content.abcde');

$responseFactory->shouldReceive('stream')->once()->andReturnUsing(function ($closure) {
$closure();
$responseFactory->shouldReceive('make')->once()->andReturnUsing(function ($content) {
return $content;
});

/*
Expand All @@ -135,8 +133,7 @@ public function test_handle_dispatch()
|------------------------------------------------------------
*/

$this->expectOutputString('testing dispatch');
$response = $middleware->handle($request, $next);
$this->assertSame('testing dispatch', $middleware->handle($request, $next));
}

public function test_handle_next()
Expand All @@ -162,6 +159,8 @@ public function test_handle_next()
|------------------------------------------------------------
*/

$debugbar->shouldReceive('dispatch')->once();

$request->shouldReceive('has')->with('_tracy_bar')->once()->andReturn(false);

/*
Expand Down

0 comments on commit 5b15bb7

Please sign in to comment.