Skip to content

Instantly share code, notes, and snippets.

@mmenozzi
Created March 6, 2014 11:39
Show Gist options
  • Save mmenozzi/9387833 to your computer and use it in GitHub Desktop.
Save mmenozzi/9387833 to your computer and use it in GitHub Desktop.
<?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/';
}
}
@mmenozzi
Copy link
Author

mmenozzi commented Mar 6, 2014

Occhio alla priority!

<service id="acme.static_site.listener" class="Acme\DemoBundle\EventListener\StaticSiteListener">
        <tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="255" />
</service>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment