-
Notifications
You must be signed in to change notification settings - Fork 611
/
ExitNode.php
82 lines (77 loc) · 2.66 KB
/
ExitNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\web\twig\nodes;
use Craft;
use craft\web\ServiceUnavailableHttpException;
use Twig\Compiler;
use Twig\Node\Node;
use yii\web\BadRequestHttpException;
use yii\web\ConflictHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\GoneHttpException;
use yii\web\HttpException;
use yii\web\MethodNotAllowedHttpException;
use yii\web\NotAcceptableHttpException;
use yii\web\NotFoundHttpException;
use yii\web\RangeNotSatisfiableHttpException;
use yii\web\ServerErrorHttpException;
use yii\web\TooManyRequestsHttpException;
use yii\web\UnauthorizedHttpException;
use yii\web\UnprocessableEntityHttpException;
use yii\web\UnsupportedMediaTypeHttpException;
/**
* Class ExitNode
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class ExitNode extends Node
{
/**
* @inheritdoc
*/
public function compile(Compiler $compiler): void
{
$compiler->addDebugInfo($this);
if ($this->hasNode('status')) {
$status = $this->getNode('status')->getAttribute('value');
$class = match ($status) {
400 => BadRequestHttpException::class,
401 => UnauthorizedHttpException::class,
403 => ForbiddenHttpException::class,
404 => NotFoundHttpException::class,
405 => MethodNotAllowedHttpException::class,
406 => NotAcceptableHttpException::class,
409 => ConflictHttpException::class,
410 => GoneHttpException::class,
415 => UnsupportedMediaTypeHttpException::class,
416 => RangeNotSatisfiableHttpException::class,
422 => UnprocessableEntityHttpException::class,
429 => TooManyRequestsHttpException::class,
500 => ServerErrorHttpException::class,
503 => ServiceUnavailableHttpException::class,
default => HttpException::class,
};
if ($class === HttpException::class) {
$compiler
->write("throw new $class($status");
if ($this->hasNode('message')) {
$compiler->raw(', ');
}
} else {
$compiler
->write("throw new $class(");
}
if ($this->hasNode('message')) {
$compiler->subcompile($this->getNode('message'));
}
$compiler->raw(");\n");
} else {
$compiler->write(Craft::class . "::\$app->end();\n");
}
}
}