/**
* we track the creation of htaccess files
*
* @var array
* @since 3.2.0
**/
protected array $htaccess = [];
/**
* Power Build Tracker
*
* @var array
* @since 3.2.0
**/
protected array $done = [];
/**
* Path Build Tracker
*
* @var array
* @since 3.2.0
**/
protected array $path_done = [];
/**
* Power Objects
*
* @var Power
* @since 3.2.0
**/
protected Power $power;
/**
* Compiler Config
*
* @var Config
* @since 3.2.0
*/
protected Config $config;
/**
* The compiler registry
*
* @var Registry
* @since 3.2.0
*/
protected Registry $registry;
/**
* Compiler Event
*
* @var EventInterface
* @since 3.2.0
*/
protected EventInterface $event;
/**
* Compiler Counter
*
* @var Counter
* @since 3.2.0
*/
protected Counter $counter;
/**
* Compiler Utilities Paths
*
* @var Paths
* @since 3.2.0
*/
protected Paths $paths;
/**
* Compiler Utilities Folder
*
* @var Folder
* @since 3.2.0
*/
protected Folder $folder;
/**
* Compiler Utilities File
*
* @var File
* @since 3.2.0
*/
protected File $file;
/**
* Compiler Utilities Files
*
* @var Files
* @since 3.2.0
*/
protected Files $files;
/**
* Database object to query local DB
*
* @var CMSApplication
* @since 3.2.0
**/
protected CMSApplication $app;
/**
* Constructor
*
* @param Power|null $power The power object.
* @param Config|null $config The compiler config object.
* @param Registry|null $registry The compiler registry object.
* @param EventInterface|null $event The compiler event api object.
* @param Counter|null $counter The compiler counter object.
* @param Paths|null $paths The compiler paths object.
* @param Folder|null $folder The compiler folder object.
* @param File|null $file The compiler file object.
* @param Files|null $files The compiler files object.
* @param CMSApplication|null $app The CMS Application object.
*
* @throws \Exception
* @since 3.2.0
*/
public function __construct(?Power $power = null, ?Config $config = null,
?Registry $registry = null, ?EventInterface $event = null,
?Counter $counter = null, ?Paths $paths = null, ?Folder $folder = null,
?File $file = null, ?Files $files = null, ?CMSApplication $app = null)
{
$this->power = $power ?: Compiler::_('Power');
$this->config = $config ?: Compiler::_('Config');
$this->registry = $registry ?: Compiler::_('Registry');
$this->event = $event ?: Compiler::_('Event');
$this->counter = $counter ?: Compiler::_('Utilities.Counter');
$this->paths = $paths ?: Compiler::_('Utilities.Paths');
$this->folder = $folder ?: Compiler::_('Utilities.Folder');
$this->file = $file ?: Compiler::_('Utilities.File');
$this->files = $files ?: Compiler::_('Utilities.Files');
$this->app = $app ?: Factory::getApplication();
}
/**
* Build the Powers files, folders
*
* @return void
* @since 3.2.0
*/
public function build()
{
if (ArrayHelper::check($this->power->active))
{
// Trigger Event: jcb_ce_onBeforeSetModules
$this->event->trigger(
'jcb_ce_onBeforeBuildPowers'
);
// set super power details
$this->setSuperPowerDetails();
foreach ($this->power->active as $guid => $power)
{
if (isset($this->done[$guid]))
{
continue;
}
if (ObjectHelper::check($power)
&& isset($power->path)
&& StringHelper::check(
$power->path
))
{
// activate dynamic folders
$this->setDynamicFolders();
// power path
$power->full_path = $this->paths->component_path . '/'
. $power->path;
$power->full_path_jcb = $this->paths->component_path . '/'
. $power->path_jcb;
$power->full_path_parent = $this->paths->component_path . '/'
. $power->path_parent;
// set the power paths
$this->registry->set('dynamic_paths.' . $power->key, $power->full_path_parent);
// create the power folder if it does not exist
// we do it like this to add html files to each part
$this->folder->create($power->full_path_jcb);
$this->folder->create($power->full_path_parent);
$this->folder->create($power->full_path);
$bom = 'add_licensing_template == 2)
{
$bom = 'licensing_template;
}
// set the main power php file
$this->createFile($bom . PHP_EOL . Placefix::_h('POWERCODE') . PHP_EOL,
$power->full_path, $power->file_name . '.php', $power->key);
// set super power files
$this->setSuperPowerFiles($power, $bom);
// set htaccess once per path
$this->setHtaccess($power);
// do each power just once
$this->done[$guid] = true;
}
}
}
}
/**
* Create a file with optional custom content and save it to the given path.
*
* @param string $content The content.
* @param string $fullPath The full path to the destination folder.
* @param string $fileName The file name without the extension.
* @param string $key The key to append the file details.
*
* @return void
* @since 3.2.0
*/
private function createFile(string $content, string $fullPath, string $fileName, string $key)
{
$file_details = [
'path' => $fullPath . '/' . $fileName,
'name' => $fileName,
'zip' => $fileName
];
// Write the content to the file
$this->file->write($file_details['path'], $content);
// Append the file details to the files array
$this->files->appendArray($key, $file_details);
// Increment the file counter
$this->counter->file++;
}
/**
* Set the .htaccess for this power path
*
* @param object $power The power object
*
* @return void
* @since 3.2.0
*/
private function setHtaccess(object &$power)
{
if (!isset($this->htaccess[$power->path_jcb]))
{
// set the htaccess data
$data = '# Apache 2.4+' . PHP_EOL .
'