Adds new helper classes.
This commit is contained in:
@ -555,6 +555,44 @@ trait Utilities
|
||||
{
|
||||
return FileHelper::write($path, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* get all the file paths in folder and sub folders
|
||||
*
|
||||
* @param string $folder The local path to parse
|
||||
* @param array $fileTypes The type of files to get
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.0.9
|
||||
*
|
||||
* @deprecated 4.0 - Use FileHelper::getPaths($folder, $fileTypes , $recurse, $full);
|
||||
*/
|
||||
public static function getAllFilePaths($folder, $fileTypes = array('\.php', '\.js', '\.css', '\.less'), $recurse = true, $full = true)
|
||||
{
|
||||
return FileHelper::getPaths($folder, $fileTypes , $recurse, $full);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file path or url
|
||||
*
|
||||
* @param string $type The (url/path) type to return
|
||||
* @param string $target The Params Target name (if set)
|
||||
* @param string $fileType The kind of filename to generate (if not set no file name is generated)
|
||||
* @param string $key The key to adjust the filename (if not set ignored)
|
||||
* @param string $default The default path if not set in Params (fallback path)
|
||||
* @param bool $createIfNotSet The switch to create the folder if not found
|
||||
*
|
||||
* @return string On success the path or url is returned based on the type requested
|
||||
*
|
||||
* @since 3.0.9
|
||||
*
|
||||
* @deprecated 4.0 - Use FileHelper::getPath($type, $target, $fileType, $key, $default, $createIfNotSet);
|
||||
*/
|
||||
public static function getFilePath($type = 'path', $target = 'filepath', $fileType = null, $key = '', $default = '', $createIfNotSet = true)
|
||||
{
|
||||
return FileHelper::getPath($type, $target, $fileType, $key, $default, $createIfNotSet);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -13,11 +13,13 @@
|
||||
namespace VDM\Joomla\Utilities;
|
||||
|
||||
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\Filesystem\Path;
|
||||
use Joomla\CMS\Filesystem\File;
|
||||
use Joomla\CMS\Filesystem\Folder;
|
||||
use Joomla\CMS\Component\ComponentHelper;
|
||||
use Joomla\Archive\Archive;
|
||||
|
||||
|
||||
@ -37,6 +39,15 @@ abstract class FileHelper
|
||||
*/
|
||||
protected static $curlError = false;
|
||||
|
||||
/**
|
||||
* The component params
|
||||
*
|
||||
* @var object
|
||||
*
|
||||
* @since 3.0.9
|
||||
*/
|
||||
protected static $params = false;
|
||||
|
||||
/**
|
||||
* The zipper method
|
||||
*
|
||||
@ -176,6 +187,141 @@ abstract class FileHelper
|
||||
}
|
||||
return $klaar;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all the file paths in folder and sub folders
|
||||
*
|
||||
* @param string $folder The local path to parse
|
||||
* @param array $fileTypes The type of files to get
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static function getPaths($folder, $fileTypes = array('\.php', '\.js', '\.css', '\.less'), $recurse = true, $full = true)
|
||||
{
|
||||
if (Folder::exists($folder))
|
||||
{
|
||||
// we must first store the current woking directory
|
||||
$joomla = getcwd();
|
||||
// we are changing the working directory to the component path
|
||||
chdir($folder);
|
||||
|
||||
// make sure we have file type filter
|
||||
if (ArrayHelper::check($fileTypes))
|
||||
{
|
||||
// get the files
|
||||
foreach ($fileTypes as $type)
|
||||
{
|
||||
// get a list of files in the current directory tree
|
||||
$files[] = Folder::files('.', $type, $recurse, $full);
|
||||
}
|
||||
}
|
||||
elseif (StringHelper::check($fileTypes))
|
||||
{
|
||||
// get a list of files in the current directory tree
|
||||
$files[] = Folder::files('.', $fileTypes, $recurse, $full);
|
||||
}
|
||||
else
|
||||
{
|
||||
// get a list of files in the current directory tree
|
||||
$files[] = Folder::files('.', '.', $recurse, $full);
|
||||
}
|
||||
|
||||
// change back to Joomla working directory
|
||||
chdir($joomla);
|
||||
|
||||
// return array of files
|
||||
return array_map( function($file) { return str_replace('./', '/', $file); }, (array) ArrayHelper::merge($files));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file path or url
|
||||
*
|
||||
* @param string $type The (url/path) type to return
|
||||
* @param string $target The Params Target name (if set)
|
||||
* @param string $fileType The kind of filename to generate (if not set no file name is generated)
|
||||
* @param string $key The key to adjust the filename (if not set ignored)
|
||||
* @param string $default The default path if not set in Params (fallback path)
|
||||
* @param bool $createIfNotSet The switch to create the folder if not found
|
||||
*
|
||||
* @return string On success the path or url is returned based on the type requested
|
||||
*
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static function getPath($type = 'path', $target = 'filepath', $fileType = null, $key = '', $default = '', $createIfNotSet = true)
|
||||
{
|
||||
// make sure to always have a string/path
|
||||
if(!StringHelper::check($default))
|
||||
{
|
||||
$default = JPATH_SITE . '/images/';
|
||||
}
|
||||
|
||||
// get the global settings
|
||||
if (!ObjectHelper::check(self::$params))
|
||||
{
|
||||
self::$params = ComponentHelper::getParams('com_componentbuilder');
|
||||
}
|
||||
$filePath = self::$params->get($target, $default);
|
||||
|
||||
// check the file path (revert to default only of not a hidden file path)
|
||||
if ('hiddenfilepath' !== $target && strpos($filePath, JPATH_SITE) === false)
|
||||
{
|
||||
$filePath = $default;
|
||||
}
|
||||
|
||||
// create the folder if it does not exist
|
||||
if ($createIfNotSet && !Folder::exists($filePath))
|
||||
{
|
||||
Folder::create($filePath);
|
||||
}
|
||||
|
||||
// setup the file name
|
||||
$fileName = '';
|
||||
|
||||
// Get basic key
|
||||
$basickey = 'Th!s_iS_n0t_sAfe_buT_b3tter_then_n0thiug';
|
||||
if (method_exists('ComponentbuilderHelper', "getCryptKey"))
|
||||
{
|
||||
$basickey = ComponentbuilderHelper::getCryptKey('basic', $basickey);
|
||||
}
|
||||
|
||||
// check the key
|
||||
if (!StringHelper::check($key))
|
||||
{
|
||||
$key = 'vDm';
|
||||
}
|
||||
|
||||
// set the file name
|
||||
if (StringHelper::check($fileType))
|
||||
{
|
||||
// set the name
|
||||
$fileName = trim(md5($type . $target . $basickey . $key) . '.' . trim($fileType, '.'));
|
||||
}
|
||||
else
|
||||
{
|
||||
$fileName = trim(md5($type . $target . $basickey . $key)) . '.txt';
|
||||
}
|
||||
|
||||
// return the url
|
||||
if ('url' === $type)
|
||||
{
|
||||
if (strpos($filePath, JPATH_SITE) !== false)
|
||||
{
|
||||
$filePath = trim( str_replace( JPATH_SITE, '', $filePath), '/');
|
||||
|
||||
return Uri::root() . $filePath . '/' . $fileName;
|
||||
}
|
||||
|
||||
// since the path is behind the root folder of the site, return only the root url (may be used to build the link)
|
||||
return Uri::root();
|
||||
}
|
||||
|
||||
// sanitize the path
|
||||
return '/' . trim( $filePath, '/' ) . '/' . $fileName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user