From 41aa3a166ccfb36cca5832ff9bb7221bb338f90b Mon Sep 17 00:00:00 2001 From: aB0t Date: Tue, 16 Apr 2024 17:05:43 +0200 Subject: [PATCH] update 2024-04-16 17:05:43 --- .../README.md | 36 +++-- .../code.php | 135 +++++++++++++----- .../code.power | 128 +++++++++++++---- .../settings.json | 14 +- 4 files changed, 226 insertions(+), 87 deletions(-) diff --git a/src/3730a000-ab64-477d-8b0f-70eaf71b517a/README.md b/src/3730a000-ab64-477d-8b0f-70eaf71b517a/README.md index b42502f..db4c59b 100644 --- a/src/3730a000-ab64-477d-8b0f-70eaf71b517a/README.md +++ b/src/3730a000-ab64-477d-8b0f-70eaf71b517a/README.md @@ -28,6 +28,8 @@ class JoomlaPower << (F,LightGreen) >> #RoyalBlue { + load(array $guids) : void + get(string $guid, int $build) : ?object - set(string $guid) : bool + - extractLastNameFromNamespace(string $namespace) : ?string + - removeLastNameFromNamespace(string $namespace) : string - isPowerSet(string $guid) : bool - isGuidValid(string $guid) : bool - getPowerData(string $guid) : ?object @@ -39,7 +41,7 @@ class JoomlaPower << (F,LightGreen) >> #RoyalBlue { note right of JoomlaPower::__construct Constructor. - since: 3.2.0 + since: 3.2.1 arguments: Config $config @@ -52,63 +54,77 @@ end note note left of JoomlaPower::load load all the powers linked to this component - since: 3.2.0 + since: 3.2.1 return: void end note note right of JoomlaPower::get Get a power - since: 3.2.0 + since: 3.2.1 return: ?object end note note left of JoomlaPower::set Set a power - since: 3.2.0 + since: 3.2.1 return: bool end note +note right of JoomlaPower::extractLastNameFromNamespace + Extracts the last part of a namespace string, which is typically the class name. + + since: 3.2.1 + return: ?string +end note + +note left of JoomlaPower::removeLastNameFromNamespace + Removes the last name from the namespace. + + since: 3.2.1 + return: string +end note + note right of JoomlaPower::isPowerSet Check if the power is already set - since: 3.2.0 + since: 3.2.1 return: bool end note note left of JoomlaPower::isGuidValid Validate the GUID - since: 3.2.0 + since: 3.2.1 return: bool end note note right of JoomlaPower::getPowerData Get the power data from the database - since: 3.2.0 + since: 3.2.1 return: ?object end note note left of JoomlaPower::getCleanNamespace Get Clean Namespace without use or ; as part of the name space - since: 3.2.0 + since: 3.2.1 return: string end note note right of JoomlaPower::getUseNamespace Get [use Namespace\Class;] - since: 3.2.0 + since: 3.2.1 return: string end note note left of JoomlaPower::setSuperPowers Set the super powers of this power - since: 3.2.0 + since: 3.2.1 return: void end note diff --git a/src/3730a000-ab64-477d-8b0f-70eaf71b517a/code.php b/src/3730a000-ab64-477d-8b0f-70eaf71b517a/code.php index 158c37f..2aa3a5f 100644 --- a/src/3730a000-ab64-477d-8b0f-70eaf71b517a/code.php +++ b/src/3730a000-ab64-477d-8b0f-70eaf71b517a/code.php @@ -16,12 +16,9 @@ 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\JsonHelper; use VDM\Joomla\Utilities\GuidHelper; -use VDM\Joomla\Utilities\String\ClassfunctionHelper; use VDM\Joomla\Utilities\String\NamespaceHelper; -use VDM\Joomla\Componentbuilder\Compiler\Factory as Compiler; use VDM\Joomla\Componentbuilder\Compiler\Config; use VDM\Joomla\Componentbuilder\Compiler\Placeholder; use VDM\Joomla\Componentbuilder\Compiler\Customcode; @@ -31,7 +28,9 @@ use VDM\Joomla\Componentbuilder\Compiler\Interfaces\PowerInterface; /** - * Power + * Joomla Power + * + * @since 3.2.1 */ final class JoomlaPower implements PowerInterface { @@ -39,7 +38,7 @@ final class JoomlaPower implements PowerInterface * All loaded powers * * @var array - * @since 3.2.0 + * @since 3.2.1 **/ public array $active = []; @@ -47,7 +46,7 @@ final class JoomlaPower implements PowerInterface * All power namespaces * * @var array - * @since 3.2.0 + * @since 3.2.1 **/ public array $namespace = []; @@ -55,7 +54,7 @@ final class JoomlaPower implements PowerInterface * All super powers of this build * * @var array - * @since 3.2.0 + * @since 3.2.1 **/ public array $superpowers = []; @@ -63,7 +62,7 @@ final class JoomlaPower implements PowerInterface * Old super powers found in the local repos * * @var array - * @since 3.2.0 + * @since 3.2.1 **/ public array $old_superpowers = []; @@ -71,7 +70,7 @@ final class JoomlaPower implements PowerInterface * The url to the power, if there is an error. * * @var string - * @since 3.2.0 + * @since 3.2.1 **/ protected string $fixUrl; @@ -79,7 +78,7 @@ final class JoomlaPower implements PowerInterface * The state of all loaded powers * * @var array - * @since 3.2.0 + * @since 3.2.1 **/ protected array $state = []; @@ -87,7 +86,7 @@ final class JoomlaPower implements PowerInterface * The state of retry to loaded powers * * @var array - * @since 3.2.0 + * @since 3.2.1 **/ protected array $retry = []; @@ -95,7 +94,7 @@ final class JoomlaPower implements PowerInterface * Compiler Config * * @var Config - * @since 3.2.0 + * @since 3.2.1 **/ protected Config $config; @@ -103,7 +102,7 @@ final class JoomlaPower implements PowerInterface * Compiler Placeholder * * @var Placeholder - * @since 3.2.0 + * @since 3.2.1 **/ protected Placeholder $placeholder; @@ -111,7 +110,7 @@ final class JoomlaPower implements PowerInterface * Compiler Customcode * * @var Customcode - * @since 3.2.0 + * @since 3.2.1 **/ protected Customcode $customcode; @@ -119,7 +118,7 @@ final class JoomlaPower implements PowerInterface * Compiler Customcode in Gui * * @var Gui - * @since 3.2.0 + * @since 3.2.1 **/ protected Gui $gui; @@ -127,21 +126,21 @@ final class JoomlaPower implements PowerInterface * The JCB Superpower class * * @var Superpower - * @since 3.2.0 + * @since 3.2.1 **/ protected Superpower $superpower; /** * Database object to query local DB * - * @since 3.2.0 + * @since 3.2.1 **/ protected $db; /** * Database object to query local DB * - * @since 3.2.0 + * @since 3.2.1 **/ protected $app; @@ -155,7 +154,7 @@ final class JoomlaPower implements PowerInterface * @param Superpower $superpower The JCB superpower object. * * @throws \Exception - * @since 3.2.0 + * @since 3.2.1 */ public function __construct(Config $config, Placeholder $placeholder, Customcode $customcode, Gui $gui, Superpower $superpower) @@ -175,7 +174,7 @@ final class JoomlaPower implements PowerInterface * @param array $guids The global unique ids of the linked powers * * @return void - * @since 3.2.0 + * @since 3.2.1 */ public function load(array $guids) { @@ -195,7 +194,7 @@ final class JoomlaPower implements PowerInterface * @param int $build Force build switch (to override global switch) * * @return object|null - * @since 3.2.0 + * @since 3.2.1 */ public function get(string $guid, int $build = 0): ?object { @@ -213,7 +212,7 @@ final class JoomlaPower implements PowerInterface * @param string $guid The global unique id of the power * * @return bool true on successful setting of a power - * @since 3.2.0 + * @since 3.2.1 */ private function set(string $guid): bool { @@ -235,14 +234,40 @@ final class JoomlaPower implements PowerInterface // all powers linked to it $this->state[$guid] = true; - echo '
';
-				var_dump($this->active[$guid]);
-				exit;
+				// convert settings to an array
+				if (JsonHelper::check($this->active[$guid]->settings))
+				{
+					$this->active[$guid]->settings = $settings
+						= json_decode($this->active[$guid]->settings, true);
+				}
 
-				// set the approved super power values
-				$this->setSuperPowers($guid);
+				// set a target version
+				$joomla_version = $this->config->joomla_version;
 
-				return true;
+				if ($joomla_version && ArrayHelper::check($settings))
+				{
+					foreach ($settings as $namespace)
+					{
+						if ($joomla_version == $namespace['joomla_version'] ||
+							$namespace['joomla_version'] == 0)
+						{
+							$this->active[$guid]->namespace = $namespace['namespace'];
+							$this->active[$guid]->type = $namespace['type'] ?? 'class';
+							break;
+						}
+					}
+
+					$this->active[$guid]->class_name =
+						$this->extractLastNameFromNamespace($this->active[$guid]->namespace);
+
+					$this->active[$guid]->_namespace =
+						$this->removeLastNameFromNamespace($this->active[$guid]->namespace);
+
+					// set the approved super power values
+					$this->setSuperPowers($guid);
+
+					return true;
+				}
 			}
 		}
 
@@ -277,13 +302,55 @@ final class JoomlaPower implements PowerInterface
 		return false;
 	}
 
+	/**
+	 * Extracts the last part of a namespace string, which is typically the class name.
+	 *
+	 * @param string $namespace  The namespace string to extract from.
+	 *
+	 * @return string|null The extracted class name.
+	 * @since 3.2.1
+	 */
+	private function extractLastNameFromNamespace(string $namespace): ?string
+	{
+		$parts = explode('\\', $namespace);
+		$result = end($parts);
+
+		// Remove '\\' from the beginning and end of the resulting string
+		$result = trim($result, '\\');
+
+		// If the resulting string is empty, return null
+		return empty($result) ? null : $result;
+	}
+
+	/**
+	 * Removes the last name from the namespace.
+	 *
+	 * @param string $namespace The namespace
+	 *
+	 * @return string The namespace shortened
+	 * @since 3.2.1
+	 */
+	private function removeLastNameFromNamespace(string $namespace): string
+	{
+		// Remove '\\' from the beginning and end of the resulting string
+		$namespace = trim($namespace, '\\');
+
+		$parts = explode('\\', $namespace);
+
+		// Remove the last part (the class name)
+		array_pop($parts);
+
+		// Reassemble the namespace without the class name
+		return implode('\\', $parts);
+	}
+
 	/**
 	 * Check if the power is already set
 	 *
 	 * @param string  $guid  The global unique id of the power
 	 *
 	 * @return bool true if the power is already set
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function isPowerSet(string $guid): bool
 	{
@@ -296,7 +363,7 @@ final class JoomlaPower implements PowerInterface
 	 * @param string  $guid  The global unique id of the power
 	 *
 	 * @return bool true if the GUID is valid
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function isGuidValid(string $guid): bool
 	{
@@ -309,7 +376,7 @@ final class JoomlaPower implements PowerInterface
 	 * @param string  $guid  The global unique id of the power
 	 *
 	 * @return object|null The power data
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function getPowerData(string $guid): ?object
 	{
@@ -336,7 +403,7 @@ final class JoomlaPower implements PowerInterface
 	 * @param bool    $removeNumbers    The switch to remove numbers
 	 *
 	 * @return string
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function getCleanNamespace(string $namespace): string
 	{
@@ -351,7 +418,7 @@ final class JoomlaPower implements PowerInterface
 	 * @param string   $as                The use as name (default is none)
 	 *
 	 * @return string
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function getUseNamespace(string $namespace, string $as = 'default'): string
 	{
@@ -369,7 +436,7 @@ final class JoomlaPower implements PowerInterface
 	 * @param string  $guid   The global unique id of the power
 	 *
 	 * @return void
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function setSuperPowers(string $guid): void
 	{
diff --git a/src/3730a000-ab64-477d-8b0f-70eaf71b517a/code.power b/src/3730a000-ab64-477d-8b0f-70eaf71b517a/code.power
index 174cf45..8ab8d24 100644
--- a/src/3730a000-ab64-477d-8b0f-70eaf71b517a/code.power
+++ b/src/3730a000-ab64-477d-8b0f-70eaf71b517a/code.power
@@ -2,7 +2,7 @@
 	 * All loaded powers
 	 *
 	 * @var    array
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	public array $active = [];
 
@@ -10,7 +10,7 @@
 	 * All power namespaces
 	 *
 	 * @var    array
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	public array $namespace = [];
 
@@ -18,7 +18,7 @@
 	 * All super powers of this build
 	 *
 	 * @var    array
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	public array $superpowers = [];
 
@@ -26,7 +26,7 @@
 	 * Old super powers found in the local repos
 	 *
 	 * @var    array
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	public array $old_superpowers = [];
 
@@ -34,7 +34,7 @@
 	 * The url to the power, if there is an error.
 	 *
 	 * @var   string
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected string $fixUrl;
 
@@ -42,7 +42,7 @@
 	 * The state of all loaded powers
 	 *
 	 * @var    array
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected array $state = [];
 
@@ -50,7 +50,7 @@
 	 * The state of retry to loaded powers
 	 *
 	 * @var    array
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected array $retry = [];
 
@@ -58,7 +58,7 @@
 	 * Compiler Config
 	 *
 	 * @var    Config
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected Config $config;
 
@@ -66,7 +66,7 @@
 	 * Compiler Placeholder
 	 *
 	 * @var    Placeholder
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected Placeholder $placeholder;
 
@@ -74,7 +74,7 @@
 	 * Compiler Customcode
 	 *
 	 * @var    Customcode
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected Customcode $customcode;
 
@@ -82,7 +82,7 @@
 	 * Compiler Customcode in Gui
 	 *
 	 * @var    Gui
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected Gui $gui;
 
@@ -90,21 +90,21 @@
 	 * The JCB Superpower class
 	 *
 	 * @var    Superpower
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected Superpower $superpower;
 
 	/**
 	 * Database object to query local DB
 	 *
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected $db;
 
 	/**
 	 * Database object to query local DB
 	 *
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 **/
 	protected $app;
 
