Exemple #1
0
 /**
  * doExecute
  *
  * @return  int
  */
 protected function doExecute()
 {
     $path = $this->getArgument(0);
     $package = $this->getOption('p');
     $folder = $this->console->get('asset.folder', 'asset');
     if ($package = PackageHelper::getPackage($package)) {
         $path = $package->getDir() . '/Resources/asset/' . $path;
     } else {
         $path = WINDWALKER_PUBLIC . '/' . trim($folder, '/') . '/' . $path;
     }
     if (is_file($path)) {
         $files = array(new \SplFileInfo($path));
     } elseif (is_dir($path)) {
         $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::FOLLOW_SYMLINKS));
     } else {
         throw new \InvalidArgumentException('No path');
     }
     /** @var \SplFileInfo $file */
     foreach ($files as $file) {
         $ext = File::getExtension($file->getPathname());
         if (StringHelper::endsWith($file->getBasename(), '.min.' . $ext)) {
             continue;
         }
         if ($ext == 'css') {
             $this->out('[<comment>Compressing</comment>] ' . $file);
             $data = \Minify_CSS_Compressor::process(file_get_contents($file));
             $data = str_replace("\n", ' ', $data);
         } elseif ($ext == 'js') {
             $this->out('[<comment>Compressing</comment>] ' . $file);
             $data = \JSMinPlus::minify(file_get_contents($file));
             $data = str_replace("\n", ';', $data);
         } else {
             continue;
         }
         $newName = $file->getPath() . '/' . File::stripExtension($file->getBasename()) . '.min.' . $ext;
         file_put_contents($newName, $data);
         $this->out('[<info>Compressed</info>] ' . $newName);
     }
 }
 /**
  * Get minify file name.
  *
  * - If in debug mode, we will search for .min file, if minify file not exists, retuen nomral file.
  * - If not in debug mode, we will search for normal file, if normal file not exists but min file does, retuen min file.
  * - If both not exists, return false.
  *
  * @param  string  $path  The path to check file.
  * @param  string  $file  The file name.
  *
  * @return  string|false  The found file or false.
  */
 protected function getMinFile($path, $file)
 {
     $ext = \JFile::getExt($file);
     if (StringHelper::endsWith($file, '.min.' . $ext)) {
         $assetFile = substr($file, 0, -strlen('.min.' . $ext)) . '.' . $ext;
         $assetMinFile = $file;
     } else {
         $assetMinFile = substr($file, 0, -strlen('.' . $ext)) . '.min.' . $ext;
         $assetFile = $file;
     }
     // Use uncompressed file first
     if ($this->debug) {
         if (is_file($path . '/' . $assetFile)) {
             return $assetFile;
         }
         if (is_file($path . '/' . $assetMinFile)) {
             return $assetMinFile;
         }
     } else {
         if (is_file($path . '/' . $assetMinFile)) {
             return $assetMinFile;
         }
         if (is_file($path . '/' . $assetFile)) {
             return $assetFile;
         }
     }
     // All file not found, fallback to default path.
     return false;
 }
 /**
  * testEndsWith
  *
  * @return  void
  */
 public function testEndsWith()
 {
     $string = 'Foo';
     $this->assertTrue(StringHelper::endsWith($string, 'oo'));
     $this->assertFalse(StringHelper::endsWith($string, 'Oo'));
     $this->assertTrue(StringHelper::endsWith($string, 'Oo', false));
     $this->assertFalse(StringHelper::endsWith($string, 'ooooo'));
     $this->assertFalse(StringHelper::endsWith($string, 'uv'));
 }