Beispiel #1
0
 protected function createManifest($id, $html)
 {
     try {
         $filename = $this->getFileName($id);
         if (@file_get_contents($filename) == false) {
             $caches = [];
             $paths = [];
             $baseUrl = Yii::getAlias('@web') . '/';
             $basePath = Yii::getAlias('@webroot') . '/';
             // css
             $matches = [];
             $pattern = '/<link [^>]*href="?([^">]+)"?/';
             preg_match_all($pattern, $html, $matches);
             if (isset($matches[1])) {
                 foreach ($matches[1] as $href) {
                     $caches[$href] = true;
                     if (($path = $this->convertUrlToPath($href, $basePath, $baseUrl)) !== false) {
                         $path = dirname($path);
                         if (!isset($paths[$path]) && is_dir($path)) {
                             $paths[$path] = true;
                             foreach (FileHelper::findFiles($path) as $file) {
                                 $caches[$this->convertPathToUrl($file, $basePath, $baseUrl)] = true;
                             }
                         }
                     }
                 }
             }
             // js
             $matches = [];
             $pattern = '/<script [^>]*src="?([^">]+)"?/';
             preg_match_all($pattern, $html, $matches);
             if (isset($matches[1])) {
                 foreach ($matches[1] as $src) {
                     $caches[$src] = true;
                 }
             }
             // img
             $matches = [];
             $pattern = '/<img [^>]*src="?([^">]+)"?/';
             preg_match_all($pattern, $html, $matches);
             if (isset($matches[1])) {
                 foreach ($matches[1] as $src) {
                     if (strpos($src, 'data:') !== 0) {
                         $caches[$src] = true;
                     }
                 }
             }
             unset($caches[false]);
             $data = array_keys($caches);
             if ($this->rel) {
                 $l = strlen($baseUrl);
                 foreach ($data as $key => $url) {
                     if (strpos($url, $baseUrl) === 0) {
                         $data[$key] = substr($url, $l);
                     }
                 }
             }
             $view = new View();
             $manifest = $view->renderPhpFile(Yii::getAlias('@mdm/clienttools/manifest.php'), ['caches' => array_merge($data, $this->extraCaches)]);
             FileHelper::createDirectory(dirname($filename));
             file_put_contents($filename, $manifest);
         }
     } catch (\Exception $exc) {
         \Yii::error($exc->getMessage());
     }
 }