@@ -118,7 +118,7 @@
 	 * @param Superpower         $superpower   The JCB superpower object.
 	 *
 	 * @throws \Exception
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	public function __construct(Config $config, Placeholder $placeholder,
 		Customcode $customcode, Gui $gui, Superpower $superpower)
@@ -138,7 +138,7 @@
 	 * @param array   $guids    The global unique ids of the linked powers
 	 *
 	 * @return void
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	public function load(array $guids)
 	{
@@ -158,7 +158,7 @@
 	 * @param int       $build   Force build switch (to override global switch)
 	 *
 	 * @return object|null
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	public function get(string $guid, int $build = 0): ?object
 	{
@@ -176,7 +176,7 @@
 	 * @param string   $guid    The global unique id of the power
 	 *
 	 * @return bool  true on successful setting of a power
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function set(string $guid): bool
 	{
@@ -198,14 +198,40 @@
 				// all powers linked to it
 				$this->state[$guid] = true;
 
-				echo '
';
-				var_dump($this->active[$guid]);
-				exit;
+				// convert settings to an array
+				if (JsonHelper::check($this->active[$guid]->settings))
+				{
+					$this->active[$guid]->settings = $settings
+						= json_decode($this->active[$guid]->settings, true);
+				}
 
-				// set the approved super power values
-				$this->setSuperPowers($guid);
+				// set a target version
+				$joomla_version = $this->config->joomla_version;
 
-				return true;
+				if ($joomla_version && ArrayHelper::check($settings))
+				{
+					foreach ($settings as $namespace)
+					{
+						if ($joomla_version == $namespace['joomla_version'] ||
+							$namespace['joomla_version'] == 0)
+						{
+							$this->active[$guid]->namespace = $namespace['namespace'];
+							$this->active[$guid]->type = $namespace['type'] ?? 'class';
+							break;
+						}
+					}
+
+					$this->active[$guid]->class_name =
+						$this->extractLastNameFromNamespace($this->active[$guid]->namespace);
+
+					$this->active[$guid]->_namespace =
+						$this->removeLastNameFromNamespace($this->active[$guid]->namespace);
+
+					// set the approved super power values
+					$this->setSuperPowers($guid);
+
+					return true;
+				}
 			}
 		}
 
@@ -240,13 +266,55 @@
 		return false;
 	}
 
+	/**
+	 * Extracts the last part of a namespace string, which is typically the class name.
+	 *
+	 * @param string $namespace  The namespace string to extract from.
+	 *
+	 * @return string|null The extracted class name.
+	 * @since 3.2.1
+	 */
+	private function extractLastNameFromNamespace(string $namespace): ?string
+	{
+		$parts = explode('\\', $namespace);
+		$result = end($parts);
+
+		// Remove '\\' from the beginning and end of the resulting string
+		$result = trim($result, '\\');
+
+		// If the resulting string is empty, return null
+		return empty($result) ? null : $result;
+	}
+
+	/**
+	 * Removes the last name from the namespace.
+	 *
+	 * @param string $namespace The namespace
+	 *
+	 * @return string The namespace shortened
+	 * @since 3.2.1
+	 */
+	private function removeLastNameFromNamespace(string $namespace): string
+	{
+		// Remove '\\' from the beginning and end of the resulting string
+		$namespace = trim($namespace, '\\');
+
+		$parts = explode('\\', $namespace);
+
+		// Remove the last part (the class name)
+		array_pop($parts);
+
+		// Reassemble the namespace without the class name
+		return implode('\\', $parts);
+	}
+
 	/**
 	 * Check if the power is already set
 	 *
 	 * @param string  $guid  The global unique id of the power
 	 *
 	 * @return bool true if the power is already set
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function isPowerSet(string $guid): bool
 	{
@@ -259,7 +327,7 @@
 	 * @param string  $guid  The global unique id of the power
 	 *
 	 * @return bool true if the GUID is valid
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function isGuidValid(string $guid): bool
 	{
@@ -272,7 +340,7 @@
 	 * @param string  $guid  The global unique id of the power
 	 *
 	 * @return object|null The power data
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function getPowerData(string $guid): ?object
 	{
@@ -299,7 +367,7 @@
 	 * @param bool    $removeNumbers    The switch to remove numbers
 	 *
 	 * @return string
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function getCleanNamespace(string $namespace): string
 	{
@@ -314,7 +382,7 @@
 	 * @param string   $as                The use as name (default is none)
 	 *
 	 * @return string
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function getUseNamespace(string $namespace, string $as = 'default'): string
 	{
@@ -332,7 +400,7 @@
 	 * @param string  $guid   The global unique id of the power
 	 *
 	 * @return void
-	 * @since 3.2.0
+	 * @since 3.2.1
 	 */
 	private function setSuperPowers(string $guid): void
 	{
diff --git a/src/3730a000-ab64-477d-8b0f-70eaf71b517a/settings.json b/src/3730a000-ab64-477d-8b0f-70eaf71b517a/settings.json
index 90fe16a..5ea74e9 100644
--- a/src/3730a000-ab64-477d-8b0f-70eaf71b517a/settings.json
+++ b/src/3730a000-ab64-477d-8b0f-70eaf71b517a/settings.json
@@ -16,10 +16,6 @@
             "use": "0a59c65c-9daf-4bc9-baf4-e063ff9e6a8a",
             "as": "default"
         },
