private function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $asset)
 {
     $compiler->repr($asset->getTargetPath());
     if (file_exists(PUB_DIR . DS . $asset->getTargetPath())) {
         return;
     }
     $writer = new \Assetic\AssetWriter(PUB_DIR . DS);
     $writer->writeAsset($asset);
 }
Ejemplo n.º 2
0
 /**
  * Creates a new asset.
  *
  * Prefixing a filter name with a question mark will cause it to be
  * omitted when the factory is in debug mode.
  *
  * Available options:
  *
  *  * output: An output string
  *  * name:   An asset name for interpolation in output patterns
  *  * debug:  Forces debug mode on or off for this asset
  *
  * @param array|string $inputs  An array of input strings
  * @param array|string $filters An array of filter names
  * @param array        $options An array of options
  *
  * @return AssetCollection An asset collection
  */
 public function createAsset($inputs = array(), $filters = array(), array $options = array())
 {
     if (!is_array($inputs)) {
         $inputs = array($inputs);
     }
     if (!is_array($filters)) {
         $filters = array($filters);
     }
     if (!isset($options['output'])) {
         $options['output'] = $this->defaultOutput;
     }
     if (!isset($options['name'])) {
         $options['name'] = $this->generateAssetName($inputs, $filters);
     }
     if (!isset($options['debug'])) {
         $options['debug'] = $this->debug;
     }
     $asset = $this->createAssetCollection();
     // inner assets
     foreach ($inputs as $input) {
         $asset->add($this->parseInput($input));
     }
     // filters
     foreach ($filters as $filter) {
         if ('?' != $filter[0]) {
             $asset->ensureFilter($this->getFilter($filter));
         } elseif (!$options['debug']) {
             $asset->ensureFilter($this->getFilter(substr($filter, 1)));
         }
     }
     // output --> target url
     $asset->setTargetPath(str_replace('*', $options['name'], $options['output']));
     foreach ($this->workers as $worker) {
         $worker->process($asset);
     }
     if (\Kohana::config('assetic.default.twig_auto_write') === true) {
         $writer = new \Assetic\AssetWriter(\Kohana::config('assetic.default.asset_save_dir'));
         $writer->writeAsset($asset);
     }
     return $asset;
 }
Ejemplo n.º 3
0
 public function initAssets()
 {
     $options = $this->getOptions();
     $defaultWoptions = array('combined' => true, 'leaves' => false);
     $woptions = array();
     $am = $this->getAssetManager();
     $fm = $this->getFilterManager();
     $fassets = array();
     if (isset($options['parser'])) {
         foreach ($options['parser'] as $parser) {
             $filters = array();
             foreach ($parser['files'] as $key => $file) {
                 $filters[$key] = array();
                 if (isset($file['filters'])) {
                     foreach ($file['filters'] as $f) {
                         if (substr($f, 0, 1) == '?') {
                             $fn = substr($f, 1);
                             $init = (bool) (!$parser['debug']);
                         } else {
                             $fn = $f;
                             $init = true;
                         }
                         if ($init) {
                             if ($fm->has($fn)) {
                                 $filters[$key][] = $fm->get($fn);
                             }
                         }
                     }
                 }
             }
             $diterator = new \RecursiveDirectoryIterator($parser['directory']);
             $riterator = new \RecursiveIteratorIterator($diterator, \RecursiveIteratorIterator::SELF_FIRST);
             foreach ($riterator as $file) {
                 if (isset($parser['blacklist'])) {
                     foreach ($parser['blacklist'] as $bl) {
                         if (preg_match($bl, $file->getPathName())) {
                             continue;
                         }
                     }
                 }
                 foreach ($parser['files'] as $key => $fopts) {
                     $ppinfo = pathinfo($fopts['output']);
                     if ($file->isFile() && preg_match($fopts['pattern'], $file->getFilename())) {
                         $pinfo = pathinfo($file);
                         $pinfo['dirname'] = str_ireplace($parser['directory'], $ppinfo['dirname'], $pinfo['dirname']);
                         $pinfo['extension'] = $ppinfo['extension'];
                         $fasset = new \Assetic\Asset\FileAsset($file->getPathName(), $filters[$key]);
                         $fasset->setTargetUrl($pinfo['dirname'] . '/' . $pinfo['filename'] . '.' . $pinfo['extension']);
                         $fassets[] = $fasset;
                     }
                 }
             }
         }
     }
     $f = $this->getAssetFactory();
     if (isset($options['collections'])) {
         foreach ($options['collections'] as $name => $coll) {
             $woptions[$name] = isset($coll['write']) ? $coll['write'] : array();
             $woptions[$name] = array_merge($defaultWoptions, $woptions[$name]);
             $coll['options']['name'] = str_ireplace("{APPLICATION_REVISION}", APPLICATION_REVISION, $coll['options']['name']);
             $asset = $f->createAsset($coll['inputs'], $coll['filters'], $coll['options']);
             if ($coll['cache']) {
                 $asset = new \Assetic\Asset\AssetCache($asset, $this->getAssetCache());
             }
             $am->set($name, $asset);
         }
     }
     if (isset($options['fileAssets'])) {
         foreach ($options['fileAssets'] as $name => $fileAsset) {
             $asset = new \Assetic\Asset\FileAsset($fileAsset['path']);
             $asset->setTargetUrl($fileAsset['targetUrl']);
             if (isset($fileAsset['filters'])) {
                 foreach ($fileAsset['filters'] as $filter) {
                     $f = $this->getFilterManager()->get($filter);
                     $asset->ensureFilter($f);
                 }
             }
             $am->set($name, $asset);
         }
     }
     $am = $this->getAssetManager();
     $writer = new \Assetic\AssetWriter(APPLICATION_PATH . '/../public');
     foreach ($fassets as $fasset) {
         if (file_exists(APPLICATION_PATH . '/../public' . '/' . $fasset->getTargetUrl())) {
             $amod = filemtime(APPLICATION_PATH . '/../public' . '/' . $fasset->getTargetUrl());
             if ($fasset->getLastModified() <= $amod) {
                 continue;
             }
         }
         $writer->writeAsset($fasset);
     }
     foreach ($am->getNames() as $name) {
         $asset = $am->get($name);
         if (file_exists(APPLICATION_PATH . '/../public' . '/' . $asset->getTargetUrl())) {
             $amod = filemtime(APPLICATION_PATH . '/../public' . '/' . $asset->getTargetUrl());
             if ($asset->getLastModified() <= $amod) {
                 continue;
             }
         }
         $writeOptions = $woptions[$name];
         if ($writeOptions['combined']) {
             $writer->writeAsset($asset);
         }
         if ($writeOptions['leaves']) {
             foreach ($asset as $leaf) {
                 $writer->writeAsset($leaf);
             }
         }
     }
 }