Skip to content

Commit

Permalink
Fixes to Colletions
Browse files Browse the repository at this point in the history
  • Loading branch information
kaviththiranga committed Dec 10, 2012
1 parent 8d8cef4 commit a4dec7c
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 47 deletions.
70 changes: 69 additions & 1 deletion libraries/joomla/media/collection.php
Expand Up @@ -54,7 +54,6 @@ public function __construct($options = array())
*/ */
public abstract function combine(); public abstract function combine();



/** /**
* Method to set combiner options. * Method to set combiner options.
* *
Expand Down Expand Up @@ -106,6 +105,73 @@ public function addFiles($files =array())
} }
} }


/**
* Static method to get a set of files combined
*
* @param array $files Set of source files
* @param array $options Options for combiner
* @param string $destination Destination file
*
* @return boolean True on success
*
* @throws RuntimeException
*
* @since 12.1
*/
public static function combineFiles($files, $options = array(), $destination = null)
{
// Detect file type
$type = JFile::getExt($files[0]);

if (!self::isSupported($files[0]))
{
throw new RuntimeException(sprintf("Error Loading Collection class for %s file type", $type));
}

// Checks for the destination
if ($destination === null)
{
$type = $extension = pathinfo($files[0], PATHINFO_EXTENSION);

// Check for the file prefix in options, assign default prefix if not dound
if (array_key_exists('PREFIX', $options) && !empty($options['PREFIX']))
{
$destination = str_ireplace('.' . $type, '.' . $options['PREFIX'] . '.' . $type, $files[0]);
}
else
{
$destination = str_ireplace('.' . $type, '.combined.' . $type, $files[0]);
}
}

$options['type'] ? $options['type']: $type;

$combiner = self::getInstance($options);

$combiner->setSources($files);

$combiner->combine();

if (!empty($combiner->_combined))
{
$force = array_key_exists('overwrite', $options) && !empty($options['overwrite']) ? $options['overwrite'] : false;

if (!JFile::exists($destination) || (JFile::exists($destination) && $force))
{
JFile::write($destination, $combiner->getCombined());
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

/** /**
* Method to get source files * Method to get source files
* *
Expand Down Expand Up @@ -192,6 +258,8 @@ public function getOptions()
* *
* @return JMediaCollection Returns a JMediaCollection object * @return JMediaCollection Returns a JMediaCollection object
* *
* @throws RuntimeException
*
* @since 12.1 * @since 12.1
*/ */
public static function getInstance( $options = array()) public static function getInstance( $options = array())
Expand Down
22 changes: 13 additions & 9 deletions libraries/joomla/media/compressor.php
Expand Up @@ -18,12 +18,6 @@
*/ */
abstract class JMediaCompressor abstract class JMediaCompressor
{ {
/**
* @var String To hold uncompressed Code.
* @since 12.1
*/
public $uncompressed = null;

/** /**
* @var int size of uncompressed Code. * @var int size of uncompressed Code.
* @since 12.1 * @since 12.1
Expand Down Expand Up @@ -127,6 +121,10 @@ public function setCompressed($compressed)
*/ */
public function getCompressed() public function getCompressed()
{ {
if ($this->compressed == null)
{
$this->compress();
}
return $this->compressed; return $this->compressed;
} }


Expand Down Expand Up @@ -214,10 +212,12 @@ public function getOptions()
* Compress a CSS/JS file with given options * Compress a CSS/JS file with given options
* *
* @param string $uncompressed The String to be compressed * @param string $uncompressed The String to be compressed
* @param array $options An asssociative array with options. Eg: type, force overwirte, prefix for minified files * @param array $options An associative array with options. Eg: type, force overwrite, prefix for minimised files
* *
* @return string compressed string * @return string compressed string
* *
* @throws RuntimeException
*
* @since 12.1 * @since 12.1
*/ */
public static function compressString( $uncompressed, $options) public static function compressString( $uncompressed, $options)
Expand Down Expand Up @@ -245,11 +245,13 @@ 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 asssociative array with options. Eg: type, force overwirte, 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
* *
* @return boolean false on failure. * @return boolean false on failure.
* *
* @throws RuntimeException
*
* @since 12.1 * @since 12.1
*/ */
public static function compressFile( $sourcefile, $options = array(), $destination = null ) public static function compressFile( $sourcefile, $options = array(), $destination = null )
Expand Down Expand Up @@ -278,7 +280,7 @@ public static function compressFile( $sourcefile, $options = array(), $destinat


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


$compressor->setUncompressed($uncompressed); $compressor->setUncompressed($uncompressed);
Expand Down Expand Up @@ -318,6 +320,8 @@ public static function compressFile( $sourcefile, $options = array(), $destinat
* @param array $options options for the compressor * @param array $options options for the compressor
* *
* @return JMediaCompressor Returns a JMediaCompressor object * @return JMediaCompressor Returns a JMediaCompressor object
*
* @throws RuntimeException
* *
* @since 12.1 * @since 12.1
*/ */
Expand Down
28 changes: 18 additions & 10 deletions libraries/joomla/media/compressor/css.php
Expand Up @@ -18,6 +18,12 @@
*/ */
class JMediaCompressorCss extends JMediaCompressor class JMediaCompressorCss extends JMediaCompressor
{ {
/**
* @var String To hold uncompressed Code.
* @since 12.1
*/
public $uncompressed = null;

/** /**
* Object constructor * Object constructor
* *
Expand All @@ -36,22 +42,24 @@ public function __construct($options = array())
* *
* @return Void * @return Void
* *
* @throws RuntimeException
*
* @since 12.1 * @since 12.1
*/ */
public function compress() public function compress()
{ {
if ($this->uncompressed === null) if ($this->uncompressed === null)
{ {
throw new RuntimeException(JText::sprintf('JMEDIA_CSS_COMPRESSION_ERROR_UNCOMPRESSED_NOTSET')); throw new RuntimeException(sprintf("Error. Source content not set for the compressor"));
} }


$this->compressed = str_replace("\r\n", "\n", $this->uncompressed); $this->compressed = str_replace("\r\n", "\n", $this->uncompressed);


$this->compressed = $this->_preServe($this->compressed); $this->compressed = $this->_preServe($this->compressed);


/* Process all valid comments and apply call back, handleComments() function will return relavent replacements /* Process all valid comments and apply call back, handleComments() function will return relevant replacements
* Second argument is to tell call $this->_handleComments() method and get replacement patterns for matches * Second argument is to tell call $this->_handleComments() method and get replacement patterns for matches
* Delimiter '~' is used because using '/' will make this regex pattern ambigious * Delimiter '~' is used because using '/' will make this regex pattern ambiguous
*/ */
$this->compressed = preg_replace_callback('~\\s*/\\*([\\s\\S]*?)\\*/\\s*~', array($this,'_handleComments'), $this->compressed); $this->compressed = preg_replace_callback('~\\s*/\\*([\\s\\S]*?)\\*/\\s*~', array($this,'_handleComments'), $this->compressed);


Expand Down Expand Up @@ -104,9 +112,9 @@ private function _preServe($source)
} }


/** /**
* Method to detect which replacement patterne to use for identified comments * Method to detect which replacement patterns to use for identified comments
* *
* @param Array $matches bacreferences from preg_replace_callback() * @param Array $matches back references from preg_replace_callback()
* *
* @return string replacements for comments * @return string replacements for comments
* *
Expand Down Expand Up @@ -144,15 +152,15 @@ private function _handleComments($matches)
/** /**
* Method to process css selectors and identify replacements * Method to process css selectors and identify replacements
* *
* @param array $matches bacreferences from preg_replace_callback() * @param array $matches back references from preg_replace_callback()
* *
* @return String replacements for selectors * @return String replacements for selectors
* *
* @since 12.1 * @since 12.1
*/ */
private function _handleSelectors($matches) private function _handleSelectors($matches)
{ {
// Remove space around combinators // Remove space around selectors
return preg_replace('/\\s*([,>+~])\\s*/', '$1', $matches[0]); return preg_replace('/\\s*([,>+~])\\s*/', '$1', $matches[0]);
} }


Expand Down Expand Up @@ -211,7 +219,7 @@ private function _removeWS($source)
(\\b|[#\'"-]) # match 3 = start of value (\\b|[#\'"-]) # match 3 = start of value
/x'; /x';


// Using backreferences 1, 2 and 3 // Using back references 1, 2 and 3
$replacements[] = '$1$2:$3'; $replacements[] = '$1$2:$3';


$tmp = preg_replace($patterns, $replacements, $source); $tmp = preg_replace($patterns, $replacements, $source);
Expand All @@ -234,7 +242,7 @@ private function _minColorCodes($source)
} }


/** /**
* Method to break minified code in to new lines to limit line lengths (optional) * Method to break minimised code in to new lines to limit line lengths (optional)
* *
* @param string $source Source css code * @param string $source Source css code
* *
Expand All @@ -244,7 +252,7 @@ private function _minColorCodes($source)
*/ */
private function _breakInToLines($source) private function _breakInToLines($source)
{ {
// Insert a newline between desendant selectors // Insert a newline between descendant selectors
$source = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $source); $source = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $source);


// Insert a new line after 1st numeric value found within a padding, margin, border or outline property // Insert a new line after 1st numeric value found within a padding, margin, border or outline property
Expand Down

0 comments on commit a4dec7c

Please sign in to comment.