Permalink
Browse files

Merge pull request #1 from jfusion/JMedia

Created unit tests and improved the API by Marius.
  • Loading branch information...
2 parents f203de1 + 51b4c41 commit 5a8d9a743225a0ca24e265a1cc9a1b93df7bdae0 @kaviththiranga committed Jul 20, 2012
@@ -22,25 +22,37 @@
* @var String To hold uncompressed Code.
* @since 12.1
*/
- public $uncompressed = null;
+ public $_uncompressed = null;
/**
- * @var int length of uncompressed Code.
+ * @var int size of uncompressed Code.
* @since 12.1
*/
- public $length;
+ public $_uncompressedSize = null;
+
+ /**
+ * @var String To hold compressed Code.
+ * @since 12.1
+ */
+ protected $_compressed = null;
+
+ /**
+ * @var int size of compressed Code.
+ * @since 12.1
+ */
+ public $_compressedSize = null;
/**
* @var Array Compression options for CSS Minifier.
* @since 12.1
*/
protected $_options = array();
- /**
- * @var String To hold compressed Code.
- * @since 12.1
+ /**
+ * @var array JMediaCompressor instances container.
+ * @since 11.1
*/
- protected $_compressed = null;
+ protected static $instances = array();
/**
* Method to set uncompressed code.
@@ -53,10 +65,49 @@
*/
public function setUncompressed($uncompressed)
{
- $this->uncompressed = $uncompressed;
- $this->length = strlen($this->uncompressed);
+ $this->_uncompressed = $uncompressed;
+ $this->_uncompressedSize = strlen($this->_uncompressed);
}
+ /**
+ * Method to get uncompressed code.
+ *
+ * @return String uncompressed code.
+ *
+ * @since 12.1
+ */
+ public function getUncompressed()
+ {
+ return $this->_uncompressed;
+ }
+
+ /**
+ * Method to set uncompressed code.
+ *
+ * @param string $uncompressed Uncomressed Code.
+ *
+ * @return void
+ *
+ * @since 12.1
+ */
+ public function setCompressed($compressed)
+ {
+ $this->_compressed = $compressed;
+ $this->_compressedSize = strlen($this->_compressed);
+ }
+
+ /**
+ * Method to get compressed code.
+ *
+ * @return String compressed code.
+ *
+ * @since 12.1
+ */
+ public function getCompressed()
+ {
+ return $this->_compressed;
+ }
+
/**
* Method to set compression options.
*
@@ -85,17 +136,6 @@ public function __construct($options = array())
$this->_options = array_merge($options, $this->_options);
}
- /**
- * Method to get compressed code.
- *
- * @return String compressed code.
- *
- * @since 12.1
- */
- public function getCompressed()
- {
- return trim($this->_compressed);
- }
/**
* Method to get compressed ratio.
@@ -106,7 +146,7 @@ public function getCompressed()
*/
public function getRatio()
{
- return $ratio = 0.0 + ( strlen($this->_compressed) / $this->length * 100 );
+ return round(($this->_compressedSize / $this->_uncompressedSize * 100),2);
}
/**
@@ -156,24 +196,47 @@ public static function getCompressors()
return $compressors;
}
+ /**
+ * 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: force overwirte, prefix for minified files
+ * @param string $destination The full file path of the destination file.
+ *
+ * @return boolean false on failure.
+ *
+ * @since 12.1
+ */
+ public static function compressString( $uncompressed, $options = array())
+ {
+ $compressor = self::getInstance( $options );
+ $compressor->setUncompressed ($uncompressed );
+
+ try
+ {
+ $compressor->compress();
+ }
+ catch (Exception $e)
+ {
+ return false;
+ }
+ return $compressor->getCompressed;
+ }
+
/**
* 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: force overwirte, prefix for minified files
- * @param string $destination The full file path of the destination file.
+ * @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.
*
* @since 12.1
*/
public static function compressFile( $sourcefile, $options = array(), $destination = null )
{
- // Detect the extension of file
- $type = JFile::getExt(strtolower($sourcefile));
-
- $compressor = self::getInstance($type, $options);
-
+ $compressor = self::getInstance( $options);
$uncompressed = JFile::read($sourcefile);
// Sets force overwrite option
@@ -182,7 +245,8 @@ public static function compressFile( $sourcefile, $options = array(), $destinat
if ($destination === null)
{
- if (array_key_exists('PREFIX', $options) && !empty($options['PREFIX']))
+ $type = $extension = pathinfo($sourcefile, PATHINFO_EXTENSION);
+ if (array_key_exists('PREFIX', $options) && !empty($options['PREFIX']))
{
$destination = str_ireplace('.' . $type, '.' . $options['PREFIX'] . '.' . $type, $sourcefile);
}
@@ -197,16 +261,16 @@ public static function compressFile( $sourcefile, $options = array(), $destinat
throw new JMediaException("Error reading the file (" . $sourcefile . ") contents");
}
- $compressor->setUncompressed($uncompressed);
+ $compressor->setUncompressed($uncompressed);
- try
- {
- $compressor->compress();
- }
- catch (Exception $e)
- {
- return false;
- }
+ try
+ {
+ $compressor->compress();
+ }
+ catch (Exception $e)
+ {
+ return false;
+ }
if (JFile::write($destination, $compressor->getCompressed()))
{
@@ -228,33 +292,43 @@ public static function compressFile( $sourcefile, $options = array(), $destinat
*
* @since 12.1
*/
- public static function getInstance($type, $options = array())
+ public static function getInstance($options = array())
{
- // Derive the class name from the type.
- $class = 'JMediaCompressor' . ucfirst(strtolower($type));
+ // Get the options signature for the database connector.
+ $signature = md5(serialize($options));
- // Load the class
- jimport('joomla.media.compressor.' . $class);
+ // If we already have a database connector instance for these options then just use that.
+ if (empty(self::$instances[$signature]))
+ {
+ // Derive the class name from the type.
+ $class = 'JMediaCompressor' . ucfirst(strtolower($options['type']));
- // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
- if (!class_exists($class))
- {
- throw new RuntimeException(JText::sprintf('JMEDIA_ERROR_LOAD_COMPRESSOR', $type));
- }
+ // Load the class
+ jimport('joomla.media.compressor.' . $class);
- // Create our new JMediaCompressor class based on the options given.
- try
- {
- $instance = new $class($options);
- }
- catch (RuntimeException $e)
- {
- throw new RuntimeException(JText::sprintf('JLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage()));
- }
+ // If the class still doesn't exist we have nothing left to do but throw an exception. We did our best.
+ if (!class_exists($class))
+ {
+ throw new RuntimeException(JText::sprintf('JMEDIA_ERROR_LOAD_COMPRESSOR', $options['type']));
+ }
- return $instance;
- }
+ // Create our new JMediaCompressor class based on the options given.
+ try
+ {
+ $instance = new $class($options);
+ }
+ catch (RuntimeException $e)
+ {
+ throw new RuntimeException(JText::sprintf('JLIB_DATABASE_ERROR_CONNECT_DATABASE', $e->getMessage()));
+ }
+
+ // Set the new connector to the global instances based on signature.
+ self::$instances[$signature] = $instance;
+ }
+
+ return self::$instances[$signature];
+ }
/**
* Method to test if supported
@@ -48,12 +48,12 @@ public function __construct($options = array())
*/
public function compress()
{
- if ($this->uncompressed === null)
+ if ($this->_uncompressed === null)
{
throw new RuntimeException(JText::sprintf('JMEDIA_CSS_COMPRESSION_ERROR_UNCOMPRESSED_NOTSET'));
}
- $this->_compressed = str_replace("\r\n", "\n", $this->uncompressed);
+ $this->_compressed = str_replace("\r\n", "\n", $this->_uncompressed);
$this->_compressed = $this->_preServe($this->_compressed);
@@ -91,14 +91,14 @@ public function __construct($options = array())
*/
public function compress()
{
- if ($this->uncompressed === null)
+ if ($this->_uncompressed === null)
{
throw new RuntimeException(JText::sprintf('JMEDIA_JS_COMPRESSION_ERROR_UNCOMPRESSED_NOTSET'));
}
$encoding = $this->_changeCharEncoding();
- $this->uncompressed = str_replace("\r\n", "\n", $this->uncompressed);
- $this->_startLength = strlen($this->uncompressed);
+ $this->_uncompressed = str_replace("\r\n", "\n", $this->_uncompressed);
+ $this->_startLength = strlen($this->_uncompressed);
/* Commands to determine start point of switch in _executeCommand()
* Command 1 : Keep A
@@ -169,7 +169,7 @@ private function _executeCommand($cmd)
// Prevent + + or - - becomes ++ or --
if ($cmd === 3 && ($this->_a === '+' || $this->_a === '-') && $this->_b === ' ' )
{
- if ($this->uncompressed[$this->_nextIndex] === $this->_a)
+ if ($this->_uncompressed[$this->_nextIndex] === $this->_a)
{
$cmd = 1;
}
@@ -342,7 +342,7 @@ private function _next()
if ($this->_nextIndex < $this->_startLength)
{
- $char = $this->uncompressed[$this->_nextIndex];
+ $char = $this->_uncompressed[$this->_nextIndex];
$this->_nextIndex++;
}
else
@@ -380,7 +380,7 @@ private function _getB()
return $nextB;
}
- $this->_preLoaded = $this->uncompressed[$this->_nextIndex];
+ $this->_preLoaded = $this->_uncompressed[$this->_nextIndex];
if ($this->_preLoaded === '/' || $this->_preLoaded === '*')
{
@@ -439,7 +439,7 @@ private function _handleComments()
if ($tmp === '*')
{
- if ($this->uncompressed[$this->_nextIndex] === '/')// End of comment
+ if ($this->_uncompressed[$this->_nextIndex] === '/')// End of comment
{
$this->_next();
Oops, something went wrong.

0 comments on commit 5a8d9a7

Please sign in to comment.