Created
December 25, 2014 11:39
-
-
Save BlackScorp/efb75adbcac5d8e343da 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
use Silex\Application; | |
use Silex\CallbackResolver; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\HttpKernel\Event\KernelEvent; | |
use Symfony\Component\HttpKernel\HttpKernelInterface; | |
use Symfony\Component\HttpKernel\KernelEvents; | |
class BeforeAfterListener implements EventSubscriberInterface | |
{ | |
/** | |
* @var Application | |
*/ | |
private $app; | |
/** | |
* @var CallbackResolver | |
*/ | |
private $resolver; | |
public function __construct(Application $app, CallbackResolver $resolver) | |
{ | |
$this->resolver = $resolver; | |
$this->app = $app; | |
} | |
public function onRequest(GetResponseEvent $event) | |
{ | |
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { | |
return; | |
} | |
$callBack = $this->getCallback($event, 'before'); | |
if(!$callBack){ | |
return; | |
} | |
$parameters = [ | |
$event->getRequest(), | |
$this->app | |
]; | |
$response = call_user_func_array($callBack,$parameters); | |
if ($response instanceof Response) { | |
$event->setResponse($response); | |
} | |
} | |
private function getCallback(KernelEvent $event, $prefix) | |
{ | |
$controller = $event->getRequest()->get('_controller'); | |
$callback = $this->resolver->resolveCallback($controller); | |
$object = $callback[0]; | |
$methodName = $prefix . ucfirst($callback[1]); | |
$newCallBack = [ | |
$object, | |
$methodName | |
]; | |
if (!method_exists($object, $methodName)) { | |
return null; | |
} | |
return $newCallBack; | |
} | |
public function onResponse(FilterResponseEvent $event) | |
{ | |
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { | |
return; | |
} | |
$callBack = $this->getCallback($event, 'after'); | |
if(!$callBack){ | |
return; | |
} | |
$parameters = [ | |
$event->getRequest(), | |
$event->getResponse(), | |
$this->app | |
]; | |
$response = call_user_func_array($callBack,$parameters); | |
if ($response instanceof Response) { | |
$event->setResponse($response); | |
} | |
} | |
public static function getSubscribedEvents() | |
{ | |
return array( | |
KernelEvents::REQUEST => 'onRequest', | |
KernelEvents::RESPONSE => 'onResponse' | |
); | |
} | |
} |
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
require_once __DIR__.'/vendor/autoload.php'; | |
$app = new Silex\Application(); | |
$app['dispatcher']->addSubscriber(new BeforeAfterListener($app,$app['callback_resolver'])); | |
$app['controllerIndex'] = $app->share(function() use($app){ | |
return new IndexController(); | |
}); | |
$app->match('/','controllerIndex:indexAction')->method('GET'); | |
$app->run(); |
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
use Silex\Application; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
class IndexController | |
{ | |
public function beforeIndexAction(Request $request,Application $app){ | |
//do some stuffs before indexAction | |
} | |
public function afterIndexAction(Request $request,Response $response,Application $app){ | |
//do some stuffs after indexAction | |
} | |
public function indexAction() | |
{ | |
//do some stuffs | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment