Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoload static files without copy/paste #3

Merged
merged 4 commits into from
Jan 9, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"zendframework/zend-diactoros": "^1.1.3"
},
"require-dev": {
"phpunit/phpunit": "^4.8.6"
"phpunit/phpunit": "^4.8.6",
"mikey179/vfsStream": "^1.6"
},
"autoload": {
"psr-4": {
Expand Down
51 changes: 51 additions & 0 deletions src/PhpDebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\Serializer;

Expand Down Expand Up @@ -38,6 +40,10 @@ public function __construct(DebugBarRenderer $debugbarRenderer)
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
{
if ($staticFile = $this->getStaticFile($request->getUri())) {
return $staticFile;
}

$outResponse = $out($request, $response);

if (!$this->isHtmlAccepted($request)) {
Expand Down Expand Up @@ -65,6 +71,51 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return new HtmlResponse($result);
}

/**
* @param UriInterface $uri
*
* @return ResponseInterface|null
*/
private function getStaticFile(UriInterface $uri)
{
if (strpos($uri->getPath(), $this->debugBarRenderer->getBaseUrl()) !== 0) {
return;
}

$pathToFile = substr($uri->getPath(), strlen($this->debugBarRenderer->getBaseUrl()));

$fullPathToFile = $this->debugBarRenderer->getBasePath() . $pathToFile;

if (!file_exists($fullPathToFile)) {
return;
}

$staticResponse = new Response();
$staticResponse->getBody()->write(file_get_contents($fullPathToFile));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better: create a new stream instance and pass it to the response on creation. When creating the stream instance, give it the path to the file:

$stream = new Stream($fullPathToFile, 'r');
$staticResponse = new Response($stream);

That way you don't have any memory overhead from reading the file, and keep I/O down to a minimum.


$contentType = $this->getContentTypeByFileName($fullPathToFile);

return $staticResponse->withHeader('Content-type', $contentType);
}

/**
* @param string $filename
*
* @return string
*/
private function getContentTypeByFileName($filename)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);

switch ($ext) {
case 'css':
return 'text/css';
case 'js':
return 'text/javascript';
}
return 'text/plain';
}

/**
* @param ResponseInterface $response
*
Expand Down
62 changes: 61 additions & 1 deletion test/PhpDebugBarMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
namespace PhpMiddlewareTest\PhpDebugBar;

use DebugBar\JavascriptRenderer;
use org\bovigo\vfs\vfsStream;
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
use PHPUnit_Framework_TestCase;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;

/**
* PhpDebugBarMiddlewareTest
*
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
class PhpDebugBarMiddlewareTest extends \PHPUnit_Framework_TestCase
class PhpDebugBarMiddlewareTest extends PHPUnit_Framework_TestCase
{
protected $debugbarRenderer;
protected $middleware;
Expand Down Expand Up @@ -101,4 +104,61 @@ public function testAppendsToEndOfHtmlResponse()
$this->assertSame($response, $result);
$this->assertSame($html . 'RenderHeadRenderBody', (string) $result->getBody());
}

public function testTryToHandleNotExistingStaticFile()
{
$this->debugbarRenderer->expects($this->any())->method('getBaseUrl')->willReturn('/phpdebugbar');

$uri = new Uri('http://example.com/phpdebugbar/boo.css');
$request = new ServerRequest([], [], $uri, null, 'php://memory');
$response = new Response\HtmlResponse('<html></html>');

$calledOut = false;
$outFunction = function ($request, $response) use (&$calledOut) {
$calledOut = true;
return $response;
};

$result = call_user_func($this->middleware, $request, $response, $outFunction);
$this->assertTrue($calledOut, 'Out is not called');
$this->assertSame($response, $result);
}

/**
* @dataProvider getContentTypes
*/
public function testHandleStaticFile($extension, $contentType)
{
$root = vfsStream::setup('boo');

$this->debugbarRenderer->expects($this->any())->method('getBaseUrl')->willReturn('/phpdebugbar');
$this->debugbarRenderer->expects($this->any())->method('getBasePath')->willReturn(vfsStream::url('boo'));

$uri = new Uri(sprintf('http://example.com/phpdebugbar/debugbar.%s', $extension));
$request = new ServerRequest([], [], $uri, null, 'php://memory');
$response = new Response\HtmlResponse('<html></html>');

vfsStream::newFile(sprintf('debugbar.%s', $extension))->withContent('filecontent')->at($root);

$calledOut = false;
$outFunction = function ($request, $response) use (&$calledOut) {
$calledOut = true;
return $response;
};

$result = call_user_func($this->middleware, $request, $response, $outFunction);
$this->assertFalse($calledOut, 'Out is called');
$this->assertNotSame($response, $result);
$this->assertSame($contentType, $result->getHeaderLine('Content-type'));
$this->assertSame('filecontent', (string) $result->getBody());
}

public function getContentTypes()
{
return [
['css', 'text/css'],
['js', 'text/javascript'],
['html', 'text/plain'],
];
}
}