Created
March 6, 2014 11:39
-
-
Save mmenozzi/9387833 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Manuele Menozzi <[email protected]> | |
*/ | |
namespace Acme\DemoBundle\EventListener; | |
use Symfony\Component\HttpFoundation\BinaryFileResponse; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\HttpKernel\HttpKernel; | |
class StaticSiteListener | |
{ | |
public function onKernelRequest(GetResponseEvent $event) | |
{ | |
if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) { | |
// non fare niente se non si è nella richiesta principale | |
return; | |
} | |
$rootDir = "/tmp/sf2/app"; | |
$requestUri = $event->getRequest()->getRequestUri(); | |
if ($requestUri == '/') { | |
$requestUri = '/index.php'; | |
} | |
$includeFile = "$rootDir/../web/webgriffe-v10$requestUri"; | |
if (!file_exists($includeFile) || !is_file($includeFile)) { | |
return; | |
} | |
if (substr_compare($includeFile, '.php', -4) === 0) { | |
ob_start(); | |
include $includeFile; | |
$responseContent = ob_get_contents(); | |
ob_end_clean(); | |
$event->setResponse(new Response($responseContent)); | |
return; | |
} | |
$binaryFileResponse = new BinaryFileResponse($includeFile); | |
if (substr_compare($includeFile, '.css', -4) === 0) { | |
$binaryFileResponse->headers->set('Content-Type', 'text/css'); | |
} | |
if (substr_compare($includeFile, '.js', -3) === 0) { | |
$binaryFileResponse->headers->set('Content-Type', 'text/javascript'); | |
} | |
$event->setResponse($binaryFileResponse); | |
} | |
public function getBaseUrl() | |
{ | |
return 'http://localhost:8000/'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Occhio alla priority!