From 2f3cc8f16ecd9bfb55e847b5245faf949ce54a84 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 25 Mar 2013 14:37:25 -0400 Subject: [PATCH] finished ActionFilter. --- framework/base/ActionFilter.php | 43 +++++++++++++++++++++++++++++++++++++---- todo.md | 17 ---------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/framework/base/ActionFilter.php b/framework/base/ActionFilter.php index 0ae7ab8..1f82e5d 100644 --- a/framework/base/ActionFilter.php +++ b/framework/base/ActionFilter.php @@ -30,8 +30,8 @@ class ActionFilter extends Behavior public function events() { return array( - 'beforeAction' => 'beforeAction', - 'afterAction' => 'afterAction', + 'beforeAction' => 'beforeFilter', + 'afterAction' => 'afterFilter', ); } @@ -39,8 +39,11 @@ class ActionFilter extends Behavior * @param ActionEvent $event * @return boolean */ - public function beforeAction($event) + public function beforeFilter($event) { + if ($this->isActive($event->action)) { + $event->isValid = $this->beforeAction($event->action); + } return $event->isValid; } @@ -48,8 +51,40 @@ class ActionFilter extends Behavior * @param ActionEvent $event * @return boolean */ - public function afterAction($event) + public function afterFilter($event) { + if ($this->isActive($event->action)) { + $this->afterAction($event->action); + } + } + + /** + * This method is invoked right before an action is to be executed (after all possible filters.) + * You may override this method to do last-minute preparation for the action. + * @param Action $action the action to be executed. + * @return boolean whether the action should continue to be executed. + */ + public function beforeAction($action) + { + return true; + } + + /** + * This method is invoked right after an action is executed. + * You may override this method to do some postprocessing for the action. + * @param Action $action the action just executed. + */ + public function afterAction($action) + { + } + /** + * Returns a value indicating whether the filer is active for the given action. + * @param Action $action the action being filtered + * @return boolean whether the filer is active for the given action. + */ + protected function isActive($action) + { + return !in_array($action->id, $this->except, true) && (empty($this->only) || in_array($action->id, $this->only, true)); } } \ No newline at end of file diff --git a/todo.md b/todo.md index f102f41..f66d3c1 100644 --- a/todo.md +++ b/todo.md @@ -1,21 +1,4 @@ -- console - * If console is executed using Windows, do not use colors. If not, use colors. Allow to override via console application settings. -- db - * pgsql, sql server, oracle, db2 drivers - * unit tests on different DB drivers - * document-based (should allow storage-specific methods additionally to generic ones) - * mongodb (put it under framework/db/mongodb) - * key-value-based (should allow storage-specific methods additionally to generic ones) - * redis (put it under framework/db/redis or perhaps framework/caching?) -- base - * TwigViewRenderer (Alex) - * SmartyViewRenderer (Alex) -- logging - * WebTarget (TBD after web is in place): should consider using javascript and make it into a toolbar - * ProfileTarget (TBD after web is in place): should consider using javascript and make it into a toolbar - * unit tests - caching - * backend-specific unit tests * dependency unit tests - validators * Refactor validators to add validateValue() for every validator, if possible. Check if value is an array.