Created
January 27, 2012 09:01
-
-
Save kozo/1687860 to your computer and use it in GitHub Desktop.
CakePHP2-mergeVars
This file contains 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
/** | |
* コンストラクタ | |
* | |
* @access public | |
* @author sakuragawa | |
*/ | |
public function __construct($request = null, $response = null) { | |
// サブクラスのメンバをマージする | |
$this->__mergeSubClass(); | |
parent::__construct($request, $response); | |
} | |
/** | |
* サブクラスのメンバをマージする | |
* | |
* @access private | |
* @author sakuragawa | |
*/ | |
private function __mergeSubClass() { | |
// マージクラス名を保存 | |
$defaultMergeParent = $this->_mergeParent; | |
// サブクラスを取得 | |
$classList = $this->__getClassTree(); | |
foreach ($classList as $key=>$val) | |
{ | |
// マージクラス名を切り替え | |
$this->_mergeParent = $val; | |
// パラメータをマージ | |
$this->_mergeControllerVars(); | |
} | |
// マージクラス名を復元 | |
$this->_mergeParent = $defaultMergeParent; | |
} | |
/** | |
* クラスの継承関係を取得する | |
* | |
* @access private | |
* @author sakuragawa | |
* @memo 自分自身のクラスとAppController以上のクラスはマージ対象としない | |
*/ | |
private function __getClassTree() { | |
$classTree = array(); | |
$className = null; | |
if (is_object($this)) { | |
$className = get_class($this); | |
while ($className = get_parent_class($className)) { | |
if ($className == 'AppController') { | |
break; | |
} | |
// マージ対象クラス名を保存 | |
$classTree[] = $className; | |
} | |
} | |
return $classTree; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment