| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace hiqdev\yii2\modules\pages\models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use yii\base\InvalidConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | abstract class AbstractPage extends \yii\base\Object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public $layout; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public $title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function setData(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->data = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         foreach (['title', 'layout'] as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             if (isset($data[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 $this->{$key} = $data[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function getData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return $this->data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct($path, $text, array $data = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->path = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->text = $text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->setData($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function getPath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return $this->path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function getDate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return $this->data['date']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     static public function getModule() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         /// XXX think | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return Yii::$app->getModule('pages'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     static public function createFromFile($path) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $extension = pathinfo($path)['extension']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (!isset(static::getModule()->handlers[$extension])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             throw new InvalidConfigException('not handled extension:' . $extension); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $class = static::getModule()->handlers[$extension]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         list($data, $text) = static::extractData($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return new $class($path, $text, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     static public function extractData($path) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $lines = static::getModule()->readArray($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $marker = "---"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $line = array_shift($lines); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if ($line === $marker) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $meta = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             while (true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $line = array_shift($lines); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 if ($line === $marker) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $meta .= "\n" . $line; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $line = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $data = Yaml::parse($meta); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return [$data, $line . join("\n", $lines)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Renders the page with given params. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param array $params  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @abstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |     abstract public function render(array $params = []); | 
            
                                                        
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |