Skip to content

Instantly share code, notes, and snippets.

@co3k
Created June 29, 2012 06:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save co3k/3016197 to your computer and use it in GitHub Desktop.
Save co3k/3016197 to your computer and use it in GitHub Desktop.
symfony 1.4.x は *.method_not_found というイベントを用意する時点でこういうのをフレームワークとして用意して欲しかったニャー (ああでもこの手の奴は PHP 5.3.x 前提だから無理ってことですねはい)
<?php
// 想像を形にしてみただけなので動かないかもよ
class sfInjectedMethod
{
protected $method, $callable;
public __construct($method, $callable)
{
$this->method = $method;
$this->callable = $callable;
}
public function __invoke(sfEvent $event)
{
if ($this->method !== $event['method'])
{
return false;
}
try
{
$result = $this->callable($event);
}
catch (BadMethodCallException $e)
{
return false;
}
$event->setReturnValue($result);
return true;
}
}
// Usage:
$dispatcher->connect('request.method_not_found', new sfInjectedMethod('getSpecialInformation', function(sfEvent $event) {
return $event->getSubject()->getHttpHeader('X-Co3k');
});
var_dump($request->getSpecialInformation()); // is now available method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment