Skip to content

Commit

Permalink
Refactored JFile usage in JMediaCompressors
Browse files Browse the repository at this point in the history
  • Loading branch information
kaviththiranga committed Dec 11, 2012
1 parent 1991793 commit 032549f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
16 changes: 11 additions & 5 deletions libraries/joomla/media/collection.php
Expand Up @@ -72,10 +72,16 @@ public function setOptions($options)


$newSignature = md5(serialize($this->options)); $newSignature = md5(serialize($this->options));


if (strcmp($prevSignature, $newSignature)) if (strcmp($prevSignature, $newSignature) !== 0)
{ {
// Remove modified instance from instances // Remove old signature from instance array
unset(self::$instances[$prevSignature]); unset(self::$instances[$prevSignature]);

// Set new instance signature
if (!array_key_exists($newSignature, self::$instances))
{
self::$instances[$newSignature] = $this;
}
} }
} }


Expand Down Expand Up @@ -222,7 +228,7 @@ public function getCombined()
public static function getCollectionTypes() public static function getCollectionTypes()
{ {
// Instantiate variables. // Instantiate variables.
$combiners = array(); $collectionTypes = array();


// Get a list of types. // Get a list of types.
$types = glob(__DIR__ . '/collection/*'); $types = glob(__DIR__ . '/collection/*');
Expand All @@ -248,11 +254,11 @@ public static function getCollectionTypes()
} }


// Combiner names should not have file extensions. // Combiner names should not have file extensions.
$combiners[] = $class; $collectionTypes[] = $class;


} }


return $combiners; return $collectionTypes;
} }


/** /**
Expand Down
58 changes: 42 additions & 16 deletions libraries/joomla/media/compressor.php
Expand Up @@ -145,8 +145,24 @@ public function getCompressed()
*/ */
public function setOptions($options) public function setOptions($options)
{ {
// Merge user defined options with default options $prevSignature = md5(serialize($this->options));

// Merge old options with new options
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);

$newSignature = md5(serialize($this->options));

if (strcmp($prevSignature, $newSignature) !== 0)
{
// Remove old signature from instance array
unset(self::$instances[$prevSignature]);

// Set new instance signature
if (!array_key_exists($newSignature, self::$instances))
{
self::$instances[$newSignature] = $this;
}
}
} }


/** /**
Expand Down Expand Up @@ -174,11 +190,13 @@ public static function getCompressors()
$compressors = array(); $compressors = array();


// Get a list of types. // Get a list of types.
$types = JFolder::files(__DIR__ . '/compressor'); $types = glob(__DIR__ . '/collection/*');


// Loop through the types and find the ones that are available. // Loop through the types and find the ones that are available.
foreach ($types as $type) foreach ($types as $type)
{ {
$type = basename($type);

// Ignore some files. // Ignore some files.
if ($type == 'index.html') if ($type == 'index.html')
{ {
Expand Down Expand Up @@ -250,7 +268,7 @@ public static function compressString( $uncompressed, $options)
/** /**
* Compress a CSS/JS file with given options * Compress a CSS/JS file with given options
* *
* @param string $sourcefile The full file path of the source file. * @param string $sourceFile The full file path of the source file.
* @param array $options An associative array with options. Eg: type, force overwrite, prefix for minified files * @param array $options An associative array with options. Eg: type, force overwrite, prefix for minified files
* @param string $destination The full file path of the destination file. If left empty the compressed file will be returned as string * @param string $destination The full file path of the destination file. If left empty the compressed file will be returned as string
* *
Expand All @@ -260,33 +278,33 @@ public static function compressString( $uncompressed, $options)
* *
* @since 12.1 * @since 12.1
*/ */
public static function compressFile( $sourcefile, $options = array(), $destination = null ) public static function compressFile( $sourceFile, $options = array(), $destination = null )
{ {
$options['type'] = strtolower(JFile::getExt($sourcefile)); $options['type'] = strtolower(pathinfo($sourceFile, PATHINFO_EXTENSION));


if (!self::isSupported($sourcefile)) if (!self::isSupported($sourceFile))
{ {
throw new RuntimeException(sprintf("The file type of the source file is not supported by the Compressors")); throw new RuntimeException(sprintf("The file type of the source file - %s is not supported by the Compressors", $options['type']));
} }
$compressor = self::getInstance($options); $compressor = self::getInstance($options);
$uncompressed = JFile::read($sourcefile); $uncompressed = file_get_contents($sourceFile);


if ($destination === null) if ($destination === null)
{ {
$type = $extension = pathinfo($sourcefile, PATHINFO_EXTENSION); $type = $extension = pathinfo($sourceFile, PATHINFO_EXTENSION);
if (array_key_exists('PREFIX', $options) && !empty($options['PREFIX'])) if (array_key_exists('PREFIX', $options) && !empty($options['PREFIX']))
{ {
$destination = str_ireplace('.' . $type, '.' . $options['PREFIX'] . '.' . $type, $sourcefile); $destination = str_ireplace('.' . $type, '.' . $options['PREFIX'] . '.' . $type, $sourceFile);
} }
else else
{ {
$destination = str_ireplace('.' . $type, '.min.' . $type, $sourcefile); $destination = str_ireplace('.' . $type, '.min.' . $type, $sourceFile);
} }
} }


if (!$uncompressed) if (!$uncompressed)
{ {
throw new RuntimeException("Error reading the file (" . $sourcefile . ") contents"); throw new RuntimeException("Error reading the file (" . $sourceFile . ") contents");
} }


$compressor->setUncompressed($uncompressed); $compressor->setUncompressed($uncompressed);
Expand All @@ -303,9 +321,9 @@ public static function compressFile( $sourcefile, $options = array(), $destinat
// Sets force overwrite option // Sets force overwrite option
$force = array_key_exists('overwrite', $options) && !empty($options['overwrite']) ? $options['overwrite'] : false; $force = array_key_exists('overwrite', $options) && !empty($options['overwrite']) ? $options['overwrite'] : false;


if (!JFile::exists($destination) || (JFile::exists($destination) && $force)) if (!file_exists($destination) || (file_exists($destination) && $force))
{ {
if (JFile::write($destination, $compressor->getCompressed())) if (file_put_contents($destination, $compressor->getCompressed()))
{ {
return true; return true;
} }
Expand Down Expand Up @@ -349,14 +367,22 @@ public static function getInstance($options = array())
throw new RuntimeException(sprintf("Error Loading Compressor class for %s file type", $options['type'])); throw new RuntimeException(sprintf("Error Loading Compressor class for %s file type", $options['type']));
} }


// Modify the signature with all the options
$signature = md5(serialize(array_merge($class::$DEFAULT_OPTIONS, $options)));

if (!empty(self::$instances[$signature]))
{
return self::$instances[$signature];
}

// Create our new JMediaCompressor class based on the options given. // Create our new JMediaCompressor class based on the options given.
try try
{ {
$instance = new $class($options); $instance = new $class($options);
} }
catch (RuntimeException $e) catch (RuntimeException $e)
{ {
throw new RuntimeException(sprintf("Error Loading Collection class for %s file type", $e->getMessage())); throw new RuntimeException(sprintf("Error Loading Collection class for %s file type", $options['type']));
} }


// Set the new connector to the global instances based on signature. // Set the new connector to the global instances based on signature.
Expand Down Expand Up @@ -386,7 +412,7 @@ public static function isSupported($sourceFile)


foreach ($compressors as $class) foreach ($compressors as $class)
{ {
if (strtolower(str_ireplace('JMediaCompressor', '', $class)) === strtolower(JFile::getExt($sourceFile))) if (strtolower(str_ireplace('JMediaCompressor', '', $class)) === strtolower(pathinfo($sourceFile, PATHINFO_EXTENSION)))
{ {
return true; return true;
} }
Expand Down
3 changes: 2 additions & 1 deletion libraries/joomla/media/compressor/css.php
Expand Up @@ -18,6 +18,7 @@
*/ */
class JMediaCompressorCss extends JMediaCompressor class JMediaCompressorCss extends JMediaCompressor
{ {
public static $DEFAULT_OPTIONS = array('REMOVE_COMMENTS' => true, 'MIN_COLOR_CODES' => true, 'LIMIT_LINE_LENGTH' => true);
/** /**
* Object constructor * Object constructor
* *
Expand All @@ -27,7 +28,7 @@ class JMediaCompressorCss extends JMediaCompressor
*/ */
public function __construct($options = array()) public function __construct($options = array())
{ {
$this->options = array('REMOVE_COMMENTS' => true, 'MIN_COLOR_CODES' => true, 'LIMIT_LINE_LENGTH' => true); $this->options = self::$DEFAULT_OPTIONS;
parent::__construct($options); parent::__construct($options);
} }


Expand Down
3 changes: 2 additions & 1 deletion libraries/joomla/media/compressor/js.php
Expand Up @@ -19,6 +19,7 @@
*/ */
class JMediaCompressorJs extends JMediaCompressor class JMediaCompressorJs extends JMediaCompressor
{ {
public static $DEFAULT_OPTIONS = array('REMOVE_COMMENTS' => true, 'CHANGE_ENCODING' => true);


/** /**
* Used to track the index * Used to track the index
Expand Down Expand Up @@ -77,7 +78,7 @@ class JMediaCompressorJs extends JMediaCompressor
*/ */
public function __construct($options = array()) public function __construct($options = array())
{ {
$this->options = array('REMOVE_COMMENTS' => true, 'CHANGE_ENCODING' => true); $this->options = self::$DEFAULT_OPTIONS;


parent::__construct($options); parent::__construct($options);
} }
Expand Down

0 comments on commit 032549f

Please sign in to comment.