Moves multiple class methods to their own power classes. Moves many compiler config values to its own config class. Updated the Expantion method to use the new config class.

This commit is contained in:
2022-08-30 17:28:41 +02:00
parent f8ac247377
commit 4928a8baaf
58 changed files with 14568 additions and 12580 deletions

View File

@@ -0,0 +1,406 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Componentbuilder\Compiler\Customcode;
use Joomla\CMS\Factory;
use Joomla\CMS\User\User;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Filesystem\Path;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Utilities\GetHelper;
use VDM\Joomla\Utilities\FileHelper;
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
use VDM\Joomla\Componentbuilder\Compiler\Placeholder;
/**
* Compiler External Custom Code
*
* @since 3.2.0
*/
class External
{
/**
* The external code/string to be added
*
* @var array
* @since 3.2.0
*/
protected array $code = [];
/**
* The external code/string cutter
*
* @var array
* @since 3.2.0
*/
protected array $cutter = [];
/**
* Compiler Placeholder
*
* @var Placeholder
* @since 3.2.0
**/
protected Placeholder $placeholder;
/**
* Database object to query local DB
*
* @var \JDatabaseDriver
* @since 3.2.0
**/
protected \JDatabaseDriver $db;
/**
* User object
*
* @var User
* @since 3.2.0
**/
protected User $user;
/**
* Database object to query local DB
*
* @var CMSApplication
* @since 3.2.0
**/
protected CMSApplication $app;
/**
* Constructor.
*
* @param Placeholder|null $placeholder The compiler placeholder object.
* @param \JDatabaseDriver|null $db The Database Driver object.
* @param User|null $user The User object.
* @param CMSApplication|null $app The CMS Application object.
*
* @throws \Exception
* @since 3.2.0
*/
public function __construct(?Placeholder $placeholder = null,
?\JDatabaseDriver $db = null, ?User $user = null, ?CMSApplication $app = null)
{
$this->placeholder = $placeholder ?: Compiler::_('Placeholder');
$this->db = $db ?: Factory::getDbo();
$this->user = $user ?: Factory::getUser();
$this->app = $app ?: Factory::getApplication();
}
/**
* Set the external code string & load it in to string
*
* @param string $string The content to check
* @param int $debug The switch to debug the update
*
* @return string
* @since 3.2.0
*/
public function set(string $string, int $debug = 0): string
{
// check if content has custom code placeholder
if (strpos($string, '[EXTERNA' . 'LCODE=') !== false)
{
// if debug
if ($debug)
{
echo 'External Code String:';
var_dump($string);
}
// target content
$bucket = array();
$found = GetHelper::allBetween(
$string, '[EXTERNA' . 'LCODE=', ']'
);
if (ArrayHelper::check($found))
{
// build local bucket
foreach ($found as $target)
{
// check for cutting sequence
// example: >{3|4
// will cut 3 rows at top and 4 rows at bottom
// if the external code has 8 or more lines
if (($pos = strpos($target, '>{')) !== false)
{
// the length
$target_len = strlen($target);
// where to cut
$cutting = $target_len - $pos;
// get the sequence
$sequence = substr($target, "-$cutting");
// remove from the URL
$target_url = str_replace($sequence, '', $target);
// set the cut key for this target if not set
$this->cutter[trim($target)] = str_replace('>{', '', $sequence);
}
else
{
$target_url = $target;
}
// check if the target is valid URL or path
if ((!filter_var($target_url, FILTER_VALIDATE_URL) === false
&& FileHelper::exists($target_url))
|| (Path::clean($target_url) === $target_url
&& FileHelper::exists($target_url)))
{
$this->getCode($target, $bucket);
}
// give notice that target is not a valid url/path
else
{
// set key
$key = '[EXTERNA' . 'LCODE=' . $target . ']';
// set the notice
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_HR_HTHREEEXTERNAL_CODE_WARNINGHTHREE'
), 'Warning'
);
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_THE_BSB_IS_NOT_A_VALID_URLPATH',
$key
), 'Warning'
);
// remove the placeholder
$bucket[$key] = '';
}
}
// now update local string if bucket has values
if (ArrayHelper::check($bucket))
{
$string = $this->placeholder->update($string, $bucket);
}
}
// if debug
if ($debug)
{
echo 'External Code String After Update:';
var_dump($string);
}
}
return $string;
}
/**
* Get the External Code/String
*
* @param string $string The content to check
* @param array $bucket The Placeholders bucket
*
* @return void
* @since 3.2.0
*/
protected function getCode(string $target, array &$bucket)
{
// set URL key
$target_key = trim($target);
// set key
$key = '[EXTERNA' . 'LCODE=' . $target . ']';
// remove the cut sequence from the url
if (isset($this->cutter[$target_key]))
{
// remove from the URL
$target_url = trim(str_replace('>{' . $this->cutter[$target_key], '', $target));
}
else
{
$target_url = trim($target);
}
// check if we already fetched this
if (!isset($this->code[$target_key]))
{
// get the data string (code)
$this->code[$target_key]
= FileHelper::getContent($target_url);
// check if we must cut this
if (isset($this->cutter[$target_key]) &&
$this->cutter[$target_key])
{
$this->code[$target_key] = $this->cut(
$this->code[$target_key],
$this->cutter[$target_key],
$key
);
}
// did we get any value
if (StringHelper::check(
$this->code[$target_key]
))
{
// check for changes
$live_hash = md5($this->code[$target_key]);
// check if it exists local
if ($hash = GetHelper::var(
'external_code', $target_key, 'target', 'hash'
))
{
// must be an admin make a change to use EXTERNAL code (we may add a custom access switch - use ADMIN for now)
if ($hash !== $live_hash && $this->user->authorise(
'core.admin', 'com_componentbuilder'
))
{
// update the hash since it changed
$object = new stdClass();
$object->target = $target_key;
$object->hash = $live_hash;
// update local hash
$this->db->updateObject(
'#__componentbuilder_external_code', $object,
'target'
);
// give notice of the change
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_HR_HTHREEEXTERNAL_CODE_WARNINGHTHREE'),
'Warning'
);
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_THE_CODESTRING_FROM_BSB_HAS_BEEN_BCHANGEDB_SINCE_THE_LAST_COMPILATION_PLEASE_INVESTIGATE_TO_ENSURE_THE_CHANGES_ARE_SAFE_BSHOULD_YOU_NOT_EXPECT_THIS_CHANGE_TO_THE_EXTERNAL_CODESTRING_BEING_ADDED_THEN_THIS_IS_A_SERIOUS_ISSUE_AND_REQUIRES_IMMEDIATE_ATTENTIONB_DO_NOT_IGNORE_THIS_WARNING_AS_IT_WILL_ONLY_SHOW_BONCEB',
$key
), 'Warning'
);
}
elseif ($hash !== $live_hash)
{
// set the notice
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_HR_HTHREEEXTERNAL_CODE_ERRORHTHREE'),
'Error'
);
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_S_WE_DETECTED_A_CHANGE_IN_BEXTERNALCODEB_BUT_YOU_DO_NOT_HAVE_PERMISSION_TO_ALLOW_THIS_CHANGE_SO_BSB_WAS_REMOVED_FROM_THE_COMPILATION_PLEASE_CONTACT_YOUR_SYSTEM_ADMINISTRATOR_FOR_MORE_INFOBR_SMALLADMIN_ACCESS_REQUIREDSMALL',
$this->user->get('name'), $key
), 'Error'
);
// remove the code/string
$this->code[$target_key] = '';
}
}
// only an admin can add new EXTERNAL code (we may add a custom access switch - use ADMIN for now)
elseif ($this->user->authorise(
'core.admin', 'com_componentbuilder'
))
{
// add the hash to track changes
$object = new stdClass();
$object->target = $target_key;
$object->hash = $live_hash;
// insert local hash
$this->db->insertObject(
'#__componentbuilder_external_code', $object
);
// give notice the first time this is added
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_HR_HTHREEEXTERNAL_CODE_NOTICEHTHREE'),
'Warning'
);
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_THE_CODESTRING_FROM_BSB_HAS_BEEN_ADDED_FOR_THE_BFIRST_TIMEB_PLEASE_IINVESTIGATEI_TO_ENSURE_THE_CORRECT_CODESTRING_WAS_USED_BSHOULD_YOU_NOT_KNOW_ABOUT_THIS_NEW_EXTERNAL_CODESTRING_BEING_ADDED_THEN_THIS_IS_A_SERIOUS_DANGER_AND_REQUIRES_IMMEDIATE_ATTENTIONB_DO_NOT_IGNORE_THIS_WARNING_AS_IT_WILL_ONLY_SHOW_BONCEB',
$key
), 'Warning'
);
}
else
{
// set the notice
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_HR_HTHREEEXTERNAL_CODE_ERRORHTHREE'),
'Error'
);
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_S_WE_DETECTED_BNEW_EXTERNALCODEB_BUT_YOU_DO_NOT_HAVE_PERMISSION_TO_ALLOW_THIS_NEW_CODESTRING_SO_BSB_WAS_REMOVED_FROM_THE_COMPILATION_PLEASE_CONTACT_YOU_SYSTEM_ADMINISTRATOR_FOR_MORE_INFOBR_SMALLADMIN_ACCESS_REQUIREDSMALL',
$this->user->get('name'), $key
), 'Error'
);
// remove the code/string
$this->code[$target_key] = '';
}
}
else
{
// set notice that we could not get a valid string from the target
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_HR_HTHREEEXTERNAL_CODE_WARNINGHTHREE'), 'Error'
);
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_THE_BSB_RETURNED_AN_INVALID_STRING', $key
), 'Error'
);
}
}
// add to local bucket
if (isset($this->code[$target_key]))
{
// update the placeholder with the external code string
$bucket[$key] = $this->code[$target_key];
}
else
{
// remove the placeholder
$bucket[$key] = '';
}
}
/**
* Cut the External Code/String
*
* @param string $string The content to cut
* @param string $sequence The cutting sequence
* @param string $key The content key
*
* @return string
* @since 3.2.0
*/
protected function cut(string $string, string $sequence, string $key): string
{
// we first break the string up in rows
$rows = (array) explode(PHP_EOL, $string);
// get the cutting sequence
$cutter = (array) explode('|', $sequence);
// we only continue if we have more rows than we have to cut
if (array_sum($cutter) < ArrayHelper::check($rows))
{
// remove the rows at the bottom if needed
if (isset($cutter[1]) && $cutter[1] > 0)
{
array_splice($rows, "-$cutter[1]");
}
// remove the rows at the top if needed
if ($cutter[0] > 0)
{
$rows = array_splice($rows, $cutter[0]);
}
// return the remaining rows
return implode(PHP_EOL, $rows);
}
// we set an error message about too few lines to cut
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_HR_HTHREEEXTERNAL_CODE_NOTICEHTHREE'),
'Error'
);
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_THE_BSB_CUT_SEQUENCE_FAILED_ON_THE_RETURNED_EXTERNAL_CODESTRING_AS_MORE_LINES_HAS_TO_BE_CUT_THEN_WAS_FOUND_IN_THE_CODESTRING_WE_HAVE_COMPLETELY_REMOVED_THE_CODE_PLEASE_CHECK_THIS_CODESTRING',
$key
), 'Error'
);
return '';
}
}