-        "use_selection1": {
-            "use": "1f28cb53-60d9-4db1-b517-3c7dc6b429ef",
-            "as": "default"
-        },
         "use_selection2": {
             "use": "4b225c51-d293-48e4-b3f6-5136cf5c3f18",
             "as": "default"
@@ -28,18 +24,10 @@
             "use": "9c513baf-b279-43fd-ae29-a585c8cbc4f0",
             "as": "default"
         },
-        "use_selection4": {
-            "use": "30c5b4c2-f75f-4d15-869a-f8bfedd87358",
-            "as": "default"
-        },
         "use_selection5": {
             "use": "ce8cf834-6bac-44fb-941c-861f7e046cc0",
             "as": "default"
         },
-        "use_selection6": {
-            "use": "d910d8b8-4c23-4f3e-8cda-438f2d2bd7ac",
-            "as": "Compiler"
-        },
         "use_selection7": {
             "use": "fa4bf18e-301e-42e3-91fb-6e0096c07adc",
             "as": "default"
@@ -62,7 +50,7 @@
         }
     },
     "namespace": "VDM\\Joomla\\Componentbuilder.Compiler.JoomlaPower",
-    "description": "Power",
+    "description": "Joomla Power\r\n\r\n@since 3.2.1",
     "licensing_template": "\/**\r\n * @package    Joomla.Component.Builder\r\n *\r\n * @created    4th September, 2022\r\n * @author     Llewellyn van der Merwe \r\n * @git        Joomla Component Builder \r\n * @copyright  Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license    GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n",
     "head": "use Joomla\\CMS\\Factory;\r\nuse Joomla\\CMS\\Application\\CMSApplication;\r\nuse Joomla\\CMS\\Language\\Text;",
     "composer": ""