<?php

namespace Neag\MainBundle\EventListener;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\SecurityContextInterface; 
//use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
//use Symfony\Component\HttpFoundation\Response;

/**
 * @author Matt Drollette <matt@drollette.com>
 */
class BeforeControllerListener
{

    public function onKernelController(FilterControllerEvent $event)
    {
        $controller = $event->getController(); 
        
        if (!is_array($controller)) {
            // not a object but a different kind of callable. Do nothing
            return;
        } 

        $controllerObject = $controller[0];       

        // skip initializing for exceptions
        if ($controllerObject instanceof ExceptionController) {            
            return;
        } else if (is_subclass_of($controllerObject, 'Neag\MainBundle\Controller\ControllerAbstract')) { 
            //$controllerObject->trace('initialize');
            $controllerObject->initialize();
        } else {
            //$controllerObject->trace(get_class($controllerObject));
        }   
    }
    
    
    /*
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        $response = new Response();
        // setup the Response object based on the caught exception
        $event->setResponse($response);
        
        exit;
        die();

        // you can alternatively set a new Exception
        // $exception = new \Exception('Some special exception');
        // $event->setException($exception);
    }
    */
}