View File

@@ -0,0 +1,256 @@
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\Componentbuilder\Compiler\Customcode;
use Joomla\CMS\Factory;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Language\Text;
use VDM\Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Utilities\StringHelper;
use VDM\Joomla\Utilities\GetHelper;
use VDM\Joomla\Utilities\FileHelper;
use VDM\Joomla\Utilities\String\FieldHelper;
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
use VDM\Joomla\Componentbuilder\Compiler\Config;
use VDM\Joomla\Componentbuilder\Compiler\Placeholder\Reverse;
/**
* Compiler Gui Custom Code
*
* @since 3.2.0
*/
class Gui
{
/**
* Compiler Config
*
* @var Config
* @since 3.2.0
**/
protected Config $config;
/**
* Compiler Placeholder Reverse
*
* @var Reverse
* @since 3.2.0
**/
protected Reverse $reverse;
/**
* Database object to query local DB
*
* @var \JDatabaseDriver
* @since 3.2.0
**/
protected \JDatabaseDriver $db;
/**
* Database object to query local DB
*
* @var CMSApplication
* @since 3.2.0
**/
protected CMSApplication $app;
/**
* Constructor.
*
* @param Config|null $config The compiler config object.
* @param Reverse|null $reverse The compiler placeholder reverse object.
* @param \JDatabaseDriver|null $db The Database Driver object.
* @param CMSApplication|null $app The CMS Application object.
*
* @throws \Exception
* @since 3.2.0
*/
public function __construct(?Config $config = null, ?Reverse $reverse = null,
?\JDatabaseDriver $db = null, ?CMSApplication $app = null)
{
$this->config = $config ?: Compiler::_('Config');
$this->reverse = $reverse ?: Compiler::_('Placeholder.Reverse');
$this->db = $db ?: Factory::getDbo();
$this->app = $app ?: Factory::getApplication();
}
/**
* Set the JCB GUI code placeholder
*
* @param string $string The code string
* @param array $config The placeholder config values
*
* @return string
* @since 3.2.0
*/
public function set(string $string, array $config): string
{
if (StringHelper::check($string))
{
if ($this->config->get('add_placeholders', false)
&& $this->check($string) && ArrayHelper::check($config)
&& isset($config['table']) && StringHelper::check($config['table'])
&& isset($config['field']) && StringHelper::check($config['field'])
&& isset($config['type']) && StringHelper::check($config['type'])
&& isset($config['id']) && is_numeric($config['id']))
{
// if we have a key we must get the ID
if (isset($config['key']) && StringHelper::check($config['key']) && $config['key'] !== 'id')
{
if (($id = GetHelper::var($config['table'], $config['id'], $config['key'], 'id')) !== false && is_numeric($id))
{
$config['id'] = $id;
}
else
{
// we must give a error message to inform the user of this issue. (should never happen)
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_ID_MISMATCH_WAS_DETECTED_WITH_THE_SSSS_GUI_CODE_FIELD_SO_THE_PLACEHOLDER_WAS_NOT_SET',
$config['table'], $config['field'],
$config['key'], $config['id']
), 'Error'
);
// check some config
if (!isset($config['prefix']))
{
$config['prefix'] = '';
}
return $config['prefix'] . $string;
}
}
// check some config
if (!isset($config['prefix']))
{
$config['prefix'] = PHP_EOL;
}
// add placeholder based on type of code
switch (strtolower($config['type']))
{
// adding with html commenting
case 'html':
$front = $config['prefix'] . '<!--' . '[JCBGUI.';
$sufix = '$$$$]-->' . PHP_EOL;
$back = '<!--[/JCBGUI' . $sufix;
break;
// adding with php commenting
default:
$front = $config['prefix'] . '/***' . '[JCBGUI.';
$sufix = '$$$$]***/' . PHP_EOL;
$back = '/***[/JCBGUI' . $sufix;
break;
}
return $front . $config['table'] . '.' . $config['field'] . '.'
. $config['id'] . '.' . $sufix . $string . $back;
}
// check some config
if (!isset($config['prefix']))
{
$config['prefix'] = '';
}
return $config['prefix'] . $string;
}
return $string;
}
/**
* search a file for gui code blocks that were updated in the IDE
*
* @param string $file The file path to search
* @param array $placeholders The values to replace in the code being stored
* @param string $today The date for today
* @param string $target The target path type
*
* @return void
* @since 3.2.0
*/
public function search(string &$file, array &$placeholders, string &$today, string &$target)
{
// get file content
$file_conent = FileHelper::getContent($file);
$guiCode = array();
// we add a new search for the GUI CODE Blocks
$guiCode[] = GetHelper::allBetween(
$file_conent, '/***[JCB' . 'GUI<>', '/***[/JCBGUI' . '$$$$]***/'
);
$guiCode[] = GetHelper::allBetween(
$file_conent, '<!--[JCB' . 'GUI<>', '<!--[/JCBGUI' . '$$$$]-->'
);
if (($guiCode = ArrayHelper::merge($guiCode)) !== false
&& ArrayHelper::check($guiCode, true))
{
foreach ($guiCode as $code)
{
$first_line = strtok($code, PHP_EOL);
// get the GUI target details
$query = explode('.', trim($first_line, '.'));
// only continue if we have 3 values in the query
if (is_array($query) && count($query) >= 3)
{
// cleanup the newlines around the code
$code = trim(str_replace($first_line, '', $code), PHP_EOL)
. PHP_EOL;
// set the ID
$id = (int) $query[2];
// make the field name save
$field = FieldHelper::safe($query[1]);
// make the table name save
$table = StringHelper::safe($query[0]);
// reverse placeholder as much as we can
$code = $this->reverse->engine(
$code, $placeholders, $target, $id, $field, $table
);
// update the GUI/Tables/Database
$object = new \stdClass();
$object->id = $id;
$object->{$field} = base64_encode(
$code
); // (TODO) this may not always work...
// update the value in GUI
$this->db->updateObject(
'#__componentbuilder_' . (string) $table, $object, 'id'
);
}
}
}
}
/**
* search a code to see if there is already any custom
* code or other reasons not to add the GUI code placeholders
*
* @param string $code The code to check
*
* @return bool true if GUI code placeholders can be added
* @since 3.2.0
*/
protected function check(string &$code): bool
{
// check for customcode placeholders
if (strpos($code, '$$$$') !== false)
{
// we do not add GUI wrapper placeholder to code
// that already has any customcode placeholders
return false;
}
return true;
}
}

View File

@@ -0,0 +1 @@
<html><body bgcolor="#FFFFFF"></body></html>