Skip to content

Commit e9b7f6d

Browse files
committed
Merge pull request #3 from php-middleware/autoload-static-files
Autoload static files without copy/paste
2 parents 502ca1d + 4835424 commit e9b7f6d

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"zendframework/zend-diactoros": "^1.1.3"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "^4.8.6"
18+
"phpunit/phpunit": "^4.8.6",
19+
"mikey179/vfsStream": "^1.6"
1920
},
2021
"autoload": {
2122
"psr-4": {

src/PhpDebugBarMiddleware.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
use Psr\Http\Message\MessageInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\ServerRequestInterface;
9+
use Psr\Http\Message\UriInterface;
10+
use Zend\Diactoros\Response;
911
use Zend\Diactoros\Response\HtmlResponse;
1012
use Zend\Diactoros\Response\Serializer;
13+
use Zend\Diactoros\Stream;
1114

1215
/**
1316
* PhpDebugBarMiddleware
@@ -38,6 +41,10 @@ public function __construct(DebugBarRenderer $debugbarRenderer)
3841
*/
3942
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
4043
{
44+
if ($staticFile = $this->getStaticFile($request->getUri())) {
45+
return $staticFile;
46+
}
47+
4148
$outResponse = $next($request, $response);
4249

4350
if (!$this->isHtmlAccepted($request)) {
@@ -65,6 +72,59 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
6572
return new HtmlResponse($result);
6673
}
6774

75+
/**
76+
* @param UriInterface $uri
77+
*
78+
* @return ResponseInterface|null
79+
*/
80+
private function getStaticFile(UriInterface $uri)
81+
{
82+
if (strpos($uri->getPath(), $this->debugBarRenderer->getBaseUrl()) !== 0) {
83+
return;
84+
}
85+
86+
$pathToFile = substr($uri->getPath(), strlen($this->debugBarRenderer->getBaseUrl()));
87+
88+
$fullPathToFile = $this->debugBarRenderer->getBasePath() . $pathToFile;
89+
90+
if (!file_exists($fullPathToFile)) {
91+
return;
92+
}
93+
94+
$stream = new Stream($fullPathToFile, 'r');
95+
$staticResponse = new Response($stream);
96+
$contentType = $this->getContentTypeByFileName($fullPathToFile);
97+
98+
return $staticResponse->withHeader('Content-type', $contentType);
99+
}
100+
101+
/**
102+
* @param string $filename
103+
*
104+
* @return string
105+
*/
106+
private function getContentTypeByFileName($filename)
107+
{
108+
$ext = pathinfo($filename, PATHINFO_EXTENSION);
109+
110+
$map = [
111+
'css' => 'text/css',
112+
'js' => 'text/javascript',
113+
'otf' => 'font/opentype',
114+
'eot' => 'application/vnd.ms-fontobject',
115+
'svg' => 'image/svg+xml',
116+
'ttf' => 'application/font-sfnt',
117+
'woff' => 'application/font-woff',
118+
'woff2' => 'application/font-woff2',
119+
];
120+
121+
if (isset($map[$ext])) {
122+
return $map[$ext];
123+
}
124+
125+
return 'text/plain';
126+
}
127+
68128
/**
69129
* @param ResponseInterface $response
70130
*

test/PhpDebugBarMiddlewareTest.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
namespace PhpMiddlewareTest\PhpDebugBar;
44

55
use DebugBar\JavascriptRenderer;
6+
use org\bovigo\vfs\vfsStream;
67
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
8+
use PHPUnit_Framework_TestCase;
79
use Zend\Diactoros\Response;
810
use Zend\Diactoros\ServerRequest;
11+
use Zend\Diactoros\Uri;
912

1013
/**
1114
* PhpDebugBarMiddlewareTest
1215
*
1316
* @author Witold Wasiczko <[email protected]>
1417
*/
15-
class PhpDebugBarMiddlewareTest extends \PHPUnit_Framework_TestCase
18+
class PhpDebugBarMiddlewareTest extends PHPUnit_Framework_TestCase
1619
{
1720
protected $debugbarRenderer;
1821
protected $middleware;
@@ -101,4 +104,61 @@ public function testAppendsToEndOfHtmlResponse()
101104
$this->assertSame($response, $result);
102105
$this->assertSame($html . 'RenderHeadRenderBody', (string) $result->getBody());
103106
}
107+
108+
public function testTryToHandleNotExistingStaticFile()
109+
{
110+
$this->debugbarRenderer->expects($this->any())->method('getBaseUrl')->willReturn('/phpdebugbar');
111+
112+
$uri = new Uri('http://example.com/phpdebugbar/boo.css');
113+
$request = new ServerRequest([], [], $uri, null, 'php://memory');
114+
$response = new Response\HtmlResponse('<html></html>');
115+
116+
$calledOut = false;
117+
$outFunction = function ($request, $response) use (&$calledOut) {
118+
$calledOut = true;
119+
return $response;
120+
};
121+
122+
$result = call_user_func($this->middleware, $request, $response, $outFunction);
123+
$this->assertTrue($calledOut, 'Out is not called');
124+
$this->assertSame($response, $result);
125+
}
126+
127+
/**
128+
* @dataProvider getContentTypes
129+
*/
130+
public function testHandleStaticFile($extension, $contentType)
131+
{
132+
$root = vfsStream::setup('boo');
133+
134+
$this->debugbarRenderer->expects($this->any())->method('getBaseUrl')->willReturn('/phpdebugbar');
135+
$this->debugbarRenderer->expects($this->any())->method('getBasePath')->willReturn(vfsStream::url('boo'));
136+
137+
$uri = new Uri(sprintf('http://example.com/phpdebugbar/debugbar.%s', $extension));
138+
$request = new ServerRequest([], [], $uri, null, 'php://memory');
139+
$response = new Response\HtmlResponse('<html></html>');
140+
141+
vfsStream::newFile(sprintf('debugbar.%s', $extension))->withContent('filecontent')->at($root);
142+
143+
$calledOut = false;
144+
$outFunction = function ($request, $response) use (&$calledOut) {
145+
$calledOut = true;
146+
return $response;
147+
};
148+
149+
$result = call_user_func($this->middleware, $request, $response, $outFunction);
150+
$this->assertFalse($calledOut, 'Out is called');
151+
$this->assertNotSame($response, $result);
152+
$this->assertSame($contentType, $result->getHeaderLine('Content-type'));
153+
$this->assertSame('filecontent', (string) $result->getBody());
154+
}
155+
156+
public function getContentTypes()
157+
{
158+
return [
159+
['css', 'text/css'],
160+
['js', 'text/javascript'],
161+
['html', 'text/plain'],
162+
];
163+
}
104164
}

0 commit comments

Comments
 (0)