private function getFilter($compass = false)
 {
     $filter = new ScssphpFilter();
     if ($compass) {
         $filter->enableCompass();
     }
     return $filter;
 }
 public function register()
 {
     $config = $this->app->make('config');
     // Register Less filter & variable value extractor
     $this->app->bind('assets/filter/less', function ($app, $assets) use($config) {
         $lessf = new LessphpFilter(array('cache_dir' => $config->get('concrete.cache.directory'), 'compress' => !!$config->get('concrete.theme.compress_preprocessor_output'), 'sourceMap' => !$config->get('concrete.theme.compress_preprocessor_output') && !!$config->get('concrete.theme.generate_less_sourcemap')));
         if ($config->get('app.asset_filter_options.less.legacy_url_support', false)) {
             $lessf->setBasePath('/' . ltrim($app['app_relative_path'], '/'));
             $lessf->setRelativeUrlPaths(true);
         }
         $variableList = $assets->getStyleSheetVariables();
         if (is_array($variableList)) {
             $lessf->setLessVariables($variableList);
         }
         $fp = new LessFunctionProvider();
         $fp->registerFor($lessf);
         return $lessf;
     });
     $this->app->bind('assets/value/extractor/less', function ($app, $args) {
         list($file, $urlroot) = array_pad((array) $args, 2, false);
         return new LessStyleValueExtractor($file, $urlroot);
     });
     // Register SCSS filter & variable value extractor
     $this->app->bind('assets/filter/scss', function ($app, $assets) use($config) {
         // There does not seem to be a way to get the source maps to the
         // ScssPhp at the moment:
         // https://github.com/leafo/scssphp/issues/135
         $scssf = new ScssphpFilter();
         if ($config->get('concrete.theme.compress_preprocessor_output')) {
             $scssf->setFormatter('Leafo\\ScssPhp\\Formatter\\Compressed');
         } else {
             $scssf->setFormatter('Leafo\\ScssPhp\\Formatter\\Expanded');
         }
         $fp = new ScssFunctionProvider();
         $fp->registerFor($scssf);
         $variableList = $assets->getStyleSheetVariables();
         if (is_array($variableList)) {
             $scssf->setVariables($variableList);
         }
         return $scssf;
     });
     $this->app->bind('assets/value/extractor/scss', function ($app, $args) {
         list($file, $urlroot) = array_pad((array) $args, 2, false);
         return new ScssStyleValueExtractor($file, $urlroot);
     });
     // Register JShrink filter
     $this->app->bind('assets/filter/jshrink', function ($app, $assets) {
         $jsf = new JShrinkFilter();
         return $jsf;
     });
     // Register CssMin filter
     $this->app->bind('assets/filter/cssmin', function ($app, $assets) {
         $cmf = new CssMinFilter();
         return $cmf;
     });
 }
 /**
  * Register the Assetic provider
  */
 private function registerAssetic()
 {
     $this->register(new SilexAssetic\AsseticServiceProvider(), array('assetic.options' => array('debug' => $this['debug'], 'auto_dump_assets' => $this['debug'])));
     $this['assetic.path_to_source'] = $this['root_dir'] . '/integration/public';
     $this['assetic.path_to_web'] = $this['root_dir'] . '/web';
     $this['assetic.filter_manager'] = $this->share($this->extend('assetic.filter_manager', function ($fm, $app) {
         $f = new Assetic\Filter\ScssphpFilter();
         $f->enableCompass();
         $fm->set('scssphp', $f);
         $fm->set('cssrewrite', new Assetic\Filter\CssRewriteFilter());
         $fm->set('lessphp', new Assetic\Filter\LessphpFilter());
         $fm->set('coffeephp', new CoffeephpFilter());
         return $fm;
     }));
 }
