Permalink
Browse files

Merge branch 'NativeFileFunctions' into JMediaCollection

  • Loading branch information...
2 parents 932898e + a5018ca commit d42c3b7b26fc3f8dd803f4f19e97441031b2572f @kaviththiranga committed Dec 14, 2012
@@ -65,8 +65,24 @@ public function __construct($options = array())
*/
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);
+
+ $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;
+ }
+ }
}
/**
@@ -88,8 +104,12 @@ public function addFiles($files =array())
foreach ($files as $file)
{
// Check file ext for compatibility
- if (JFile::getExt($file) == $type)
+ if (pathinfo($file, PATHINFO_EXTENSION) == $type)
{
+ if (!file_exists($file))
+ {
+ throw new RuntimeException(sprintf("%s File not exists", $file));
+ }
// Check whether file already registered
if (!in_array($file, $this->sources))
{
@@ -99,7 +119,7 @@ public function addFiles($files =array())
}
else
{
- throw new RuntimeException(sprintf("Multiple File types detected in files array. %s"), $type);
+ throw new RuntimeException(sprintf("Multiple File types detected in files array. %s", $type));
}
}
@@ -121,7 +141,7 @@ public function addFiles($files =array())
public static function combineFiles($files, $options = array(), $destination = null)
{
// Detect file type
- $type = JFile::getExt($files[0]);
+ $type = pathinfo($files[0], PATHINFO_EXTENSION);
if (!self::isSupported($files[0]))
{
@@ -156,9 +176,9 @@ public static function combineFiles($files, $options = array(), $destination = n
{
$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))
{
- JFile::write($destination, $combiner->getCombined());
+ file_put_contents($destination, $combiner->getCombined());
return true;
}
else
@@ -208,14 +228,16 @@ public function getCombined()
public static function getCollectionTypes()
{
// Instantiate variables.
- $combiners = array();
+ $collectionTypes = array();
// Get a list of types.
- $types = JFolder::files(__DIR__ . '/collection');
+ $types = glob(__DIR__ . '/collection/*');
// Loop through the types and find the ones that are available.
foreach ($types as $type)
{
+ $type = basename($type);
+
// Ignore some files.
if ($type == 'index.html')
{
@@ -232,11 +254,11 @@ public static function getCollectionTypes()
}
// Combiner names should not have file extensions.
- $combiners[] = $class;
+ $collectionTypes[] = $class;
}
- return $combiners;
+ return $collectionTypes;
}
/**
@@ -265,10 +287,10 @@ public function getOptions()
public static function getInstance( $options = array())
{
- // Get the options signature for the database connector.
+ // Get the options signature for the instance
$signature = md5(serialize($options));
- // If we already have a database connector instance for these options then just use that.
+ // If we already have a Collection instance for these options then just use that.
if (empty(self::$instances[$signature]))
{
// Derive the class name from the type.
@@ -280,6 +302,13 @@ public static function getInstance( $options = array())
throw new RuntimeException(sprintf("Error Loading Collection 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.
try
{
@@ -313,11 +342,11 @@ public static function getInstance( $options = array())
*/
public static function isSupported($sourceFile)
{
- $combiners = self::getCollectionTypes();
+ $collectionTypes = self::getCollectionTypes();
- foreach ($combiners as $class)
+ foreach ($collectionTypes as $class)
{
- if (strtolower(str_ireplace('JMediaCollection', '', $class)) === strtolower(JFile::getExt($sourceFile)))
+ if (strtolower(str_ireplace('JMediaCollection', '', $class)) === strtolower(pathinfo($sourceFile, PATHINFO_EXTENSION)))
{
return true;
}
@@ -18,6 +18,9 @@
*/
class JMediaCollectionCss extends JMediaCollection
{
+
+ public static $DEFAULT_OPTIONS = array('COMPRESS' => false, 'FILE_COMMENTS' => true, 'COMPRESS_OPTIONS' => array(), 'COMPRESSOR' => null);
+
/**
* Constructor
*
@@ -27,7 +30,7 @@ class JMediaCollectionCss extends JMediaCollection
*/
public function __construct($options = array())
{
- $this->options = array('COMPRESS' => false, 'FILE_COMMENTS' => true, 'COMPRESS_OPTIONS' => array());
+ $this->options = self::$DEFAULT_OPTIONS;
parent::__construct($options);
}
@@ -46,7 +49,7 @@ public function combine()
{
if ($this->options['FILE_COMMENTS'])
{
- $this->combined .= '/** File : ' . JFile::getName($file) . ' : Start **/' . "\n\n";
+ $this->combined .= '/** File : ' . basename($file) . ' : Start **/' . "\n\n";
}
if ($this->options['COMPRESS'])
@@ -63,17 +66,17 @@ public function combine()
}
else
{
- $this->combined .= JMediaCompressor::compressString(JFile::read($file), $this->options['COMPRESS_OPTIONS']) . "\n\n";
+ $this->combined .= JMediaCompressor::compressString(file_get_contents($file), $this->options['COMPRESS_OPTIONS']) . "\n\n";
}
}
else
{
- $this->combined .= JFile::read($file) . "\n\n";
+ $this->combined .= file_get_contents($file) . "\n\n";
}
if ($this->options['FILE_COMMENTS'])
{
- $this->combined .= '/** File : ' . JFile::getName($file) . ' : End **/' . "\n\n";
+ $this->combined .= '/** File : ' . basename($file) . ' : End **/' . "\n\n";
}
}
@@ -19,6 +19,8 @@
class JMediaCollectionJs extends JMediaCollection
{
+ public static $DEFAULT_OPTIONS = array('COMPRESS' => false, 'FILE_COMMENTS' => true, 'COMPRESS_OPTIONS' => array(), 'COMPRESSOR' => null);
+
/**
* Constructor
*
@@ -28,7 +30,7 @@ class JMediaCollectionJs extends JMediaCollection
*/
public function __construct($options = array())
{
- $this->options = array('COMPRESS' => false, 'FILE_COMMENTS' => true, 'COMPRESS_OPTIONS' => array());
+ $this->options = self::$DEFAULT_OPTIONS;
parent::__construct($options);
}
@@ -47,7 +49,7 @@ public function combine()
{
if ($this->options['FILE_COMMENTS'])
{
- $this->combined .= '/** File : ' . JFile::getName($file) . ' : Start **/' . "\n\n";
+ $this->combined .= '/** File : ' . basename($file) . ' : Start **/' . "\n\n";
}
if ($this->options['COMPRESS'])
@@ -64,17 +66,17 @@ public function combine()
}
else
{
- $this->combined .= JMediaCompressor::compressString(JFile::read($file), $this->options['COMPRESS_OPTIONS']) . "\n\n";
+ $this->combined .= JMediaCompressor::compressString(file_get_contents($file), $this->options['COMPRESS_OPTIONS']) . "\n\n";
}
}
else
{
- $this->combined .= JFile::read($file) . "\n\n";
+ $this->combined .= file_get_contents($file) . "\n\n";
}
if ($this->options['FILE_COMMENTS'])
{
- $this->combined .= '/** File : ' . JFile::getName($file) . ' : End **/' . "\n\n";
+ $this->combined .= '/** File : ' . basename($file) . ' : End **/' . "\n\n";
}
}
@@ -79,7 +79,7 @@ public function __construct($options = array())
/**
* Method to set uncompressed code.
*
- * @param string $uncompressed Uncomressed Code.
+ * @param string $uncompressed Uncompressed Code.
*
* @return void
*
@@ -104,7 +104,7 @@ public function getUncompressed()
}
/**
- * Method to set uncompressed code.
+ * Method to set compressed code.
*
* @param string $compressed compressed Code.
*
@@ -145,8 +145,24 @@ public function getCompressed()
*/
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);
+
+ $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;
+ }
+ }
}
/**
@@ -174,11 +190,13 @@ public static function getCompressors()
$compressors = array();
// 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.
foreach ($types as $type)
{
+ $type = basename($type);
+
// Ignore some files.
if ($type == 'index.html')
{
@@ -250,7 +268,7 @@ 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 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 string $destination The full file path of the destination file. If left empty the compressed file will be returned as string
*
@@ -260,33 +278,33 @@ public static function compressString( $uncompressed, $options)
*
* @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);
- $uncompressed = JFile::read($sourcefile);
+ $uncompressed = file_get_contents($sourceFile);
if ($destination === null)
{
- $type = $extension = pathinfo($sourcefile, PATHINFO_EXTENSION);
+ $type = $extension = pathinfo($sourceFile, PATHINFO_EXTENSION);
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
{
- $destination = str_ireplace('.' . $type, '.min.' . $type, $sourcefile);
+ $destination = str_ireplace('.' . $type, '.min.' . $type, $sourceFile);
}
}
if (!$uncompressed)
{
- throw new RuntimeException("Error reading the file (" . $sourcefile . ") contents");
+ throw new RuntimeException("Error reading the file (" . $sourceFile . ") contents");
}
$compressor->setUncompressed($uncompressed);
@@ -303,9 +321,9 @@ public static function compressFile( $sourcefile, $options = array(), $destinat
// Sets force overwrite option
$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;
}
@@ -349,14 +367,22 @@ public static function getInstance($options = array())
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.
try
{
$instance = new $class($options);
}
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.
@@ -386,7 +412,7 @@ public static function isSupported($sourceFile)
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;
}
Oops, something went wrong.

0 comments on commit d42c3b7

Please sign in to comment.