예제 #1
0
파일: DynCSS.php 프로젝트: rukzuk/rukzuk
 /**
  * Generate 'dynamic' CSS (css which depends on formValues)
  * @param \Render\APIs\RootAPIv1\RootCssAPI|\Render\APIs\RootAPIv1\RootRenderAPI $rootCssApi
  * @param \Render\Unit $rootUnit
  * @param boolean $outputTreeData - weather data required for client side recompile is written or not
  */
 public function generateCSS($rootCssApi, $rootUnit, $outputTreeData)
 {
     $unitData = $rootCssApi->getAllUnitData($rootUnit);
     $globalDataHash = md5(json_encode($rootCssApi->getColorScheme()) . json_encode($rootCssApi->getResolutions()));
     // collect css.js code
     $dynCSSPlugins = array();
     $formValues = array();
     foreach ($unitData as $uid => $data) {
         if (isset($data['dyncss']['formValues'])) {
             $formValues[$uid] = $data['dyncss']['formValues'];
         }
         if ($data['dyncss']['plugin']) {
             $pluginName = $data['dyncss']['plugin']['name'];
             $dynCSSPlugins[$pluginName] = $data['dyncss']['plugin'];
         }
     }
     // create css for each non-extension unit
     foreach ($unitData as $unitId => $singleUnitData) {
         if ($singleUnitData['dyncss']['isExtension']) {
             continue;
         }
         // unit data subtree
         $unitDataTree = $this->buildUnitTree($rootCssApi, $unitData, $unitId);
         // convert to json tree (used by dyncss)
         $cssJsonTree = array();
         $this->buildJsonTree($cssJsonTree, $unitDataTree);
         $cssCode = '';
         // try to get cached value
         $cacheKey = md5(json_encode($unitDataTree) . $globalDataHash . self::CACHE_VERSION);
         $cachedValue = $rootCssApi->getUnitCache($rootCssApi->getUnitById($unitId), 'css');
         if (isset($cachedValue['hash']) && $cachedValue['hash'] === $cacheKey) {
             $cssCode = $cachedValue['cssCode'];
         } else {
             // regenerate css
             try {
                 $cssCode = $this->cssEngine->compile($dynCSSPlugins, $cssJsonTree, $formValues, $rootCssApi);
                 $rootCssApi->setUnitCache($rootCssApi->getUnitById($unitId), 'css', array('hash' => $cacheKey, 'cssCode' => $cssCode));
             } catch (\Exception $e) {
                 $this->addError($e->getMessage());
             }
         }
         if ($outputTreeData) {
             echo '<style data-tree="' . htmlentities(json_encode($cssJsonTree)) . '" class="generatedStyles" id="generatedStyle_' . $unitId . '" data-unit-id="' . $unitId . '">';
             echo $cssCode;
             echo '</style>';
         } else {
             echo $cssCode;
         }
     }
     // load dyncss js module definition for client
     if ($outputTreeData) {
         $pluginUrls = array();
         foreach ($dynCSSPlugins as $data) {
             $pluginUrls[] = $data['url'];
         }
         echo '<script type="application/json" id="dyncss-plugins">' . json_encode($pluginUrls) . '</script>';
     }
 }