File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 4
4
5
5
use bedezign \yii2 \audit \AuditTrailBehavior ;
6
6
use dmstr \modules \prototype \traits \EditorEntry ;
7
+ use Twig \Error \Error as TwigError ;
8
+ use Yii ;
9
+ use yii \base \InvalidConfigException ;
7
10
8
11
/**
9
12
* This is the model class for table "app_twig".
@@ -30,4 +33,50 @@ public function behaviors()
30
33
$ behaviors ['audit-trail ' ] = AuditTrailBehavior::class;
31
34
return $ behaviors ;
32
35
}
36
+
37
+ /**
38
+ * @inheritdoc
39
+ */
40
+ public function rules ()
41
+ {
42
+ $ rules = parent ::rules ();
43
+ $ rules [] = [
44
+ 'value ' ,
45
+ 'string '
46
+ ];
47
+ $ rules [] = [
48
+ 'value ' ,
49
+ 'validateTwigTemplate '
50
+ ];
51
+ return $ rules ;
52
+ }
53
+
54
+ /**
55
+ * @param $attribute
56
+ *
57
+ * @throws \yii\base\InvalidConfigException
58
+ * @return void
59
+ */
60
+ public function validateTwigTemplate ($ attribute )
61
+ {
62
+ /** @var \yii\twig\ViewRenderer|null $twigRenderer */
63
+ $ twigRendererConfig = Yii::$ app ->view ->renderers ['twig ' ] ?? null ;
64
+
65
+
66
+ if (is_null ($ twigRendererConfig )) {
67
+ throw new InvalidConfigException ('TWIG renderer must be defined ' );
68
+ }
69
+
70
+ $ twigRenderer = Yii::createObject ($ twigRendererConfig );
71
+ $ environment = $ twigRenderer ->twig ;
72
+ try {
73
+ $ template = $ environment ->createTemplate ($ this ->$ attribute );
74
+ $ template ->render ();
75
+ } catch (TwigError $ e ) {
76
+ $ this ->addError ($ attribute , Yii::t ('prototype ' , 'Line {lineNumber}: {message} ' , [
77
+ 'lineNumber ' => $ e ->getTemplateLine (),
78
+ 'message ' => $ e ->getRawMessage ()
79
+ ]));
80
+ }
81
+ }
33
82
}
You can’t perform that action at this time.
0 commit comments