jcb-compiler/src/6bbb6ffe-3f09-4c21-aa9d-c93.../code.power

145 lines
2.9 KiB
Plaintext

/**
* Compiler Counter
*
* @var Counter
* @since 3.2.0
*/
protected Counter $counter;
/**
* Compiler Utilities File
*
* @var File
* @since 3.2.0
*/
protected File $file;
/**
* Constructor
*
* @param Counter|null $counter The compiler counter object.
* @param File|null $file The compiler file object.
*
* @since 3.2.0
*/
public function __construct(?Counter $counter = null, ?File $file = null)
{
$this->counter = $counter ?: Compiler::_('Utilities.Counter');
$this->file = $file ?: Compiler::_('Utilities.File');
}
/**
* Create Path if not exist
*
* @param string $path The path to folder to create
* @param bool $addHtml The the switch to add the HTML
*
* @return void
* @since 3.2.0
*/
public function create(string $path, bool $addHtml = true)
{
// check if the path exist
if (!JoomlaFolder::exists($path))
{
// create the path
JoomlaFolder::create(
$path
);
// count the folder created
$this->counter->folder++;
if ($addHtml)
{
// add index.html (boring I know)
$this->file->html(
$path, ''
);
}
}
}
/**
* Remove folders with files
*
* @param string $path The path to folder to remove
* @param array|null $ignore The folders and files to ignore and not remove
*
* @return boolean True if all are removed
* @since 3.2.0
*/
public function remove(string $path, ?array $ignore = null): bool
{
if (JoomlaFolder::exists($path))
{
$it = new \RecursiveDirectoryIterator($path);
$it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
// remove ending /
$path = rtrim($path, '/');
// now loop the files & folders
foreach ($it as $file)
{
if ('.' === $file->getBasename() || '..' === $file->getBasename()) continue;
// set file dir
$file_dir = $file->getPathname();
// check if this is a dir or a file
if ($file->isDir())
{
$keeper = false;
if (ArrayHelper::check($ignore))
{
foreach ($ignore as $keep)
{
if (strpos((string) $file_dir, $path . '/' . $keep) !== false)
{
$keeper = true;
}
}
}
if ($keeper)
{
continue;
}
JoomlaFolder::delete($file_dir);
}
else
{
$keeper = false;
if (ArrayHelper::check($ignore))
{
foreach ($ignore as $keep)
{
if (strpos((string) $file_dir, $path . '/'. $keep) !== false)
{
$keeper = true;
}
}
}
if ($keeper)
{
continue;
}
JoomlaFile::delete($file_dir);
}
}
// delete the root folder if ignore not set
if (!ArrayHelper::check($ignore))
{
return JoomlaFolder::delete($path);
}
return true;
}
return false;
}