Adds the customcode dispencer class
This commit is contained in:
@ -0,0 +1,283 @@
|
||||
<?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 VDM\Joomla\Utilities\StringHelper;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Placeholder;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode\Gui;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode\Hash;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode\LockBase;
|
||||
|
||||
|
||||
/**
|
||||
* Compiler Custom Code Dispenser
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
class Dispenser
|
||||
{
|
||||
/**
|
||||
* Customcode Dispenser Hub
|
||||
*
|
||||
* @var array
|
||||
* @since 3.2.0
|
||||
**/
|
||||
public array $hub;
|
||||
|
||||
/**
|
||||
* Compiler Customcode
|
||||
*
|
||||
* @var Customcode
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Customcode $customcode;
|
||||
|
||||
/**
|
||||
* Compiler Placeholder
|
||||
*
|
||||
* @var Placeholder
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Placeholder $placeholder;
|
||||
|
||||
/**
|
||||
* Compiler Customcode in Gui
|
||||
*
|
||||
* @var Gui
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Gui $gui;
|
||||
|
||||
/**
|
||||
* Compiler Customcode to Hash
|
||||
*
|
||||
* @var Hash
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Hash $hash;
|
||||
|
||||
/**
|
||||
* Compiler Customcode to LockBase
|
||||
*
|
||||
* @var LockBase
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected LockBase $base64;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Customcode|null $customcode The compiler customcode object.
|
||||
* @param Placeholder|null $placeholder The compiler placeholder object.
|
||||
* @param Gui|null $gui The compiler customcode gui object.
|
||||
* @param Hash|null $hash The compiler customcode hash object.
|
||||
* @param LockBase|null $base64 The compiler customcode lock base64 object.
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function __construct(?Placeholder $placeholder = null, ?Customcode $customcode = null,
|
||||
?Gui $gui = null, ?Hash $hash = null, ?LockBase $base64 = null)
|
||||
{
|
||||
$this->placeholder = $placeholder ?: Compiler::_('Placeholder');
|
||||
$this->customcode = $customcode ?: Compiler::_('Customcode');
|
||||
$this->gui = $gui ?: Compiler::_('Customcode.Gui');
|
||||
$this->hash = $hash ?: Compiler::_('Customcode.Hash');
|
||||
$this->base64 = $base64 ?: Compiler::_('Customcode.LockBase');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the script for the customcode dispenser
|
||||
*
|
||||
* @param string $script The script
|
||||
* @param string $first The first key
|
||||
* @param string|null $second The second key (if not set we use only first key)
|
||||
* @param string|null $third The third key (if not set we use only first and second key)
|
||||
* @param array $config The config options
|
||||
* @param bool $base64 The switch to decode base64 the script
|
||||
* default: true
|
||||
* @param bool $dynamic The switch to dynamic update the script
|
||||
* default: true
|
||||
* @param bool $add The switch to add to exiting instead of replace
|
||||
* default: false
|
||||
*
|
||||
* @return bool true on success
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set(&$script, string $first, ?string $second = null, ?string $third = null,
|
||||
array $config = array(), bool $base64 = true, bool $dynamic = true, bool $add = false): bool
|
||||
{
|
||||
// only load if we have a string
|
||||
if (!StringHelper::check($script))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// this needs refactoring (TODO)
|
||||
if (!isset($this->hub[$first])
|
||||
|| ($second
|
||||
&& !isset($this->hub[$first][$second])))
|
||||
{
|
||||
// check if the script first key is set
|
||||
if ($second && !isset($this->hub[$first]))
|
||||
{
|
||||
$this->hub[$first] = array();
|
||||
}
|
||||
elseif ($add && !$second
|
||||
&& !isset($this->hub[$first]))
|
||||
{
|
||||
$this->hub[$first] = '';
|
||||
}
|
||||
// check if the script second key is set
|
||||
if ($second && $third
|
||||
&& !isset($this->hub[$first][$second]))
|
||||
{
|
||||
$this->hub[$first][$second] = array();
|
||||
}
|
||||
elseif ($add && $second && !$third
|
||||
&& !isset($this->hub[$first][$second]))
|
||||
{
|
||||
$this->hub[$first][$second] = '';
|
||||
}
|
||||
// check if the script third key is set
|
||||
if ($add && $second && $third
|
||||
&& !isset($this->hub[$first][$second][$third]))
|
||||
{
|
||||
$this->hub[$first][$second][$third] = '';
|
||||
}
|
||||
}
|
||||
// prep the script string
|
||||
if ($base64 && $dynamic)
|
||||
{
|
||||
$script = $this->customcode->add(base64_decode($script));
|
||||
}
|
||||
elseif ($base64)
|
||||
{
|
||||
$script = base64_decode($script);
|
||||
}
|
||||
elseif ($dynamic) // this does not happen (just incase)
|
||||
{
|
||||
$script = $this->customcode->add($script);
|
||||
}
|
||||
// check if we still have a string
|
||||
if (StringHelper::check($script))
|
||||
{
|
||||
// now load the placeholder snippet if needed
|
||||
if ($base64 || $dynamic)
|
||||
{
|
||||
$script = $this->gui->set($script, $config);
|
||||
}
|
||||
// add Dynamic HASHING option of a file/string
|
||||
$script = $this->hash->set($script);
|
||||
// add base64 locking option of a string
|
||||
$script = $this->base64->set($script);
|
||||
// load the script
|
||||
if ($first && $second && $third)
|
||||
{
|
||||
// now act on loading option
|
||||
if ($add)
|
||||
{
|
||||
$this->hub[$first][$second][$third]
|
||||
.= $script;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->hub[$first][$second][$third]
|
||||
= $script;
|
||||
}
|
||||
}
|
||||
elseif ($first && $second)
|
||||
{
|
||||
// now act on loading option
|
||||
if ($add)
|
||||
{
|
||||
$this->hub[$first][$second] .= $script;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->hub[$first][$second] = $script;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// now act on loading option
|
||||
if ($add)
|
||||
{
|
||||
$this->hub[$first] .= $script;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->hub[$first] = $script;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the script from the customcode dispenser
|
||||
*
|
||||
* @param string $first The first key
|
||||
* @param string $second The second key
|
||||
* @param string $prefix The prefix to add in front of the script if found
|
||||
* @param string|null $note The switch/note to add to the script
|
||||
* @param bool $unset The switch to unset the value if found
|
||||
* @param mixed|null $default The switch/string to use as default return if script not found
|
||||
* @param string $suffix The suffix to add after the script if found
|
||||
*
|
||||
* @return mixed The string/script if found or the default value if not found
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function get(string $first, string $second, string $prefix = '', ?string $note = null,
|
||||
bool $unset = false, $default = null, string $suffix = '')
|
||||
{
|
||||
// default is to return an empty string
|
||||
$script = '';
|
||||
// check if there is any custom script
|
||||
if (isset($this->hub[$first][$second])
|
||||
&& StringHelper::check(
|
||||
$this->hub[$first][$second]
|
||||
))
|
||||
{
|
||||
// add not if set
|
||||
if ($note)
|
||||
{
|
||||
$script .= $note;
|
||||
}
|
||||
// load the actual script
|
||||
$script .= $prefix . str_replace(
|
||||
array_keys($this->placeholder->active),
|
||||
array_values($this->placeholder->active),
|
||||
$this->hub[$first][$second]
|
||||
) . $suffix;
|
||||
// clear some memory
|
||||
if ($unset)
|
||||
{
|
||||
unset($this->hub[$first][$second]);
|
||||
}
|
||||
}
|
||||
// if not found return default
|
||||
if (!StringHelper::check($script) && $default)
|
||||
{
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,110 @@
|
||||
<?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 VDM\Joomla\Utilities\GetHelper;
|
||||
use VDM\Joomla\Utilities\FileHelper;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Placeholder;
|
||||
|
||||
|
||||
/**
|
||||
* Compiler Custom Code MD5
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
class Hash
|
||||
{
|
||||
/**
|
||||
* Compiler Placeholder
|
||||
*
|
||||
* @var Placeholder
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Placeholder $placeholder;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Placeholder|null $placeholder The compiler placeholder object.
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function __construct(?Placeholder $placeholder = null)
|
||||
{
|
||||
$this->placeholder = $placeholder ?: Compiler::_('Placeholder');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the the MD5 hashed string or file or string
|
||||
*
|
||||
* @param string $script The code string
|
||||
*
|
||||
* @return string
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set(string $script): string
|
||||
{
|
||||
// check if we should hash a string
|
||||
if (\strpos($script, 'HASH' . 'STRING((((') !== false)
|
||||
{
|
||||
// get the strings
|
||||
$values = GetHelper::allBetween(
|
||||
$script, 'HASH' . 'STRING((((', '))))'
|
||||
);
|
||||
$locker = array();
|
||||
// convert them
|
||||
foreach ($values as $value)
|
||||
{
|
||||
$locker['HASH' . 'STRING((((' . $value . '))))']
|
||||
= \md5($value);
|
||||
}
|
||||
|
||||
// update the script
|
||||
return $this->placeholder->update($script, $locker);
|
||||
}
|
||||
// check if we should hash a file
|
||||
if (\strpos($script, 'HASH' . 'FILE((((') !== false)
|
||||
{
|
||||
// get the strings
|
||||
$values = GetHelper::allBetween(
|
||||
$script, 'HASH' . 'FILE((((', '))))'
|
||||
);
|
||||
$locker = array();
|
||||
// convert them
|
||||
foreach ($values as $path)
|
||||
{
|
||||
// we first get the file if it exist
|
||||
if ($value = FileHelper::getContent($path))
|
||||
{
|
||||
// now we hash the file content
|
||||
$locker['HASH' . 'FILE((((' . $path . '))))']
|
||||
= \md5($value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// could not retrieve the file so we show error
|
||||
$locker['HASH' . 'FILE((((' . $path . '))))']
|
||||
= 'ERROR';
|
||||
}
|
||||
}
|
||||
|
||||
// update the script
|
||||
return $this->placeholder->update($script, $locker);
|
||||
}
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,85 @@
|
||||
<?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 VDM\Joomla\Utilities\GetHelper;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Utilities\Indent;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Placeholder;
|
||||
|
||||
|
||||
/**
|
||||
* Compiler Custom Code Base64
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
class LockBase
|
||||
{
|
||||
/**
|
||||
* Compiler Placeholder
|
||||
*
|
||||
* @var Placeholder
|
||||
* @since 3.2.0
|
||||
**/
|
||||
protected Placeholder $placeholder;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Placeholder|null $placeholder The compiler placeholder object.
|
||||
*
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function __construct(?Placeholder $placeholder = null)
|
||||
{
|
||||
$this->placeholder = $placeholder ?: Compiler::_('Placeholder');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a string as bsae64 (basic)
|
||||
*
|
||||
* @param string $script The code string
|
||||
*
|
||||
* @return string
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function set(string $script): string
|
||||
{
|
||||
if (\strpos($script, 'LOCK'.'BASE64((((') !== false)
|
||||
{
|
||||
// get the strings
|
||||
$values = GetHelper::allBetween(
|
||||
$script, 'LOCK'.'BASE64((((', '))))'
|
||||
);
|
||||
$locker = array();
|
||||
// convert them
|
||||
foreach ($values as $value)
|
||||
{
|
||||
$locker['LOCK'.'BASE64((((' . $value . '))))']
|
||||
= "base64_decode( preg_replace('/\s+/', ''," .
|
||||
PHP_EOL . Indent::_(2) . "'" .
|
||||
\wordwrap(
|
||||
\base64_encode($value), 64, PHP_EOL . Indent::_(2), true
|
||||
) .
|
||||
"'))";
|
||||
}
|
||||
|
||||
// update the script
|
||||
return $this->placeholder->update($script, $locker);
|
||||
}
|
||||
|
||||
return $script;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,6 +17,9 @@ use Joomla\DI\ServiceProviderInterface;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode as CompilerCustomcode;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode\External;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode\Gui;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode\Hash;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode\LockBase;
|
||||
use VDM\Joomla\Componentbuilder\Compiler\Customcode\Dispenser;
|
||||
|
||||
|
||||
/**
|
||||
@ -44,6 +47,15 @@ class Customcode implements ServiceProviderInterface
|
||||
|
||||
$container->alias(Gui::class, 'Customcode.Gui')
|
||||
->share('Customcode.Gui', [$this, 'getGui'], true);
|
||||
|
||||
$container->alias(Hash::class, 'Customcode.Hash')
|
||||
->share('Customcode.Hash', [$this, 'getHash'], true);
|
||||
|
||||
$container->alias(LockBase::class, 'Customcode.LockBase')
|
||||
->share('Customcode.LockBase', [$this, 'getLockBase'], true);
|
||||
|
||||
$container->alias(Dispenser::class, 'Customcode.Dispenser')
|
||||
->share('Customcode.Dispenser', [$this, 'getDispenser'], true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,6 +106,55 @@ class Customcode implements ServiceProviderInterface
|
||||
$container->get('Placeholder.Reverse')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Customcode Hash
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return Hash
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function getHash(Container $container): Hash
|
||||
{
|
||||
return new Hash(
|
||||
$container->get('Placeholder')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Customcode LockBase64
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return LockBase
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function getLockBase(Container $container): LockBase
|
||||
{
|
||||
return new LockBase(
|
||||
$container->get('Placeholder')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Customcode Dispenser
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return Dispenser
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public function getDispenser(Container $container): Dispenser
|
||||
{
|
||||
return new Dispenser(
|
||||
$container->get('Placeholder'),
|
||||
$container->get('Customcode'),
|
||||
$container->get('Customcode.Gui'),
|
||||
$container->get('Customcode.Hash'),
|
||||
$container->get('Customcode.LockBase')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user