Esempio n. 1
0
 /**
  * Build the theme : copy every resource files in themes/{themename}
  *
  * @param boolean $force If set to true, the theme will be rebuilt without condition
  *
  * @return boole True if the theme has been built, false it it has been taken from cache
  */
 public function build($force = false)
 {
     if ($this->getDefinition('extends')) {
         if (Theme::get($this->getDefinition('extends'))) {
             Theme::get($this->getDefinition('extends'))->build($force);
         }
     }
     $build = false;
     if ($force) {
         $build = true;
     }
     if (!file_exists($this->getStaticDir())) {
         mkdir($this->getStaticDir(), 0755, true);
         $build = true;
     }
     if (!$build) {
         $dest = $this->getStaticCssFile();
         if (!is_file($dest)) {
             $build = true;
         } else {
             // Get all files in less/
             $files = App::fs()->find($this->getLessDirname(), '*.less');
             $lastUpdate = filemtime($dest);
             foreach ($files as $file) {
                 if (filemtime($file) > $lastUpdate) {
                     $build = true;
                     break;
                 }
             }
         }
     }
     if ($build) {
         // Build the theme => Copy each accessible files in static dir
         foreach (glob($this->getRootDir() . '*') as $elt) {
             if (!in_array(basename($elt), array('views', 'start.php'))) {
                 App::fs()->copy($elt, $this->getStaticDir());
             }
         }
         // In the main less file, replace the editable vars by their customized values
         $values = $this->getVariablesCustomValues();
         $precompiledLess = preg_replace_callback(self::EDITABLE_VARS_PATTERN, function ($m) use($values) {
             return '@' . $m[1] . ' : ' . (isset($values[$m[1]]) ? $values[$m[1]] : $m[2]) . ';';
         }, file_get_contents($this->getBaseLessFile()));
         file_put_contents($this->getStaticLessFile(), $precompiledLess);
         Less::compile($this->getStaticLessFile(), $this->getStaticCssFile());
     }
     return $build;
 }
Esempio n. 2
0
 /**
  * Return the URL of a public CSS file,
  * or the URL of the directory containing public CSS files if $basename is empty
  *
  * @param string $basename The Less file basename
  *
  * @return string The URL
  */
 public function getCssUrl($basename = "")
 {
     $cssUrl = $this->getStaticUrl() . 'css/';
     if (empty($basename)) {
         return $cssUrl;
     } else {
         $privateFilename = $this->getLessDir() . $basename;
         $cssBasename = preg_replace('/\\.less$/', '.css', $basename);
         $publicFilename = $this->getPublicCssDir() . $cssBasename;
         if (is_file($privateFilename)) {
             Event::on('built-less', function (Event $event) use($privateFilename) {
                 if ($event->getData('source') === $privateFilename) {
                     // Copy all static files except less and JS
                     foreach (glob($this->getStaticDir() . '*') as $elt) {
                         if (!in_array(basename($elt), array('less', 'js'))) {
                             App::fs()->copy($elt, $this->getPublicStaticDir());
                         }
                     }
                 }
             });
             Less::compile($privateFilename, $publicFilename);
         }
         return $cssUrl . $cssBasename . '?' . filemtime($publicFilename);
     }
 }