Example #1
0
 public function __construct()
 {
     $this->template = \Template::instance();
     $f3 = $this->f3 = \Base::instance();
     $opt_defaults = array('auto_include' => true, 'greedy' => false, 'filter' => array(), 'public_path' => '', 'combine' => array('public_path' => '', 'exclude' => '', 'slots' => array(10 => 'top', 20 => 'external', 40 => 'internal', 60 => 'excluded', 80 => 'inline')), 'minify' => array('public_path' => '', 'exclude' => '.*(.min.).*', 'inline' => false), 'handle_inline' => false, 'timestamps' => false, 'onFileNotFound' => null, 'prepend_base' => false);
     // merge options with defaults
     $f3->set('ASSETS', $f3->exists('ASSETS', $opt) ? array_replace_recursive($opt_defaults, $opt) : $opt_defaults);
     // propagate default public temp dir
     if (!$f3->devoid('ASSETS.public_path')) {
         if ($f3->devoid('ASSETS.combine.public_path')) {
             $f3->copy('ASSETS.public_path', 'ASSETS.combine.public_path');
         }
         if ($f3->devoid('ASSETS.minify.public_path')) {
             $f3->copy('ASSETS.public_path', 'ASSETS.minify.public_path');
         }
     }
     $self = $this;
     $this->formatter = array('js' => function ($asset) use($f3, $self) {
         if ($asset['origin'] == 'inline') {
             return sprintf('<script>%s</script>', $asset['data']);
         } else {
             $asset['charset'] = $f3->get('ENCODING');
         }
         $path = $asset['path'];
         unset($asset['path'], $asset['origin'], $asset['type'], $asset['exclude'], $asset['slot']);
         $params = $self->resolveAttr($asset + array('src' => $path));
         return sprintf('<script%s></script>', $params);
     }, 'css' => function ($asset) use($f3, $self) {
         if ($asset['origin'] == 'inline') {
             return sprintf('<style type="text/css">%s</style>', $asset['data']);
         }
         $path = $asset['path'];
         unset($asset['path'], $asset['origin'], $asset['type'], $asset['exclude'], $asset['slot']);
         $params = $self->resolveAttr($asset + array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => $path));
         return sprintf('<link%s/>', $params);
     });
     $this->filter = array('combine' => array($this, 'combine'), 'minify' => array($this, 'minify'));
     $this->reset();
     if ($f3->get('ASSETS.auto_include')) {
         $this->template->extend('head', 'Assets::renderHeadTag');
         $this->template->extend('body', 'Assets::renderBodyTag');
     }
     $this->template->extend('asset', 'Assets::renderAssetTag');
     if ($f3->get('ASSETS.greedy')) {
         $this->template->extend('script', 'Assets::renderScriptTag');
         $this->template->extend('link', 'Assets::renderLinkCSSTag');
         $this->template->extend('style', 'Assets::renderStyleTag');
     }
     $this->template->afterrender(function ($data) use($f3, $self) {
         foreach ($self->getGroups() as $group) {
             if (preg_match('<!--\\s*assets-' . $group . '+\\s*-->', $data)) {
                 $data = preg_replace('/(\\s*<!--\\s*assets-' . $group . '+\\s*-->\\s*)/i', $self->renderGroup($self->getAssets($group)), $data, 1);
             }
         }
         return $data;
     });
 }