This commit is contained in:
Alex Solomaha
2016-11-19 22:50:12 +02:00
parent 08837c2fa6
commit 4ea249fe68
5 changed files with 169 additions and 79 deletions

113
components/ErrorAction.php Normal file
View File

@@ -0,0 +1,113 @@
<?php
namespace app\components;
use Yii;
use yii\base\Action;
use yii\base\Exception;
use yii\base\UserException;
use yii\web\HttpException;
/**
* ErrorAction displays application errors using a specified view.
*
* To use ErrorAction, you need to do the following steps:
*
* First, declare an action of ErrorAction type in the `actions()` method of your `SiteController`
* class (or whatever controller you prefer), like the following:
*
* ```php
* public function actions()
* {
* return [
* 'error' => ['class' => 'yii\web\ErrorAction'],
* ];
* }
* ```
*
* Then, create a view file for this action. If the route of your error action is `site/error`, then
* the view file should be `views/site/error.php`. In this view file, the following variables are available:
*
* - `$name`: the error name
* - `$message`: the error message
* - `$exception`: the exception being handled
*
* Finally, configure the "errorHandler" application component as follows,
*
* ```php
* 'errorHandler' => [
* 'errorAction' => 'site/error',
* ]
* ```
*/
class ErrorAction extends Action
{
/**
* @var string the view file to be rendered. If not set, it will take the value of [[id]].
* That means, if you name the action as "error" in "SiteController", then the view name
* would be "error", and the corresponding view file would be "views/site/error.php".
*/
public $view;
/**
* @var string the view layout
*/
public $layout = 'main';
/**
* @var string the name of the error when the exception name cannot be determined.
* Defaults to "Error".
*/
public $defaultName;
/**
* @var string the message to be displayed when the exception message contains sensitive information.
* Defaults to "An internal server error occurred.".
*/
public $defaultMessage;
/**
* Runs the action
*
* @return string result content
*/
public function run()
{
$this->controller->layout = $this->layout;
if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
// action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
$exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
}
if ($exception instanceof HttpException) {
$code = $exception->statusCode;
} else {
$code = $exception->getCode();
}
if ($exception instanceof Exception) {
$name = $exception->getName();
} else {
$name = $this->defaultName ?: Yii::t('yii', 'Error');
}
if ($code) {
$name .= " (#$code)";
}
if ($exception instanceof UserException) {
$message = $exception->getMessage();
} else {
$message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
}
if (Yii::$app->getRequest()->getIsAjax()) {
return "$name: $message";
} else {
return $this->controller->render($this->view ?: $this->id, [
'name' => $name,
'message' => $message,
'exception' => $exception,
]);
}
}
}

View File

@@ -5,6 +5,7 @@ namespace app\controllers;
use Yii;
use app\models\History;
use app\models\HistorySearch;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
@@ -26,6 +27,15 @@ class HistoryController extends Controller
'delete' => ['POST'],
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
@@ -44,55 +54,6 @@ class HistoryController extends Controller
]);
}
/**
* Displays a single History model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new History model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new History();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing History model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing History model.
* If deletion is successful, the browser will be redirected to the 'index' page.

View File

@@ -3,10 +3,7 @@
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
class SiteController extends Controller
{
@@ -14,7 +11,8 @@ class SiteController extends Controller
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
'class' => 'app\components\ErrorAction',
'layout' => Yii::$app->user->isGuest ? 'base' : 'main',
],
];
}

View File

@@ -9,6 +9,7 @@ use app\models\User;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\TimerInterface;
use Yii;
use yii\base\NotSupportedException;
use yii\helpers\Json;
@@ -46,7 +47,7 @@ class Panel implements MessageComponentInterface
protected $items;
/**
* @var array
* @var TimerInterface[]
*/
protected $isConnectedTimers;
@@ -183,30 +184,10 @@ class Panel implements MessageComponentInterface
$conn->Board = $board;
$this->board_clients[$board->id] = $conn;
$this->isConnectedTimers[$board->id] = $this->loop->addPeriodicTimer(
$this->isConnectedTimers[$board->id] = $this->loop->addTimer(
Yii::$app->params['server']['connectionCheckTimeout'],
function () use ($board) {
$this->log("Checking for timeout [$board->id] board...");
if (!isset($this->board_clients[$board->id])) {
$this->logBoardConnection($board, false);
return $this->log("Board [$board->id] has already been disconnected!");
}
if (isset($this->awaitingPong[$board->id])) {
$this->log("There was no pong from last ping! Disconnecting...");
return $this->board_clients[$board->id]->close();
}
$this->log("Sending ping command...");
$this->awaitingPong[$board->id] = true;
return $this->sendToBoard($board->id, [
'type' => 'ping',
]);
return $this->doConnectionCheckTimer($board);
}
);
@@ -444,6 +425,9 @@ class Panel implements MessageComponentInterface
case 'pong':
$this->log("Pong from board [$board->id]");
break;
default:
$this->log("Unknown command: \"{$data['type']}\"");
break;
}
@@ -557,7 +541,7 @@ class Panel implements MessageComponentInterface
$this->logSwitch($item, $user, 0);
return false;
return true;
}
/**
@@ -654,7 +638,7 @@ class Panel implements MessageComponentInterface
var_dump($history->errors);
}
return false;
return true;
}
/**
@@ -807,4 +791,38 @@ class Panel implements MessageComponentInterface
var_dump($model->errors);
}
}
public function doConnectionCheckTimer($board)
{
$this->log("Checking for timeout [$board->id] board...");
if (!isset($this->board_clients[$board->id])) {
$this->logBoardConnection($board, false);
return $this->log("Board [$board->id] has already been disconnected!");
}
if (isset($this->awaitingPong[$board->id])) {
$this->log("There was no pong from last ping! Disconnecting...");
return $this->board_clients[$board->id]->close();
}
$this->awaitingPong[$board->id] = true;
$this->sendToBoard($board->id, [
'type' => 'ping',
]);
$this->isConnectedTimers[$board->id]->cancel();
$this->isConnectedTimers[$board->id] = $this->loop->addTimer(
Yii::$app->params['server']['connectionCheckTimeout'],
function () use ($board) {
return $this->doConnectionCheckTimer($board);
}
);
return true;
}
}

View File

@@ -1,2 +1,2 @@
User-agent: *
Disallow:
Disallow: /