From a4dec7c945a3521edb9ab4e30fefe34aac65208f Mon Sep 17 00:00:00 2001 From: Kavith Thiranga Date: Mon, 10 Dec 2012 18:39:12 +0530 Subject: [PATCH] Fixes to Colletions --- libraries/joomla/media/collection.php | 70 ++++++++++++++++++- libraries/joomla/media/compressor.php | 22 +++--- libraries/joomla/media/compressor/css.php | 28 +++++--- libraries/joomla/media/compressor/js.php | 35 +++++----- ...binerTest.php => JMediaCollectionTest.php} | 4 +- .../joomla/media/JMediaCompressorTest.php | 10 +-- .../compressor/JMediaCompressorCssTest.php | 6 +- .../compressor/JMediaCompressorJsTest.php | 4 +- 8 files changed, 132 insertions(+), 47 deletions(-) rename tests/suites/unit/joomla/media/{JMediaCombinerTest.php => JMediaCollectionTest.php} (93%) diff --git a/libraries/joomla/media/collection.php b/libraries/joomla/media/collection.php index bbc4bd79dc..0c0d6d212f 100644 --- a/libraries/joomla/media/collection.php +++ b/libraries/joomla/media/collection.php @@ -54,7 +54,6 @@ public function __construct($options = array()) */ public abstract function combine(); - /** * Method to set combiner options. * @@ -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 * @@ -192,6 +258,8 @@ public function getOptions() * * @return JMediaCollection Returns a JMediaCollection object * + * @throws RuntimeException + * * @since 12.1 */ public static function getInstance( $options = array()) diff --git a/libraries/joomla/media/compressor.php b/libraries/joomla/media/compressor.php index a07abb54f5..a898d50bda 100644 --- a/libraries/joomla/media/compressor.php +++ b/libraries/joomla/media/compressor.php @@ -18,12 +18,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 */ diff --git a/libraries/joomla/media/compressor/css.php b/libraries/joomla/media/compressor/css.php index dc6438661e..c4d4ad23ba 100644 --- a/libraries/joomla/media/compressor/css.php +++ b/libraries/joomla/media/compressor/css.php @@ -18,6 +18,12 @@ */ class JMediaCompressorCss extends JMediaCompressor { + /** + * @var String To hold uncompressed Code. + * @since 12.1 + */ + public $uncompressed = null; + /** * Object constructor * @@ -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,7 +152,7 @@ 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 * @@ -152,7 +160,7 @@ private function _handleComments($matches) */ 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 diff --git a/libraries/joomla/media/compressor/js.php b/libraries/joomla/media/compressor/js.php index 9b9bd327ab..6cc01aea6a 100644 --- a/libraries/joomla/media/compressor/js.php +++ b/libraries/joomla/media/compressor/js.php @@ -53,7 +53,7 @@ class JMediaCompressorJs extends JMediaCompressor private $_startLength = 0; /** - * to hold a preloaded char to peek nextindex + * to hold a preloaded char to peek next index * * @var char * @since 12.1 @@ -61,7 +61,7 @@ class JMediaCompressorJs extends JMediaCompressor private $_preLoaded = null; /** - * last preocessed char used to identify keywords + * last processed char used to identify keywords * * @var char * @since 12.1 @@ -71,7 +71,7 @@ class JMediaCompressorJs extends JMediaCompressor /** * Object Constructor one parameters. * - * @param Array $options Compression options for CSS Minifier. + * @param Array $options Compression options for CSS Compressor. * * @since 12.1 */ @@ -86,14 +86,16 @@ public function __construct($options = array()) * Method to compress the code. * * @return void - * + * + * @throws RuntimeException + * * @since 12.1 */ public function compress() { if ($this->uncompressed === null) { - throw new RuntimeException(JText::sprintf('JMEDIA_JS_COMPRESSION_ERROR_UNCOMPRESSED_NOTSET')); + throw new RuntimeException(sprintf("Error. Source content not set for the compressor")); } $encoding = $this->_changeCharEncoding(); @@ -145,12 +147,12 @@ public function compress() }// End While - // Resets multibyte encoding type + // Resets multi byte encoding type if ($encoding !== null) { mb_internal_encoding($encoding); } - + $this->compressed = trim($this->compressed); $this->compressedSize = strlen($this->compressed); } @@ -162,7 +164,7 @@ public function compress() * * @return void * - * @throws JMediaException + * @throws RuntimeException * * @since 12.1 */ @@ -202,7 +204,7 @@ private function _executeCommand($cmd) if ($this->_a === '\n') { - throw new JMediaException("Unterminated string at index " . $this->_nextIndex); + throw new RuntimeException(sprintf("Unterminated string at index %d", $this->_nextIndex)); } if ($this->_a === '\\') @@ -237,7 +239,7 @@ private function _executeCommand($cmd) } elseif (ord($this->_a) <= 10) { - throw new JMediaException("Unterminated Regular expression at index" . $this->_nextIndex); + throw new RuntimeException(sprintf("Unterminated Regular expression at index %d", $this->_nextIndex)); } $this->compressed .= $this->_a; @@ -315,7 +317,7 @@ private function _checkRegExp() /** * Method to change multi-byte encoding * - * @return string current multibyte encoding type + * @return string current multi byte encoding type * * @since 12.1 */ @@ -399,7 +401,9 @@ private function _getB() * Method to handle comments when getting next B * * @return string Immediate char on newline after single line comment or space if a multiline comment or the whole comment in some conditions - * + * + * @throws RuntimeException + * * @since 12.1 */ private function _handleComments() @@ -462,7 +466,7 @@ private function _handleComments() } elseif ($tmp === null) { - throw new JMediaException("Unterminated multiline comment at index" . $this->_nextIndex); + throw new RuntimeException(sprintf("Unterminated multi line comment at index %d", $this->_nextIndex)); } $comment .= $tmp; @@ -471,7 +475,7 @@ private function _handleComments() return $comment; } - + /** * Method to clear compressor data * @@ -482,13 +486,12 @@ private function _handleComments() public function clear() { parent::clear(); - $this->_a = "\n"; $this->_b = ''; $this->_nextIndex = 0; $this->_startLength = 0; $this->_preLoaded = ''; $this->_previousChar = ''; - + } } diff --git a/tests/suites/unit/joomla/media/JMediaCombinerTest.php b/tests/suites/unit/joomla/media/JMediaCollectionTest.php similarity index 93% rename from tests/suites/unit/joomla/media/JMediaCombinerTest.php rename to tests/suites/unit/joomla/media/JMediaCollectionTest.php index 72a1ddcac7..ef3282896d 100644 --- a/tests/suites/unit/joomla/media/JMediaCombinerTest.php +++ b/tests/suites/unit/joomla/media/JMediaCollectionTest.php @@ -13,7 +13,7 @@ /** * Test class for JMediaCompressor. */ -class JMediaCombinerTest extends TestCase +class JMediaCollectionTest extends TestCase { /** * @var JMediaCompressor @@ -133,7 +133,7 @@ public function testClear() $this->assertAttributeEquals(array(), 'sources', $this->object); $this->assertAttributeEquals(0, 'sourceCount', $this->object); - $this->assertAttributeEquals(null, '_combined', $this->object); + $this->assertAttributeEquals(null, 'combined', $this->object); } diff --git a/tests/suites/unit/joomla/media/JMediaCompressorTest.php b/tests/suites/unit/joomla/media/JMediaCompressorTest.php index 44ae1e2b90..4507f14c4c 100644 --- a/tests/suites/unit/joomla/media/JMediaCompressorTest.php +++ b/tests/suites/unit/joomla/media/JMediaCompressorTest.php @@ -51,7 +51,7 @@ public function testSetCompressed() $random = rand(); $this->object->setCompressed($random); $test = $this->object->getCompressed(); - $this->assertEquals($random,$test); + $this->assertEquals($random, $test); $this->object->clear(); } @@ -61,6 +61,7 @@ public function testSetUncompressed() $this->object->setUncompressed($random); $test = $this->object->getUncompressed(); $this->assertEquals($random,$test); + $this->assertAttributeEquals($random, 'uncompressed', $this->object); } public function testGetRatio() @@ -133,15 +134,16 @@ public function testIsSupported() $this->assertTrue(JMediaCompressor::isSupported($file2)); } + public function testClear() { - $this->object->setUncompressed(rand()); + $this->object->setUncompressed("Compress This"); $this->object->compress(); $this->object->clear(); $this->assertEquals(null, $this->object->getUncompressed()); $this->assertEquals(null, $this->object->getcompressed()); - $this->assertAttributeEquals(null, '_compressedSize', $this->object); - $this->assertAttributeEquals(null, '_uncompressedSize', $this->object); + $this->assertAttributeEquals(null, 'compressedSize', $this->object); + $this->assertAttributeEquals(null, 'uncompressedSize', $this->object); } } \ No newline at end of file diff --git a/tests/suites/unit/joomla/media/compressor/JMediaCompressorCssTest.php b/tests/suites/unit/joomla/media/compressor/JMediaCompressorCssTest.php index 538a1a6f31..40171ac6cf 100644 --- a/tests/suites/unit/joomla/media/compressor/JMediaCompressorCssTest.php +++ b/tests/suites/unit/joomla/media/compressor/JMediaCompressorCssTest.php @@ -13,7 +13,7 @@ /** * Test class for JMediaCompressor. */ -class JMediaCompressorTest extends TestCase +class JMediaCompressorCssTest extends TestCase { /** * @var JMediaCompressor @@ -103,8 +103,8 @@ public function testClear() $this->assertEquals(null, $this->object->getUncompressed()); $this->assertEquals(null, $this->object->getcompressed()); - $this->assertAttributeEquals(null, '_compressedSize', $this->object); - $this->assertAttributeEquals(null, '_uncompressedSize', $this->object); + $this->assertAttributeEquals(null, 'compressedSize', $this->object); + $this->assertAttributeEquals(null, 'uncompressedSize', $this->object); $this->assertAttributeEquals(false, '_inHack', $this->object); } } \ No newline at end of file diff --git a/tests/suites/unit/joomla/media/compressor/JMediaCompressorJsTest.php b/tests/suites/unit/joomla/media/compressor/JMediaCompressorJsTest.php index 73dfb1c691..6a00ba6837 100644 --- a/tests/suites/unit/joomla/media/compressor/JMediaCompressorJsTest.php +++ b/tests/suites/unit/joomla/media/compressor/JMediaCompressorJsTest.php @@ -143,9 +143,9 @@ public function testClear() $this->assertEquals(null, $this->object->getcompressed()); - $this->assertAttributeEquals(null, '_compressedSize', $this->object); + $this->assertAttributeEquals(null, 'compressedSize', $this->object); - $this->assertAttributeEquals(null, '_uncompressedSize', $this->object); + $this->assertAttributeEquals(null, 'uncompressedSize', $this->object); $this->assertAttributeEquals("\n", '_a', $this->object);