From d28e4c74af041caa7ce2b6b9ff89d78514048cba Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Sun, 18 Dec 2022 10:16:43 +0200 Subject: [PATCH] Adds power infusion class. --- README.md | 6 +- admin/README.txt | 6 +- admin/helpers/compiler/f_Infusion.php | 33 +-- componentbuilder.xml | 2 +- .../Compiler/Power/Autoloader.php | 12 +- .../Compiler/Power/Infusion.php | 207 ++++++++++++++++++ .../Compiler/Service/Power.php | 24 ++ 7 files changed, 252 insertions(+), 38 deletions(-) create mode 100644 libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Power/Infusion.php diff --git a/README.md b/README.md index b0dff3c68..8916b273a 100644 --- a/README.md +++ b/README.md @@ -140,13 +140,13 @@ TODO + *Author*: [Llewellyn van der Merwe](mailto:joomla@vdm.io) + *Name*: [Component Builder](https://git.vdm.dev/joomla/Component-Builder) + *First Build*: 30th April, 2015 -+ *Last Build*: 11th December, 2022 ++ *Last Build*: 18th December, 2022 + *Version*: 3.1.13 + *Copyright*: Copyright (C) 2015 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt -+ *Line count*: **337314** ++ *Line count*: **337557** + *Field count*: **2009** -+ *File count*: **2206** ++ *File count*: **2207** + *Folder count*: **388** > This **component** was build with a [Joomla](https://extensions.joomla.org/extension/component-builder/) [Automated Component Builder](https://www.joomlacomponentbuilder.com). diff --git a/admin/README.txt b/admin/README.txt index b0dff3c68..8916b273a 100644 --- a/admin/README.txt +++ b/admin/README.txt @@ -140,13 +140,13 @@ TODO + *Author*: [Llewellyn van der Merwe](mailto:joomla@vdm.io) + *Name*: [Component Builder](https://git.vdm.dev/joomla/Component-Builder) + *First Build*: 30th April, 2015 -+ *Last Build*: 11th December, 2022 ++ *Last Build*: 18th December, 2022 + *Version*: 3.1.13 + *Copyright*: Copyright (C) 2015 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt -+ *Line count*: **337314** ++ *Line count*: **337557** + *Field count*: **2009** -+ *File count*: **2206** ++ *File count*: **2207** + *Folder count*: **388** > This **component** was build with a [Joomla](https://extensions.joomla.org/extension/component-builder/) [Automated Component Builder](https://www.joomlacomponentbuilder.com). diff --git a/admin/helpers/compiler/f_Infusion.php b/admin/helpers/compiler/f_Infusion.php index b709e5f10..6eb6c123d 100644 --- a/admin/helpers/compiler/f_Infusion.php +++ b/admin/helpers/compiler/f_Infusion.php @@ -1802,37 +1802,10 @@ class Infusion extends Interpretation { CFactory::_('Content')->set('README', $this->componentData->readme); } - // remove all the power placeholders - CFactory::_('Content')->set('ADMIN_POWER_HELPER', ''); - CFactory::_('Content')->set('SITE_POWER_HELPER', ''); - CFactory::_('Content')->set('CUSTOM_POWER_AUTOLOADER', ''); - // infuse powers data if set - if (ArrayHelper::check(CFactory::_('Power')->active)) - { - // start the autoloader - $autoloader = array(); - foreach (CFactory::_('Power')->active as $power) - { - if (ObjectHelper::check($power)) - { - // Trigger Event: jcb_ce_onBeforeInfusePowerData - CFactory::_('Event')->trigger( - 'jcb_ce_onBeforeInfusePowerData', - array(&$this->componentContext, &$power, &$this) - ); - // POWERCODE - CFactory::_('Content')->set_($power->key, 'POWERCODE', $this->getPowerCode($power)); - // Trigger Event: jcb_ce_onAfterInfusePowerData - CFactory::_('Event')->trigger( - 'jcb_ce_onAfterInfusePowerData', - array(&$this->componentContext, &$power, &$this) - ); - } - } - // now set the power autoloader - CFactory::_('Power.Autoloader')->set((!CFactory::_('Config')->remove_site_folder || !CFactory::_('Config')->remove_site_edit_folder)); - } + // Infuse POWERS + CFactory::_('Power.Infusion')->set(); + // tweak system to set stuff to the module domain $_backup_target = CFactory::_('Config')->build_target; $_backup_lang = CFactory::_('Config')->lang_target; diff --git a/componentbuilder.xml b/componentbuilder.xml index dd7c7fece..fcbdf9929 100644 --- a/componentbuilder.xml +++ b/componentbuilder.xml @@ -1,7 +1,7 @@ COM_COMPONENTBUILDER - 11th December, 2022 + 18th December, 2022 Llewellyn van der Merwe joomla@vdm.io https://dev.vdm.io diff --git a/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Power/Autoloader.php b/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Power/Autoloader.php index b496a2006..95a43d324 100644 --- a/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Power/Autoloader.php +++ b/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Power/Autoloader.php @@ -84,12 +84,15 @@ class Autoloader if (($size = ArrayHelper::check($this->power->namespace)) > 0) { // set if we should load the autoloader on the site area - $loadSite = true; + $loadSite = (!$this->config->remove_site_folder || !$this->config->remove_site_edit_folder); + // check if we are using a plugin $use_plugin = $this->content->exist('PLUGIN_POWER_AUTOLOADER'); + // build the methods $autoloadNotSiteMethod = array(); $autoloadMethod = array(); + // add only if we are not using a plugin $tab_space = 2; if (!$use_plugin) @@ -107,6 +110,7 @@ class Autoloader $autoloadNotSiteMethod[] = Indent::_(3) . 'return;'; $autoloadNotSiteMethod[] = Indent::_(2) . '}' . PHP_EOL; } + // we start building the spl_autoload_register function call $autoloadMethod[] = Indent::_($tab_space) . '//' . Line::_(__Line__, __Class__) . ' register this component namespace'; @@ -114,6 +118,7 @@ class Autoloader $autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '//' . Line::_(__Line__, __Class__) . ' project-specific base directories and namespace prefix'; $autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '$search = array('; + // ==== IMPORTANT NOTICE ===== // make sure the name space values are sorted from the longest string to the shortest // so that the search do not mistakenly match a shorter namespace before a longer one @@ -125,9 +130,11 @@ class Autoloader // ^^^^^^^^^^^^^^^^^^^^^ // NameSpace\SubName\SubSubName\ClassName // ^^^^^^^^^^^^^^^^^^^^^ + uksort($this->power->namespace, function ($a, $b) { return strlen($b) - strlen($a); }); + // counter to manage the comma in the actual array $counter = 1; foreach ($this->power->namespace as $base_dir => $prefix) @@ -188,8 +195,10 @@ class Autoloader $autoloadMethod[] = Indent::_($tab_space) . Indent::_(2) . 'require $file;'; $autoloadMethod[] = Indent::_($tab_space) . Indent::_(1) . '}'; $autoloadMethod[] = Indent::_($tab_space) . '});'; + // create the method string $autoloader = implode(PHP_EOL, $autoloadNotSiteMethod) . implode(PHP_EOL, $autoloadMethod); + // check if we are using a plugin if ($use_plugin) { @@ -205,6 +214,7 @@ class Autoloader $this->content->add('SITE_POWER_HELPER', $autoloader); } } + // to add to custom files $this->content->add('CUSTOM_POWER_AUTOLOADER', PHP_EOL . implode(PHP_EOL, $autoloadMethod)); } diff --git a/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Power/Infusion.php b/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Power/Infusion.php new file mode 100644 index 000000000..f7d1157fc --- /dev/null +++ b/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Power/Infusion.php @@ -0,0 +1,207 @@ + + * @git 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\Power; + + +use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler; +use VDM\Joomla\Componentbuilder\Compiler\Config; +use VDM\Joomla\Componentbuilder\Compiler\Power; +use VDM\Joomla\Componentbuilder\Compiler\Content; +use VDM\Joomla\Componentbuilder\Compiler\Power\Autoloader; +use VDM\Joomla\Componentbuilder\Compiler\Placeholder; +use VDM\Joomla\Componentbuilder\Compiler\Interfaces\EventInterface as Event; +use VDM\Joomla\Utilities\StringHelper; +use VDM\Joomla\Utilities\ArrayHelper; +use VDM\Joomla\Utilities\ObjectHelper; + + +/** + * Compiler Power Infusion + * @since 3.2.0 + */ +class Infusion +{ + /** + * Compiler Config + * + * @var Config + * @since 3.2.0 + **/ + protected Config $config; + + /** + * Power Objects + * + * @var Power + * @since 3.2.0 + **/ + protected Power $power; + + /** + * Compiler Content + * + * @var Content + * @since 3.2.0 + **/ + protected Content $content; + + /** + * Compiler Powers Autoloader + * + * @var Autoloader + * @since 3.2.0 + **/ + protected Autoloader $autoloader; + + /** + * Compiler Placeholder + * + * @var Placeholder + * @since 3.2.0 + **/ + protected Placeholder $placeholder; + + /** + * Compiler Event + * + * @var Event + * @since 3.2.0 + **/ + protected Event $event; + + /** + * Constructor. + * + * @param Config|null $config The Config object. + * @param Power|null $power The power object. + * @param Content|null $content The compiler content object. + * @param Autoloader|null $autoloader The powers autoloader object. + * @param Placeholder|null $placeholder The placeholder object. + * @param Event|null $event The events object. + * + * @since 3.2.0 + */ + public function __construct(?Config $config = null, ?Power $power = null, ?Content $content = null, + ?Autoloader $autoloader = null, ?Placeholder $placeholder = null, ?Event $event = null) + { + $this->config = $config ?: Compiler::_('Config'); + $this->power = $power ?: Compiler::_('Power'); + $this->content = $content ?: Compiler::_('Content'); + $this->autoloader = $autoloader ?: Compiler::_('Power.Autoloader'); + $this->placeholder = $placeholder ?: Compiler::_('Placeholder'); + $this->event = $event ?: Compiler::_('Event'); + } + + /** + * Infuse the powers data with the content + * + * @return void + * @since 3.2.0 + */ + public function set() + { + // infuse powers data if set + if (ArrayHelper::check($this->power->active)) + { + // TODO we need to update the event signatures + $context = $this->config->component_context; + + foreach ($this->power->active as $power) + { + if (ObjectHelper::check($power)) + { + // Trigger Event: jcb_ce_onBeforeInfusePowerData + $this->event->trigger( + 'jcb_ce_onBeforeInfusePowerData', + array(&$context, &$power) + ); + + // POWERCODE + $this->content->set_($power->key, 'POWERCODE', $this->get($power)); + + // Trigger Event: jcb_ce_onAfterInfusePowerData + $this->event->trigger( + 'jcb_ce_onAfterInfusePowerData', + array(&$context, &$power) + ); + } + } + + // now set the power autoloader + $this->autoloader->set(); + } + } + + /** + * Get the Power code + * + * @param object $power A power object. + * + * @return string + * @since 3.2.0 + */ + protected function get(object &$power): string + { + $code = []; + + // set the name space + $code[] = 'namespace ' . $power->_namespace . ';' . PHP_EOL; + + // check if we have header data + if (StringHelper::check($power->head)) + { + $code[] = PHP_EOL . $power->head; + } + + // add description if set + if (StringHelper::check($power->description)) + { + // check if this is escaped + if (strpos($power->description, '/*') === false) + { + // make this description escaped + $power->description = '/**' . PHP_EOL . ' * ' . implode(PHP_EOL . ' * ', explode(PHP_EOL, $power->description)) . PHP_EOL . ' */'; + } + $code[] = PHP_EOL . $power->description; + } + + // build power declaration + $declaration = $power->type . ' ' . $power->class_name; + + // check if we have extends + if (StringHelper::check($power->extends_name)) + { + $declaration .= ' extends ' . $power->extends_name; + } + + // check if we have implements + if (ArrayHelper::check($power->implement_names)) + { + $declaration .= ' implements ' . implode(', ', $power->implement_names); + } + + $code[] = $declaration; + $code[] = '{'; + + // add the main code if set + if (StringHelper::check($power->main_class_code)) + { + $code[] = $power->main_class_code; + } + + $code[] = '}' . PHP_EOL . PHP_EOL; + + return $this->placeholder->update(implode(PHP_EOL, $code), $this->content->active); + } + +} + diff --git a/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Service/Power.php b/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Service/Power.php index 9b6090dc7..f252fe7bd 100644 --- a/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Service/Power.php +++ b/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Service/Power.php @@ -15,6 +15,7 @@ namespace VDM\Joomla\Componentbuilder\Compiler\Service; use Joomla\DI\Container; use Joomla\DI\ServiceProviderInterface; use VDM\Joomla\Componentbuilder\Compiler\Power as Powers; +use VDM\Joomla\Componentbuilder\Compiler\Power\Infusion; use VDM\Joomla\Componentbuilder\Compiler\Power\Autoloader; @@ -40,6 +41,9 @@ class Power implements ServiceProviderInterface $container->alias(Autoloader::class, 'Power.Autoloader') ->share('Power.Autoloader', [$this, 'getAutoloader'], true); + + $container->alias(Infusion::class, 'Power.Infusion') + ->share('Power.Infusion', [$this, 'getInfusion'], true); } /** @@ -76,6 +80,26 @@ class Power implements ServiceProviderInterface $container->get('Content') ); } + + /** + * Get the Compiler Power Infusion + * + * @param Container $container The DI container. + * + * @return Infusion + * @since 3.2.0 + */ + public function getInfusion(Container $container): Infusion + { + return new Infusion( + $container->get('Config'), + $container->get('Power'), + $container->get('Content'), + $container->get('Power.Autoloader'), + $container->get('Placeholder'), + $container->get('Event') + ); + } }