| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Yii2 Pages Module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @link      https://github.com/hiqdev/yii2-module-pages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package   yii2-module-pages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license   BSD-3-Clause | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace hiqdev\yii2\modules\pages\models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | abstract class AbstractPage extends \yii\base\Object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public $layout; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public $title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function setData($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if (!is_array($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->data = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         foreach (['title', 'layout'] as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             if (isset($data[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 $this->{$key} = $data[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function getData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return $this->data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function __construct($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         list($data, $text) = $this->extractData($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->path = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->text = $text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->setData($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function getPath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return $this->path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function getDate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $this->data['date']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     public static function getModule() | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         /// XXX think | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         return Yii::$app->getModule('pages'); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public static function createFromFile($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $extension = pathinfo($path)['extension']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $class = static::getModule()->findPageClass($extension); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return new $class($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 79 |  | View Code Duplication |     public function extractData($path) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $lines = static::getModule()->readArray($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $yaml = $this->readQuotedLines($lines, '/^---$/', '/^---$/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (empty($yaml)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $text = $lines; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $data = $this->readYaml($yaml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $text = array_slice($lines, count($yaml)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return [$data, implode("\n", $text)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function readFrontMatter($lines) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $yaml = $this->readQuotedLines($lines, '/^---$/', '/^---$/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (empty($yaml)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return empty($yaml) ? [] : $this->readYaml($yaml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function readYaml($lines) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $data = Yaml::parse(implode("\n", $lines)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (is_int($data['date'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $data['date'] = date('c', $data['date']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function readQuotedLines($lines, $headMarker, $tailMarker) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $line = array_shift($lines); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         if (!preg_match($headMarker, $line, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $res[] = ltrim(substr($line, strlen($matches[0]))); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         while ($line) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $line = array_shift($lines); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             if (preg_match($tailMarker, $line, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $res[] = rtrim(substr($line, 0, -strlen($matches[0]))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $res[] = $line; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         return $res; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * Renders the page with given params. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @abstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     abstract public function render(array $params = []); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.