From 18be2212708365bf8addb0952d1e0c1587cb6f47 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 1 Feb 2014 02:51:55 +0400 Subject: [PATCH] Refactored debug module, added all missing docs --- extensions/debug/DebugAsset.php | 2 + extensions/debug/LogTarget.php | 7 +++ extensions/debug/Module.php | 21 +++++++- extensions/debug/Panel.php | 5 ++ extensions/debug/components/search/Filter.php | 56 ++++++++++++---------- .../debug/components/search/matchers/Base.php | 40 ++++++++++++++++ .../components/search/matchers/GreaterThan.php | 25 ++++++++++ .../debug/components/search/matchers/LowerThan.php | 25 ++++++++++ .../search/matchers/MatcherInterface.php | 39 +++++++++++++++ .../debug/components/search/matchers/SameAs.php | 34 +++++++++++++ .../debug/components/search/matches/Base.php | 26 ---------- .../debug/components/search/matches/Exact.php | 36 -------------- .../debug/components/search/matches/Greater.php | 27 ----------- .../debug/components/search/matches/Lower.php | 27 ----------- .../components/search/matches/MatcherInterface.php | 25 ---------- extensions/debug/controllers/DefaultController.php | 8 ++++ extensions/debug/models/search/Base.php | 37 ++++++++------ extensions/debug/models/search/Db.php | 23 ++++++--- extensions/debug/models/search/Debug.php | 24 +++++++--- extensions/debug/models/search/Log.php | 21 ++++++-- extensions/debug/models/search/Profile.php | 21 ++++++-- extensions/debug/panels/ConfigPanel.php | 22 +++++++++ extensions/debug/panels/DbPanel.php | 34 ++++++++----- extensions/debug/panels/LogPanel.php | 21 ++++++-- extensions/debug/panels/ProfilingPanel.php | 15 +++++- extensions/debug/panels/RequestPanel.php | 13 ++++- 26 files changed, 411 insertions(+), 223 deletions(-) create mode 100644 extensions/debug/components/search/matchers/Base.php create mode 100644 extensions/debug/components/search/matchers/GreaterThan.php create mode 100644 extensions/debug/components/search/matchers/LowerThan.php create mode 100644 extensions/debug/components/search/matchers/MatcherInterface.php create mode 100644 extensions/debug/components/search/matchers/SameAs.php delete mode 100644 extensions/debug/components/search/matches/Base.php delete mode 100644 extensions/debug/components/search/matches/Exact.php delete mode 100644 extensions/debug/components/search/matches/Greater.php delete mode 100644 extensions/debug/components/search/matches/Lower.php delete mode 100644 extensions/debug/components/search/matches/MatcherInterface.php diff --git a/extensions/debug/DebugAsset.php b/extensions/debug/DebugAsset.php index 2d1b289..da0b91f 100644 --- a/extensions/debug/DebugAsset.php +++ b/extensions/debug/DebugAsset.php @@ -9,6 +9,8 @@ namespace yii\debug; use yii\web\AssetBundle; /** + * Debugger asset bundle + * * @author Qiang Xue * @since 2.0 */ diff --git a/extensions/debug/LogTarget.php b/extensions/debug/LogTarget.php index f1f6dd8..3f0be69 100644 --- a/extensions/debug/LogTarget.php +++ b/extensions/debug/LogTarget.php @@ -72,6 +72,13 @@ class LogTarget extends Target $this->updateIndexFile($indexFile, $summary); } + /** + * Updates index file with summary log data + * + * @param string $indexFile path to index file + * @param array $summary summary log data + * @throws \yii\base\InvalidConfigException + */ private function updateIndexFile($indexFile, $summary) { touch($indexFile); diff --git a/extensions/debug/Module.php b/extensions/debug/Module.php index c113843..7ad9863 100644 --- a/extensions/debug/Module.php +++ b/extensions/debug/Module.php @@ -50,7 +50,9 @@ class Module extends \yii\base\Module */ public $historySize = 50; - + /** + * @inheritdoc + */ public function init() { parent::init(); @@ -69,13 +71,16 @@ class Module extends \yii\base\Module } } + /** + * @inheritdoc + */ public function beforeAction($action) { Yii::$app->getView()->off(View::EVENT_END_BODY, [$this, 'renderToolbar']); unset(Yii::$app->getLog()->targets['debug']); $this->logTarget = null; - if ($this->checkAccess($action)) { + if ($this->checkAccess()) { return parent::beforeAction($action); } elseif ($action->id === 'toolbar') { return false; @@ -84,6 +89,11 @@ class Module extends \yii\base\Module } } + /** + * Renders mini-toolbar at the end of page body. + * + * @param \yii\base\Event $event + */ public function renderToolbar($event) { if (!$this->checkAccess() || Yii::$app->getRequest()->getIsAjax()) { @@ -99,6 +109,10 @@ class Module extends \yii\base\Module echo ''; } + /** + * Checks if current user is allowed to access the module + * @return boolean if access is granted + */ protected function checkAccess() { $ip = Yii::$app->getRequest()->getUserIP(); @@ -111,6 +125,9 @@ class Module extends \yii\base\Module return false; } + /** + * @return array default set of panels + */ protected function corePanels() { return [ diff --git a/extensions/debug/Panel.php b/extensions/debug/Panel.php index f7cad4a..48efec0 100644 --- a/extensions/debug/Panel.php +++ b/extensions/debug/Panel.php @@ -73,6 +73,11 @@ class Panel extends Component return null; } + /** + * Loads data into the panel + * + * @param mixed $data + */ public function load($data) { $this->data = $data; diff --git a/extensions/debug/components/search/Filter.php b/extensions/debug/components/search/Filter.php index 557e12c..55e6ebb 100644 --- a/extensions/debug/components/search/Filter.php +++ b/extensions/debug/components/search/Filter.php @@ -1,33 +1,44 @@ + * @since 2.0 + */ class Filter extends Component { - /** * @var array rules for matching filters in the way: [:fieldName => [rule1, rule2,..]] */ protected $rules = []; /** - * Adds rules for filtering data. Match can be partial or exactly. + * Adds data filtering rule. + * * @param string $name attribute name - * @param \yii\debug\components\search\matches\Base $rule + * @param MatcherInterface $rule */ - public function addMatch($name, $rule) + public function addMatcher($name, MatcherInterface $rule) { - if (empty($rule->value) && $rule->value !== 0) { - return; + if ($rule->hasValue()) { + $this->rules[$name][] = $rule; } - - $this->rules[$name][] = $rule; } /** - * Applies filter on given array and returns filtered data. + * Applies filter on a given array and returns filtered data. + * * @param array $data data to filter * @return array filtered data */ @@ -36,7 +47,7 @@ class Filter extends Component $filtered = []; foreach ($data as $row) { - if ($this->checkFilter($row)) { + if ($this->passesFilter($row)) { $filtered[] = $row; } } @@ -45,28 +56,25 @@ class Filter extends Component } /** - * Check if the given data satisfies filters. - * @param array $row + * Checks if the given data satisfies filters. + * + * @param array $row data + * @return boolean if data passed filtering */ - public function checkFilter(array $row) + private function passesFilter(array $row) { - $matched = true; - foreach ($row as $name => $value) { if (isset($this->rules[$name])) { - - #check all rules for given attribute - + // check all rules for a given attribute foreach ($this->rules[$name] as $rule) { - if (!$rule->check($value)) { - $matched = false; + /** @var MatcherInterface $rule */ + if (!$rule->match($value)) { + return false; } } - } } - return $matched; + return true; } - } diff --git a/extensions/debug/components/search/matchers/Base.php b/extensions/debug/components/search/matchers/Base.php new file mode 100644 index 0000000..411e6c3 --- /dev/null +++ b/extensions/debug/components/search/matchers/Base.php @@ -0,0 +1,40 @@ + + * @since 2.0 + */ +abstract class Base extends Component implements MatcherInterface +{ + /** + * @var mixed base value to check + */ + protected $baseValue; + + /** + * @inheritdoc + */ + public function setValue($value) + { + $this->baseValue = $value; + } + + /** + * @inheritdoc + */ + public function hasValue() + { + return !empty($this->baseValue) || $this->baseValue === 0; + } +} diff --git a/extensions/debug/components/search/matchers/GreaterThan.php b/extensions/debug/components/search/matchers/GreaterThan.php new file mode 100644 index 0000000..486fac4 --- /dev/null +++ b/extensions/debug/components/search/matchers/GreaterThan.php @@ -0,0 +1,25 @@ + + * @since 2.0 + */ +class GreaterThan extends Base +{ + /** + * @inheritdoc + */ + public function match($value) + { + return ($value > $this->baseValue); + } +} diff --git a/extensions/debug/components/search/matchers/LowerThan.php b/extensions/debug/components/search/matchers/LowerThan.php new file mode 100644 index 0000000..018001a --- /dev/null +++ b/extensions/debug/components/search/matchers/LowerThan.php @@ -0,0 +1,25 @@ + + * @since 2.0 + */ +class LowerThan extends Base +{ + /** + * @inheritdoc + */ + public function match($value) + { + return ($value < $this->baseValue); + } +} diff --git a/extensions/debug/components/search/matchers/MatcherInterface.php b/extensions/debug/components/search/matchers/MatcherInterface.php new file mode 100644 index 0000000..febd06d --- /dev/null +++ b/extensions/debug/components/search/matchers/MatcherInterface.php @@ -0,0 +1,39 @@ + + * @since 2.0 + */ +interface MatcherInterface +{ + /** + * Checks if the value passed matches base value. + * + * @param mixed $value value to be matched + * @return boolean if there is a match + */ + public function match($value); + + /** + * Sets base value to match against + * + * @param mixed $value + */ + public function setValue($value); + + /** + * Checks if base value is set + * + * @return boolean if base value is set + */ + public function hasValue(); +} diff --git a/extensions/debug/components/search/matchers/SameAs.php b/extensions/debug/components/search/matchers/SameAs.php new file mode 100644 index 0000000..ba3eedd --- /dev/null +++ b/extensions/debug/components/search/matchers/SameAs.php @@ -0,0 +1,34 @@ + + * @since 2.0 + */ +class SameAs extends Base +{ + /** + * @var boolean if partial match should be used. + */ + public $partial = false; + + /** + * @inheritdoc + */ + public function match($value) + { + if (!$this->partial) { + return (mb_strtolower($this->baseValue, 'utf8') == mb_strtolower($value, 'utf8')); + } else { + return (mb_strpos(mb_strtolower($value, 'utf8'), mb_strtolower($this->baseValue, 'utf8')) !== false); + } + } +} diff --git a/extensions/debug/components/search/matches/Base.php b/extensions/debug/components/search/matches/Base.php deleted file mode 100644 index ce2ceae..0000000 --- a/extensions/debug/components/search/matches/Base.php +++ /dev/null @@ -1,26 +0,0 @@ - - * @since 2.0 - */ -abstract class Base extends Component implements MatcherInterface -{ - - /** - * @var mixed current value to check for the matcher - */ - public $value; - -} diff --git a/extensions/debug/components/search/matches/Exact.php b/extensions/debug/components/search/matches/Exact.php deleted file mode 100644 index 46992e9..0000000 --- a/extensions/debug/components/search/matches/Exact.php +++ /dev/null @@ -1,36 +0,0 @@ - - * @since 2.0 - */ -class Exact extends Base -{ - - /** - * @var boolean if current matcher should consider partial match of given value. - */ - public $partial = false; - - /** - * Checks if the given value is the same as base one or has partial match with base one. - * @param mixed $value - */ - public function check($value) - { - if (!$this->partial) { - return (mb_strtolower($this->value, 'utf8') == mb_strtolower($value, 'utf8')); - } else { - return (mb_strpos(mb_strtolower($value, 'utf8'), mb_strtolower($this->value,'utf8')) !== false); - } - } - -} diff --git a/extensions/debug/components/search/matches/Greater.php b/extensions/debug/components/search/matches/Greater.php deleted file mode 100644 index 7796f6b..0000000 --- a/extensions/debug/components/search/matches/Greater.php +++ /dev/null @@ -1,27 +0,0 @@ - - * @since 2.0 - */ -class Greater extends Base -{ - - /** - * Checks if the given value is the same as base one or has partial match with base one. - * @param mixed $value - */ - public function check($value) - { - return ($value > $this->value); - } - -} diff --git a/extensions/debug/components/search/matches/Lower.php b/extensions/debug/components/search/matches/Lower.php deleted file mode 100644 index 034ea4a..0000000 --- a/extensions/debug/components/search/matches/Lower.php +++ /dev/null @@ -1,27 +0,0 @@ - - * @since 2.0 - */ -class Lower extends Base -{ - - /** - * Checks if the given value is the same as base one or has partial match with base one. - * @param mixed $value - */ - public function check($value) - { - return ($value < $this->value); - } - -} diff --git a/extensions/debug/components/search/matches/MatcherInterface.php b/extensions/debug/components/search/matches/MatcherInterface.php deleted file mode 100644 index 16c0705..0000000 --- a/extensions/debug/components/search/matches/MatcherInterface.php +++ /dev/null @@ -1,25 +0,0 @@ - - * @since 2.0 - */ -interface MatcherInterface -{ - - /** - * Check if the value is correct according current matcher. - * @param mixed $value - */ - public function check($value); - -} diff --git a/extensions/debug/controllers/DefaultController.php b/extensions/debug/controllers/DefaultController.php index 521116c..f4be8a5 100644 --- a/extensions/debug/controllers/DefaultController.php +++ b/extensions/debug/controllers/DefaultController.php @@ -13,11 +13,16 @@ use yii\web\NotFoundHttpException; use yii\debug\models\search\Debug; /** + * Debugger controller + * * @author Qiang Xue * @since 2.0 */ class DefaultController extends Controller { + /** + * @inheritdoc + */ public $layout = 'main'; /** * @var \yii\debug\Module @@ -28,6 +33,9 @@ class DefaultController extends Controller */ public $summary; + /** + * @inheritdoc + */ public function actions() { $actions = []; diff --git a/extensions/debug/models/search/Base.php b/extensions/debug/models/search/Base.php index be9b852..f81c058 100644 --- a/extensions/debug/models/search/Base.php +++ b/extensions/debug/models/search/Base.php @@ -1,37 +1,44 @@ + * @since 2.0 + */ class Base extends Model { - /** - * @param Filter $filter - * @param string $attribute - * @param boolean $partial + * Adds filtering condition for a given attribute + * + * @param Filter $filter filter instance + * @param string $attribute attribute to filter + * @param boolean $partial if partial match should be used */ - public function addCondition($filter, $attribute, $partial = false) + public function addCondition(Filter $filter, $attribute, $partial = false) { $value = $this->$attribute; if (mb_strpos($value, '>') !== false) { - $value = intval(str_replace('>', '', $value)); - $filter->addMatch($attribute, new matches\Greater(['value' => $value])); + $filter->addMatcher($attribute, new matchers\GreaterThan(['value' => $value])); } elseif (mb_strpos($value, '<') !== false) { - $value = intval(str_replace('<', '', $value)); - $filter->addMatch($attribute, new matches\Lower(['value' => $value])); - + $filter->addMatcher($attribute, new matchers\LowerThan(['value' => $value])); } else { - $filter->addMatch($attribute, new matches\Exact(['value' => $value, 'partial' => $partial])); + $filter->addMatcher($attribute, new matchers\SameAs(['value' => $value, 'partial' => $partial])); } - } - } diff --git a/extensions/debug/models/search/Db.php b/extensions/debug/models/search/Db.php index 0a17f2d..90ee26e 100644 --- a/extensions/debug/models/search/Db.php +++ b/extensions/debug/models/search/Db.php @@ -1,4 +1,9 @@ + * @author Mark Jebri + * @since 2.0 */ class Db extends Base { - /** - * @var string type attribute input search value + * @var string type of the input search value */ public $type; @@ -21,6 +29,9 @@ class Db extends Base */ public $query; + /** + * @inheritdoc + */ public function rules() { return [ @@ -41,8 +52,9 @@ class Db extends Base /** * Returns data provider with filled models. Filter applied if needed. - * @param array $params - * @param array $models + * + * @param array $params an array of parameter values indexed by parameter names + * @param array $models data to return provider for * @return \yii\data\ArrayDataProvider */ public function search($params, $models) @@ -69,5 +81,4 @@ class Db extends Base return $dataProvider; } - } diff --git a/extensions/debug/models/search/Debug.php b/extensions/debug/models/search/Debug.php index 8ffcf54..3fbebd2 100644 --- a/extensions/debug/models/search/Debug.php +++ b/extensions/debug/models/search/Debug.php @@ -1,4 +1,9 @@ + * @author Mark Jebri + * @since 2.0 */ class Debug extends Base { @@ -51,6 +60,9 @@ class Debug extends Base */ public $criticalCodes = [400, 404, 500]; + /** + * @inheritdoc + */ public function rules() { return [ @@ -76,8 +88,8 @@ class Debug extends Base /** * Returns data provider with filled models. Filter applied if needed. - * @param array $params - * @param array $models + * @param array $params an array of parameter values indexed by parameter names + * @param array $models data to return provider for * @return \yii\data\ArrayDataProvider */ public function search($params, $models) @@ -110,13 +122,13 @@ class Debug extends Base } /** - * Checks if the code is critical: 400 or greater, 500 or greater. + * Checks if code is critical. + * * @param integer $code - * @return bool + * @return boolean */ public function isCodeCritical($code) { return in_array($code, $this->criticalCodes); } - } diff --git a/extensions/debug/models/search/Log.php b/extensions/debug/models/search/Log.php index 718476f..ba19c5a 100644 --- a/extensions/debug/models/search/Log.php +++ b/extensions/debug/models/search/Log.php @@ -1,4 +1,9 @@ + * @author Mark Jebri + * @since 2.0 */ class Log extends Base { - /** * @var string ip attribute input search value */ @@ -26,6 +34,9 @@ class Log extends Base */ public $message; + /** + * @inheritdoc + */ public function rules() { return [ @@ -47,8 +58,9 @@ class Log extends Base /** * Returns data provider with filled models. Filter applied if needed. - * @param array $params - * @param array $models + * + * @param array $params an array of parameter values indexed by parameter names + * @param array $models data to return provider for * @return \yii\data\ArrayDataProvider */ public function search($params, $models) @@ -73,5 +85,4 @@ class Log extends Base return $dataProvider; } - } diff --git a/extensions/debug/models/search/Profile.php b/extensions/debug/models/search/Profile.php index 4f4a170..f39f4ca 100644 --- a/extensions/debug/models/search/Profile.php +++ b/extensions/debug/models/search/Profile.php @@ -1,4 +1,9 @@ + * @author Mark Jebri + * @since 2.0 */ class Profile extends Base { - /** * @var string method attribute input search value */ @@ -21,6 +29,9 @@ class Profile extends Base */ public $info; + /** + * @inheritdoc + */ public function rules() { return [ @@ -41,8 +52,9 @@ class Profile extends Base /** * Returns data provider with filled models. Filter applied if needed. - * @param array $params - * @param array $models + * + * @param array $params an array of parameter values indexed by parameter names + * @param array $models data to return provider for * @return \yii\data\ArrayDataProvider */ public function search($params, $models) @@ -69,5 +81,4 @@ class Profile extends Base return $dataProvider; } - } diff --git a/extensions/debug/panels/ConfigPanel.php b/extensions/debug/panels/ConfigPanel.php index 8e1b9ed..9f85ced 100644 --- a/extensions/debug/panels/ConfigPanel.php +++ b/extensions/debug/panels/ConfigPanel.php @@ -18,26 +18,45 @@ use yii\debug\Panel; */ class ConfigPanel extends Panel { + /** + * @inheritdoc + */ public function getName() { return 'Configuration'; } + /** + * Returns Yii logo ready to use in `