Exemplo n.º 1
0
 public static function renderCodeBlocks($inline = TRUE, $options = array())
 {
     // if options is not an array assume it is the minify option
     if (!is_array($options)) {
         $options = array('minify' => (bool) $options);
     }
     // ensure our defualt options are populated
     $options += array('minify' => FALSE);
     extract($options);
     // build a var with the css code blocks
     $styleblocks = self::$styleBlocks;
     self::$styleBlocks = array();
     $cache = Cache::instance();
     // loop all the css includes and determine what needs to go on this page
     // and in what order
     $blockOutputBuffer = array();
     foreach ($styleblocks as $cond => $condblock) {
         // sort this condblock of codeblocks by weight
         ksort($condblock);
         $blockOutputBuffer[$cond] = array();
         foreach ($condblock as $codeblocks) {
             // if we are storing these codeblocks in cache then get them now
             if (self::$cache) {
                 // since they are in cache we have to do each one
                 foreach ($codeblocks as $cacheName) {
                     // check if we are minify'n these style blocks
                     if ($minify) {
                         // see if we have already minified this
                         $cacheMinName = 'css_min_' . substr($cacheName, 4);
                         $cached = $cache->get($cacheMinName);
                         // if this style is not already cached minified do so
                         if (is_null($cached)) {
                             // try to get the un-minified style
                             $cached = $cache->get($cacheName);
                             if (is_null($cached)) {
                                 kohana::log('error', 'Unable to minify cached codeblock  ' . $style);
                                 continue;
                             }
                             // minify and cache this style
                             $cached = self::minify($cached);
                             $cache->set($cacheMinName, $cached, array(), Kohana::config('cache.default.lifetime'));
                         }
                         // add the minified style to the output buffer
                         $blockOutputBuffer[$cond][] = $cached;
                     } else {
                         // see if we can get this style out of cache
                         $cached = $cache->get($cacheName);
                         if (is_null($cached)) {
                             kohana::log('error', 'Unable to locate cached codeblock ' . $style);
                             continue;
                         }
                         // add the style to the output buffer
                         $blockOutputBuffer[$cond][] = $cached;
                     }
                 }
             } else {
                 // if we are not storing the style in cache then the array
                 // contains the code blocks directly
                 $blockOutputBuffer[$cond] = array_merge($blockOutputBuffer[$cond], $codeblocks);
             }
         }
     }
     // run through everything that needs to be on the page and
     // make in conditional where apropriate then convert them into css links
     $codeblock = '';
     foreach ($blockOutputBuffer as $cond => $codeblocks) {
         // if all the includes are already on page move on
         if (empty($codeblocks)) {
             continue;
         }
         // combine these blocks into a string
         $block = sprintf(self::$csstags['codeblock'], implode("\n", $codeblocks));
         // if these are conditional css blocks then wrap them
         // in the conditional statement
         if (array_key_exists($cond, self::$cssconditional)) {
             $block = sprintf(self::$cssconditional[$cond], $block);
         }
         // add these blocks to our string
         $codeblock .= $block;
     }
     // are we displaying this inline or returning the string
     if ($inline) {
         echo $codeblock;
     } else {
         return $codeblock;
     }
 }