| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright  Copyright (c) Flipbox Digital Limited | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link       https://github.com/flipboxfactory/craft-ember/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace flipbox\craft\ember\filters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Craft; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use craft\helpers\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use flipbox\craft\ember\helpers\ViewHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use flipbox\craft\ember\views\ViewInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use yii\base\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use yii\base\ActionFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use yii\base\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use yii\web\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author Flipbox Factory <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @since 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @property Controller $sender | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | class RawFilter extends ActionFilter | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     use FormatTrait, | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         ActionTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @var array this property defines the transformers for each action. | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Each action that should only support one transformer. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * You can use `'*'` to stand for all actions. When an action is explicitly | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * specified, it takes precedence over the specification given by `'*'`. | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * For example, | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * ```php | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * [ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      *   'create' => $view, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      *   'update' => $view, | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      *   'delete' => $view, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      *   '*' => $view, | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * ] | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * ``` | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     public $actions = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @param Action $action | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @param mixed $result | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @return mixed|\Twig_Markup | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @throws Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function afterAction($action, $result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if ($this->formatMatch($action->id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return $this->renderTemplate($action, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return parent::afterAction($action, $result); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @param Action $action | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param $result | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @return \Twig\Markup | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @throws Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     protected function renderTemplate(Action $action, $result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (!$view = $this->findAction($action->id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return Template::raw( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->resolveView($view)->render($result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @param $view | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @return ViewInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @throws Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     protected function resolveView($view): ViewInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (ViewHelper::isView($view)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return $view; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if (ViewHelper::isViewClass($view)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             return new $view(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $view = Craft::createObject($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (!$view instanceof ViewInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             throw new Exception("Invalid view"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $view; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |