Skip to content

Commit

Permalink
Czh/add error stack (#12)
Browse files Browse the repository at this point in the history
* add error Stack

* add ErrorStack
  • Loading branch information
zhihuacui authored Aug 9, 2019
1 parent 94bb2d7 commit 563d4f8
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 75 deletions.
18 changes: 18 additions & 0 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,29 @@ public function init()
parent::init();
}

/**
* Render View File Use ethercap\apiBase\components\responseTemplates\ITemplate
*
* @param $view
* @param array $params
* @return string
*/
public function renderApi($view, $params = [])
{
return $this->getView()->renderApi($view, $params, $this);
}

/**
* Render View File Use ResBuilder rtData only
* @param $view
* @param array $params
* @return string
*/
public function renderApiPartial($view, $params = [])
{
return $this->getView()->renderApiPartial($view, $params, $this);
}

/**
* Returns the view object that can be used to render views or view files.
* The [[render()]], [[renderPartial()]] and [[renderFile()]] methods will use
Expand Down
24 changes: 24 additions & 0 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,33 @@

class View extends BaseView
{
/**
* @param $view
* @param array $params
* @param null $context
* @return string
*/
public function renderApi($view, $params = [], $context = null)
{
$originalExtension = $this->defaultExtension;
$this->defaultExtension = 'api';
$viewFile = $this->findViewFile($view, $context);
$this->defaultExtension = $originalExtension;
return $this->renderFile($viewFile, $params, $context);
}

/**
* @param $view
* @param array $params
* @param null $context
* @return string
*/
public function renderApiPartial($view, $params = [], $context = null)
{
$originalExtension = $this->defaultExtension;
$this->defaultExtension = 'api';
$viewFile = $this->findViewFile($view, $context);
$this->defaultExtension = $originalExtension;
return $this->renderFile($viewFile, $params + ['_renderApiPartial' => true], $context);
}
}
6 changes: 6 additions & 0 deletions src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use yii\di\Instance;
use ethercap\apiBase\components\ResBuilder;
use yii\base\ViewRenderer as BaseViewRenderer;
use yii\helpers\ArrayHelper;

