Last active
December 15, 2015 16:59
-
-
Save polidog/5292947 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 | |
/** | |
* 実行したいメソッドに対してフィルター機能を提供する | |
* @version 0.0.1 | |
* @author polidog <[email protected]> | |
* | |
* @property mixed $object 実行するクラスのインスタンス | |
* @property boolean $isExecuteProtectMethod private, protectedなメソッドを実行するかのフラグ | |
*/ | |
abstract class MethodFilter { | |
protected $object; | |
protected $isExecuteProtectMethod = false; | |
/** | |
* コンストラクタ | |
* @param object $object | |
* @throws MethodFilterExecption | |
*/ | |
public function __construct($object) { | |
if (is_object($object)) { | |
$this->object = $object; | |
} else { | |
throw new MethodFilterExecption('no set object'); | |
} | |
} | |
/** | |
* メソッドが実行された場合の動作 | |
* @param string $name | |
* @param array $arguments | |
* @return mixed | |
* @throws MethodFilterExecption | |
*/ | |
public function __call($name, $arguments) { | |
$this->callPreFilter($name, $arguments); | |
if (!$this->isExecuteMethod($name)) { | |
if (!$this->isExecuteProtectMethod) { | |
throw new MethodFilterExecption('method not exist! name:' . $name); | |
} | |
$method = new ReflectionMethod($this->object, $name); | |
$method->setAccessible(true); | |
$return = $method->invokeArgs($this->object, $arguments); | |
} else { | |
$return = call_user_func_array(array($this->object, $name), $arguments); | |
} | |
$return = $this->callPostFilter($name, $arguments, $return); | |
return $return; | |
} | |
/** | |
* インスタンス変数でそのままコールされた場合の処理 | |
* @param string $name | |
* @param array $arguments | |
* @return mixed | |
*/ | |
public function __invoke($name, $arguments = array()) { | |
return $this->__call($name, $arguments); | |
} | |
/** | |
* クラス変数をセットする | |
* @param string $name 変数名 | |
* @param mixed $value 変数にセットする名前 | |
*/ | |
public function __set($name, $value) { | |
if ($name == 'isExecuteProtectMethod') { | |
if (is_bool($value)) { | |
$this->isExecuteProtectMethod = $value; | |
} | |
} | |
} | |
/** | |
* 実行できるメソッドか判定する | |
* @param string $name | |
* @return boolean | |
*/ | |
protected function isExecuteMethod($name) { | |
return is_callable(array($this->object, $name)); | |
} | |
/** | |
* 実行前フィルタークラス | |
* @param string $name 実行するメソッド名 | |
* @param array $arguments 実行するメソッドの引数 | |
*/ | |
abstract protected function callPreFilter($name, $arguments); | |
/** | |
* 実行後フィルタークラス | |
* @param string $name 実行したメソッド名 | |
* @param array $arguments 実行したメソッドの引数 | |
*/ | |
abstract protected function callPostFilter($name, $arguments, $return); | |
} | |
class MethodFilterExecption extends ErrorException { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
詳しい使い方はここに記載してます。
http://www.polidog.jp/2013/04/03/php%E3%81%A7%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E3%81%8C%E5%AE%9F%E8%A1%8C%E3%81%95%E3%82%8C%E3%81%9F%E6%99%82%E3%81%AB%E3%83%95%E3%82%A3%E3%83%AB%E3%82%BF%E3%83%BC%E3%82%92%E5%AE%9F%E8%A1%8C/