Example #4
0
File: File.php Project: lalop/aphet
 /**
  * @param $path String target path
  * @return \Assetic\Filter\FileAsset
  */
 public function asset($opts = array())
 {
     $asset = new FileAsset($this->real_path);
     $ext = strtolower(substr($this->real_path, strrpos($this->real_path, '.')));
     if (in_array($ext, array('.css', '.scss'))) {
         //il faut rajouter la méthode asset-url
         $scss = new ScssphpFilter();
         if (isset($opts['compass']) && $opts['compass']) {
             $scss->enableCompass(true);
         }
         $scss->registerFunction('aphet_url', function ($args, $scss) {
             if ($args[0][0] === 'string') {
                 $url = is_array($args[0][2][0]) ? $args[0][2][0][2][0] : $args[0][2][0];
             } else {
                 throw new \Exception('je ne sais pas quoi faire là');
             }
             if (strpos($url, '?') !== false) {
                 list($url, $query) = explode('?', $url);
             } else {
                 $query = null;
             }
             if (strpos($url, '#') !== false) {
                 list($url, $hash) = explode('#', $url);
             } else {
                 $hash = null;
             }
             return 'url(' . aphet_url($url) . ($query ? "?{$query}" : '') . ($hash ? "?{$hash}" : '') . ')';
         });
         $asset->ensureFilter($scss);
     } elseif ($ext === '.js') {
         $filter = new \Assetic\Filter\CallablesFilter(function ($asset) {
             $asset->setContent(preg_replace_callback('/aphet_url\\((.*)\\)/', function ($match) {
                 return '\'' . aphet_url(json_decode($match[1])) . '\'';
             }, $asset->getContent()));
         });
         $asset->ensureFilter($filter);
     }
     return $asset;
 }
 /**
  * Bootstraps the application.
  *
  * This method is called after all services are registered
  * and should be used for "dynamic" configuration (whenever
  * a service must be requested).
  *
  * @param  Application $app
  * @throws \InvalidArgumentException
  */
 public function boot(Application $app)
 {
     $app['assetic.filters'] = $app->share($app->extend('assetic.filters', function ($filters) use($app) {
         $filters['scssphp'] = $app->share(function () use($app) {
             $filter = new ScssphpFilter();
             $filter->enableCompass($app['assetic.filter.scssphp.compass']);
             $filter->setImportPaths($app['assetic.filter.scssphp.import_paths']);
             $filter->setVariables($app['assetic.filter.scssphp.variables']);
             $filter->setFormatter($app['assetic.filter.scssphp.formatter']);
             return $filter;
         });
         return $filters;
     }));
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     try {
         // Potion config
         if ($this->config === false) {
             throw new \Exception('Invalid potion configuration, please run "artisan vendor:publish" in your project root to public the potion config file.');
         }
         // Clean up paths
         $this->config['resource_path'] = rtrim($this->config['resource_path'], '/');
         $this->config['resource_path'] = rtrim($this->config['resource_path'], '\\');
         $this->config['assets_path'] = rtrim($this->config['assets_path'], '/');
         $this->config['assets_path'] = rtrim($this->config['assets_path'], '\\');
         // Make the assets path
         if (!$this->makePath($this->config['assets_path'])) {
             throw new \Exception("Unable to make assets_path from config: {$this->config['assets_path']}");
         }
         // Filters
         $filters = [];
         // -- optipng
         $filter = new OptiPngFilter($this->config['filters']['optipng']['path']);
         $filter->setLevel($this->config['filters']['optipng']['level']);
         $filters['optipng'] = $filter;
         // -- jpegoptim
         $filter = new JpegoptimFilter($this->config['filters']['jpegoptim']['path']);
         $filter->setStripAll($this->config['filters']['jpegoptim']['strip']);
         $filter->setMax($this->config['filters']['jpegoptim']['max']);
         $filters['jpegoptim'] = $filter;
         // -- Css import
         $filter = new CssImportFilter();
         $filters['css_import'] = $filter;
         // -- Css rewrite
         $filter = new CssRewriteFilter();
         $filters['css_rewrite'] = $filter;
         // -- Css min
         $filter = new CssMinFilter();
         $filters['css_min'] = $filter;
         // -- Css Yui
         $filter = new CssCompressorFilter($this->config['filters']['css_yui']['path_jar'], $this->config['filters']['css_yui']['path_java']);
         $filters['css_yui'] = $filter;
         // -- CSS LessPHP
         $filter = new LessphpFilter();
         $filter->setLoadPaths($this->config['filters']['css_lessphp']['path_imports']);
         $filter->setFormatter($this->config['filters']['css_lessphp']['format']);
         $filter->setPreserveComments($this->config['filters']['css_lessphp']['preserve_comments']);
         $filters['css_lessphp'] = $filter;
         // -- CSS ScssPHP
         $filter = new ScssphpFilter();
         $filter->setImportPaths($this->config['filters']['css_scssphp']['path_imports']);
         $filter->setFormatter($this->config['filters']['css_scssphp']['format']);
         $filters['css_scssphp'] = $filter;
         // -- JS Min
         $filter = new JSMinFilter();
         $filters['js_min'] = $filter;
         // -- Js Yui
         $filter = new JsCompressorFilter($this->config['filters']['js_yui']['path_jar'], $this->config['filters']['js_yui']['path_java']);
         $filter->setNomunge($this->config['filters']['js_yui']['no_munge']);
         $filter->setPreserveSemi($this->config['filters']['js_yui']['preserve_semi']);
         $filter->setDisableOptimizations($this->config['filters']['js_yui']['disable_opti']);
         $filters['js_yui'] = $filter;
         // Cache
         $cache = [];
         // Each potion
         foreach ($this->config['potions'] as $potion) {
             // -- Find assets
             $resource_filters = [];
             foreach ($potion['filters'] as $filter) {
                 $resource_filters[] = $filters[$filter];
             }
             // -- Asset content
             $asset_content = '';
             // -- Resources
             foreach ($potion['resources'] as $resource) {
                 // -- -- Make full path
                 $resource = ltrim($resource, '/');
                 $resource = ltrim($resource, '\\');
                 $asset_path = $this->config['resource_path'] . DIRECTORY_SEPARATOR . $resource;
                 // -- -- Echo
                 $this->info("Processing resource: {$asset_path}");
                 // -- -- Get path info
                 $pathinfo = pathinfo($asset_path);
                 // -- -- File assets
                 $file_assets = [];
                 // -- -- Glob?
                 if ($pathinfo['extension'] == '*' || $pathinfo['filename'] == '*') {
                     // -- -- -- Get all file assets
                     $glob = new GlobAsset($asset_path, $resource_filters);
                     foreach ($glob->all() as $file_asset) {
                         $file_assets[] = new FileAsset(rtrim($file_asset->getSourceRoot(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file_asset->getSourcePath(), $resource_filters);
                     }
                 } else {
                     $file_assets[] = new FileAsset($asset_path, $resource_filters);
                 }
                 // -- -- Each file asset
                 foreach ($file_assets as $file_asset) {
                     // -- -- -- File
                     $file_path = $this->config['assets_path'] . DIRECTORY_SEPARATOR . $file_asset->getSourcePath();
                     // -- -- -- Echo
                     $this->info("Processing resource file: {$file_path}");
                     // -- -- -- Make file, or combine
                     if ($potion['output'] !== false) {
                         $asset_content .= $file_asset->dump();
                     } else {
                         // -- -- -- -- Echo
                         $this->info("Writing asset file: {$file_path}");
                         // -- -- -- -- Write
                         if (file_put_contents($file_path, $file_asset->dump()) === false) {
                             $this->error("Error writing asset file: {$file_path}");
                         }
                         // -- -- -- -- Add to cache
                         $cache[$file_asset->getSourcePath()] = $this->versionFile($file_path);
                     }
                 }
             }
             // -- Combine to a single file
             if ($potion['output'] !== false) {
                 // -- -- Write to file
                 $file_path = $this->config['assets_path'] . DIRECTORY_SEPARATOR . $potion['output'];
                 // -- -- Echo
                 $this->info("Writing asset file: {$file_path}");
                 // -- -- Write
                 if (file_put_contents($file_path, $asset_content) === false) {
                     $this->error("Error writing asset file: {$file_path}");
                 }
                 // -- -- Add to cache
                 $cache[$potion['output']] = $this->versionFile($file_path);
             }
         }
         // Set cache
         Cache::forever('potion_assets', $cache);
     } catch (\Exception $e) {
         // Echo
         $this->error($e->getMessage());
     }
 }
Example #7
0
 public function filterLoad(AssetInterface $asset)
 {
     parent::setVariables($this->variables);
     parent::filterLoad($asset);
 }