/**
* Api ViewRenderer
Expand All @@ -15,6 +16,9 @@
*/
class ViewRenderer extends BaseViewRenderer
{
/**
* @var View
*/
public $view;

public $resBuilder = ResBuilder::class;
Expand All @@ -40,6 +44,8 @@ public function render($view, $viewFile, $params)
{
$this->view = $view;
$res = clone $this->_resBuilder;
$res->initErrorStack();
$res->renderPartial = ArrayHelper::remove($params, '_renderApiPartial');
return $this->renderApiFile($viewFile, $params, $res);
}

Expand Down
53 changes: 37 additions & 16 deletions src/components/ResBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ethercap\apiBase\components;

use ethercap\apiBase\components\responseTemplates\BlockTpl;
use ethercap\apiBase\components\responseTemplates\FaasTpl;
use ethercap\apiBase\components\responseTemplates\NoBlockTpl;
use ethercap\apiBase\components\responseTemplates\TemplateManager;
use Yii;
Expand All @@ -16,9 +15,11 @@
* @package ethercap\apiBase\components
*
* @property TemplateManager $tm
* @property string $id
*/
class ResBuilder extends Component
{
public $renderPartial;
public $rtData;
public $errorHandler;
public $templates = [];
Expand All @@ -36,18 +37,30 @@ class ResBuilder extends Component
'noBlock' => [
'class' => NoBlockTpl::class,
],
'faas' => [
'class' => FaasTpl::class,
],
];

protected $_id;

public function getId()
{
if (!$this->_id) {
$this->_id = spl_object_hash($this);
}
return $this->_id;
}

public function init()
{
parent::init();
$this->tm = new TemplateManager();
$this->initTemplates();
}

public function initErrorStack()
{
ResBuilderErrorStack::getInstance($this->id);
}

protected function initTemplates()
{
$templates = array_merge($this->_builtInTemplates, $this->templates);
Expand All @@ -73,14 +86,21 @@ public function run()
{
Yii::$app->response->formatters[Response::FORMAT_JSON] = Formatter::class;
$this->processData();
if ($this->errorHandler && $this->_errorModels && is_callable($this->errorHandler)) {
return call_user_func($this->errorHandler, $this->getErrModel());
if ($this->errorHandler && is_callable($this->errorHandler)) {
if ($this->id == ResBuilderErrorStack::getInstance($this->id)->creator) {
$error = clone ResBuilderErrorStack::getInstance($this->id);
ResBuilderErrorStack::getInstance($this->id)->clear();
return call_user_func($this->errorHandler, $error, $this);
}
}
if ($this->renderPartial) {
return $this->rtData;
}
return $this->tm->loadBuilder($this)->getRes();
}

/**
* @param null $value
* @param null $value
* @param array $default
* @return Value|null
*/
Expand All @@ -94,8 +114,8 @@ public function data($value = null, $default = [])
}

/**
* @param null $key
* @param null $value
* @param null $key
* @param null $value
* @param array $default
* @return Value|null
*/
Expand All @@ -112,11 +132,7 @@ public function field($key = null, $value = null, $default = [])

public function getErrModel()
{
if ($this->_errModel !== null) {
return $this->_errModel;
}
$this->_errModel = reset($this->_errorModels);
return $this->_errModel;
return ResBuilderErrorStack::getInstance($this->id)->getErrorModel();
}

protected function processData()
Expand Down Expand Up @@ -153,11 +169,16 @@ protected function buildFields()

public function pushError($model)
{
array_push($this->_errorModels, $model);
return ResBuilderErrorStack::getInstance($this->id)->pushError($model);
}

public function hasError()
{
return (bool) count($this->_errorModels);
return ResBuilderErrorStack::getInstance($this->id)->hasError();
}

public function __clone()
{
$this->_id = null;
}
}
71 changes: 71 additions & 0 deletions src/components/ResBuilderErrorStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace ethercap\apiBase\components;

use yii\base\Component;
use yii\base\Model;

/**
* Class ResBuilderErrorStack
* @package ethercap\apiBase\components
*
* @property string $creator
* @property Model $errorModel
* @property Model[] $errors
*/
class ResBuilderErrorStack extends Component
{
private $_creator;

private $_stack = [];

private static $_stacks = [];

private static $_instance;

public static function getInstance(string $creator)
{
if (self::$_instance === null) {
self::$_instance = new self();
self::$_instance->_creator = $creator;
}
return self::$_instance;
}

public function getCreator()
{
return $this->_creator;
}

public function pushError($errorModels)
{
if (!is_array($errorModels)) {
$errorModels = [$errorModels];
}
return array_push($this->_stack, ...$errorModels);
}

public function getErrorModel()
{
return reset($this->_stack);
}

public function getErrors()
{
return $this->_stack;
}

public function hasError()
{
return (bool) count($this->_stack);
}

/**
* 释放当前builder的error信息
*/
public function clear()
{
self::$_stacks[$this->_creator] = $this->_stack;
self::$_instance = null;
}
}
7 changes: 5 additions & 2 deletions src/components/responseTemplates/BlockTpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ethercap\apiBase\components\responseTemplates;

use ethercap\apiBase\components\ResBuilderErrorStack;
use yii\base\BaseObject;
use ethercap\common\helpers\SysMsg;

Expand All @@ -24,8 +25,10 @@ class BlockTpl extends BaseObject implements ITemplate

public function getRes()
{
if ($this->builder->hasError()) {
return SysMsg::getErrData($this->builder->getErrModel());
if ($this->builder->hasError() && ResBuilderErrorStack::getInstance($this->builder->id)->creator == $this->builder->id) {
$errorModel = $this->builder->getErrModel();
ResBuilderErrorStack::getInstance($this->builder->id)->clear();
return SysMsg::getErrData($errorModel);
}

$ret = $this->template;
Expand Down
57 changes: 0 additions & 57 deletions src/components/responseTemplates/FaasTpl.php

This file was deleted.

0 comments on commit 563d4f8

Please sign in to comment.