コード例 #1
0
ファイル: Scss.php プロジェクト: nsams/koala-framework
 public function warmupCaches()
 {
     $cacheFile = $this->_getCacheFileName();
     $useCache = false;
     if (file_exists("{$cacheFile}.sourcetimes")) {
         $useCache = true;
         $sourceTimes = unserialize(file_get_contents("{$cacheFile}.sourcetimes"));
         foreach ($sourceTimes as $i) {
             if ($i['mtime']) {
                 if (!file_exists($i['file']) || filemtime($i['file']) != $i['mtime']) {
                     //file was modified or deleted
                     $useCache = false;
                     break;
                 }
             } else {
                 if (file_exists($i['file'])) {
                     //file didn't exist, was created
                     $useCache = false;
                     break;
                 }
             }
         }
     }
     if (!$useCache) {
         $fileName = $this->getAbsoluteFileName();
         static $loadPath;
         if (!isset($loadPath)) {
             $loadPath = array();
             foreach (glob(VENDOR_PATH . '/bower_components/*') as $p) {
                 $bowerMain = null;
                 $mainExt = null;
                 if (file_exists($p . '/bower.json')) {
                     $bower = json_decode(file_get_contents($p . '/bower.json'));
                     if (isset($bower->main) && is_string($bower->main)) {
                         $bowerMain = $bower->main;
                         $mainExt = substr($bowerMain, -5);
                     }
                 }
                 if ($mainExt == '.scss' || $mainExt == '.sass') {
                     $mainDir = substr($bowerMain, 0, strrpos($bowerMain, '/'));
                     $loadPath[] = $p . '/' . $mainDir;
                 } else {
                     if (file_exists($p . '/scss')) {
                         $loadPath[] = $p . '/scss';
                     }
                 }
             }
             $loadPath[] = './scss';
             if (KWF_PATH == '..') {
                 $loadPath[] = substr(getcwd(), 0, strrpos(getcwd(), '/')) . '/sass/Kwf/stylesheets';
             } else {
                 $loadPath[] = KWF_PATH . '/sass/Kwf/stylesheets';
             }
             $loadPath = escapeshellarg(implode(PATH_SEPARATOR, $loadPath));
         }
         if (substr($fileName, 0, 2) == './') {
             $fileName = getcwd() . substr($fileName, 1);
         }
         $bin = Kwf_Config::getValue('server.nodeSassBinary');
         if (!$bin) {
             $bin = getcwd() . "/" . VENDOR_PATH . "/bin/node " . dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/node_modules/node-sass/bin/node-sass';
         }
         $cmd = "{$bin} --include-path {$loadPath} --output-style compressed ";
         $cmd .= " --source-map " . escapeshellarg($cacheFile . '.map');
         $cmd .= " " . escapeshellarg($fileName) . " " . escapeshellarg($cacheFile);
         $cmd .= " 2>&1";
         $out = array();
         exec($cmd, $out, $retVal);
         if ($retVal) {
             throw new Kwf_Exception("compiling sass failed: " . implode("\n", $out));
         }
         $map = json_decode(file_get_contents("{$cacheFile}.map"));
         $sourceFiles = array();
         foreach ($map->sources as $k => $i) {
             //sources are relative to cache/sass, strip that
             if (substr($i, 0, 6) != '../../') {
                 throw new Kwf_Exception('source doesn\'t start with ../../');
             }
             $i = substr($i, 6);
             $f = self::getPathWithTypeByFileName(getcwd() . '/' . $i);
             if (!$f) {
                 throw new Kwf_Exception("Can't find path for '" . getcwd() . '/' . $i . "'");
             }
             $map->sources[$k] = $f;
             $sourceFiles[] = $f;
             if (substr($f, 0, 16) == 'web/scss/config/') {
                 $sourceFiles[] = 'kwf/sass/Kwf/stylesheets/config/' . substr($f, 16);
             } else {
                 if (substr($f, 0, 32) == 'kwf/sass/Kwf/stylesheets/config/') {
                     $sourceFiles[] = 'web/scss/config/' . substr($f, 32);
                 }
             }
         }
         $map->file = $cacheFile;
         file_put_contents("{$cacheFile}.map", json_encode($map));
         $ret = file_get_contents($cacheFile);
         $ret = str_replace("@charset \"UTF-8\";\n", '', $ret);
         //remove charset, no need to adjust sourcemap as sourcemap doesn't include that (bug in libsass)
         $ret = preg_replace("#/\\*\\# sourceMappingURL=.* \\*/#", '', $ret);
         $map = new Kwf_SourceMaps_SourceMap(file_get_contents("{$cacheFile}.map"), $ret);
         if (strpos($ret, 'cssClass') !== false && (strpos($ret, '$cssClass') !== false || strpos($ret, '.cssClass') !== false)) {
             $cssClass = $this->_getComponentCssClass();
             if ($cssClass) {
                 if (strpos($ret, '.cssClass') !== false) {
                     $map->stringReplace('.cssClass', ".{$cssClass}");
                 }
                 if (strpos($ret, '$cssClass') !== false) {
                     $map->stringReplace('$cssClass', ".{$cssClass}");
                 }
             }
         }
         if (strpos($ret, 'kwfup-') !== false) {
             if (Kwf_Config::getValue('application.uniquePrefix')) {
                 $map->stringReplace('kwfup-', Kwf_Config::getValue('application.uniquePrefix') . '-');
             } else {
                 $map->stringReplace('kwfup-', '');
             }
         }
         $usesVars = false;
         $assetVars = self::getAssetVariables();
         foreach ($assetVars as $k => $i) {
             $search = 'var(' . $k . ')';
             if (strpos($ret, $search) !== false) {
                 $map->stringReplace($search, $i);
                 $usesVars = true;
             }
         }
         $map->save("{$cacheFile}.map", $cacheFile);
         unset($map);
         $sourceTimes = array();
         foreach ($sourceFiles as $f) {
             $f = new Kwf_Assets_Dependency_File($f);
             $f = $f->getAbsoluteFileName();
             $sourceTimes[] = array('file' => $f, 'mtime' => file_exists($f) ? filemtime($f) : null);
         }
         if ($usesVars) {
             $files = array('assetVariables.ini', 'config.ini', KWF_PATH . '/config.ini');
             if (Kwf_Config::getValue('kwc.theme')) {
                 $files[] = Kwf_Config_Web::findThemeConfigIni(Kwf_Config::getValue('kwc.theme'));
             }
             foreach ($files as $f) {
                 $sourceTimes[] = array('file' => $f, 'mtime' => file_exists($f) ? filemtime($f) : null);
             }
         }
         file_put_contents("{$cacheFile}.sourcetimes", serialize($sourceTimes));
     }
 }