Permalink
Browse files

Fixes to Colletions

  • Loading branch information...
1 parent 8d8cef4 commit a4dec7c945a3521edb9ab4e30fefe34aac65208f @kaviththiranga committed Dec 10, 2012
@@ -54,7 +54,6 @@ public function __construct($options = array())
*/
public abstract function combine();
-
/**
* Method to set combiner options.
*
@@ -107,6 +106,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
*
* @return array Source File
@@ -192,6 +258,8 @@ public function getOptions()
*
* @return JMediaCollection Returns a JMediaCollection object
*
+ * @throws RuntimeException
+ *
* @since 12.1
*/
public static function getInstance( $options = array())
@@ -19,12 +19,6 @@
abstract class JMediaCompressor
{
/**
- * @var String To hold uncompressed Code.
- * @since 12.1
- */
- public $uncompressed = null;
-
- /**
* @var int size of uncompressed Code.
* @since 12.1
*/
@@ -127,6 +121,10 @@ public function setCompressed($compressed)
*/
public function getCompressed()
{
+ if ($this->compressed == null)
+ {
+ $this->compress();
+ }
return $this->compressed;
}
@@ -214,10 +212,12 @@ public function getOptions()
* Compress a CSS/JS file with given options
*
* @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
*
+ * @throws RuntimeException
+ *
* @since 12.1
*/
public static function compressString( $uncompressed, $options)
@@ -245,11 +245,13 @@ public static function compressString( $uncompressed, $options)
* Compress a CSS/JS file with given options
*
* @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
*
* @return boolean false on failure.
*
+ * @throws RuntimeException
+ *
* @since 12.1
*/
public static function compressFile( $sourcefile, $options = array(), $destination = null )
@@ -278,7 +280,7 @@ public static function compressFile( $sourcefile, $options = array(), $destinat
if (!$uncompressed)
{
- throw new Exception("Error reading the file (" . $sourcefile . ") contents");
+ throw new RuntimeException("Error reading the file (" . $sourcefile . ") contents");
}
$compressor->setUncompressed($uncompressed);
@@ -318,6 +320,8 @@ public static function compressFile( $sourcefile, $options = array(), $destinat
* @param array $options options for the compressor
*
* @return JMediaCompressor Returns a JMediaCompressor object
+ *
+ * @throws RuntimeException
*
* @since 12.1
*/
@@ -19,6 +19,12 @@
class JMediaCompressorCss extends JMediaCompressor
{
/**
+ * @var String To hold uncompressed Code.
+ * @since 12.1
+ */
+ public $uncompressed = null;
+
+ /**
* Object constructor
*
* @param Array $options Compression options for CSS Minifier.
@@ -36,22 +42,24 @@ public function __construct($options = array())
*
* @return Void
*
+ * @throws RuntimeException
+ *
* @since 12.1
*/
public function compress()
{
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 = $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
- * 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);
@@ -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
*
@@ -144,15 +152,15 @@ private function _handleComments($matches)
/**
* 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
*
* @since 12.1
*/
private function _handleSelectors($matches)
{
- // Remove space around combinators
+ // Remove space around selectors
return preg_replace('/\\s*([,>+~])\\s*/', '$1', $matches[0]);
}
@@ -211,7 +219,7 @@ private function _removeWS($source)
(\\b|[#\'"-]) # match 3 = start of value
/x';
- // Using backreferences 1, 2 and 3
+ // Using back references 1, 2 and 3
$replacements[] = '$1$2:$3';
$tmp = preg_replace($patterns, $replacements, $source);
@@ -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
*
@@ -244,7 +252,7 @@ private function _minColorCodes($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);
// Insert a new line after 1st numeric value found within a padding, margin, border or outline property
Oops, something went wrong.

0 comments on commit a4dec7c

Please sign in to comment.