Beispiel #1
0
 protected function getAsseticFactory()
 {
     if (empty($this->config["input_path"][0])) {
         throw new \Exception('Empty input path');
     }
     $factory = new AssetFactory($this->config["input_path"][0], $this->config["debug"]);
     $factory->setDefaultOutput("");
     // add a FilterManager to the AssetFactory
     $fm = new FilterManager();
     $factory->setFilterManager($fm);
     // adding some filters to the filter manager
     if ($this->config["less_filter"]) {
         $lessphpFilter = new LessphpFilter();
         if ($this->lessPresets) {
             $lessphpFilter->setPresets($this->lessPresets);
         }
         $fm->set("less", $lessphpFilter);
     }
     if ($this->config["scss_filter"]) {
         $scssFilter = new ScssphpFilter();
         $fm->set("scss", $scssFilter);
     }
     $fm->set("min", new CssMinFilter());
     return $factory;
 }
 public function getChildren(AssetFactory $factory, $content, $loadPath = null)
 {
     $hash = md5($content);
     if ($this->cache->has($hash)) {
         return $factory->createAsset($this->cache->get($hash), array(), array('root' => $loadPath));
     } else {
         return parent::getChildren($factory, $content, $loadPath);
     }
 }
 /**
  * @param \Assetic\Asset\AssetInterface $asset
  */
 public function filterLoad(AssetInterface $asset)
 {
     $root = $asset->getSourceRoot();
     $path = $asset->getSourcePath();
     $filename = realpath($root . '/' . $path);
     if (file_exists($filename)) {
         touch($filename);
     }
     parent::filterLoad($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['lessphp'] = $app->share(function () use($app) {
             $filter = new LessphpFilter();
             $filter->setPresets($app['assetic.filter.lessphp.presets']);
             $filter->setLoadPaths($app['assetic.filter.lessphp.paths']);
             $filter->setFormatter($app['assetic.filter.lessphp.formatter']);
             $filter->setPreserveComments($app['assetic.filter.lessphp.preserve_comments']);
             return $filter;
         });
         return $filters;
     }));
 }
 print "CSS ...\n";
 $cssFiles = array();
 foreach (array('index', 'site', 'singleSite', 'sysadmin', 'widget') as $type) {
     $cssFiles[$type] = array();
     foreach ($extensions as $extension) {
         $dir = APP_ROOT_DIR . $extension . '/theme/default/css/' . $type . DIRECTORY_SEPARATOR;
         if (is_dir($dir) && ($handle = opendir($dir))) {
             while (false !== ($entry = readdir($handle))) {
                 if ($entry != '.' && $entry != '..' && substr($entry, -5) == '.less') {
                     $cssFiles[$type][$entry] = realpath($dir . $entry);
                 }
             }
         }
     }
 }
 $cssFilterLess = new LessphpFilter();
 $vars = array();
 foreach ($extensions as $extensionDir) {
     $file = APP_ROOT_DIR . $extensionDir . '/theme/default/variables.ini';
     if (file_exists($file)) {
         $vars = array_merge($vars, parse_ini_file($file, false));
     }
 }
 $cssFilterLess->setPresets($vars);
 foreach ($CONFIG->extensions as $extension) {
     $dir = APP_ROOT_DIR . '/extension/' . $extension . '/theme/default/css/';
     if (is_dir($dir)) {
         $cssFilterLess->addLoadPath(realpath($dir));
     }
 }
 $cssFilterLess->addLoadPath(APP_ROOT_DIR . '/core/theme/default/css/');
 /**
  * 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());
     }
 }