update 2024-05-22 10:57:27
This commit is contained in:
parent
4335ebb2af
commit
60fe05b75c
@ -28,6 +28,11 @@ class JoomlaPower << (F,LightGreen) >> #RoyalBlue {
|
||||
+ load(array $guids) : void
|
||||
+ get(string $guid, int $build) : ?object
|
||||
- set(string $guid) : bool
|
||||
- convertSettingsToArray(string $settingsJson) : ?array
|
||||
- setTargetVersion(string $guid, ?array $settings) : bool
|
||||
- setNamespaceAndType(string $guid, array $namespace) : void
|
||||
- setClassAndNamespace(string $guid) : void
|
||||
- handlePowerNotFound(string $guid) : bool
|
||||
- extractLastNameFromNamespace(string $namespace) : ?string
|
||||
- removeLastNameFromNamespace(string $namespace) : string
|
||||
- isPowerSet(string $guid) : bool
|
||||
@ -66,62 +71,97 @@ note right of JoomlaPower::get
|
||||
end note
|
||||
|
||||
note left of JoomlaPower::set
|
||||
Set a power
|
||||
Set a Joomla power
|
||||
|
||||
since: 3.2.1
|
||||
return: bool
|
||||
end note
|
||||
|
||||
note right of JoomlaPower::extractLastNameFromNamespace
|
||||
note right of JoomlaPower::convertSettingsToArray
|
||||
Convert settings JSON string to array
|
||||
|
||||
since: 3.2.2
|
||||
return: ?array
|
||||
end note
|
||||
|
||||
note left of JoomlaPower::setTargetVersion
|
||||
Set the target version based on Joomla version and settings
|
||||
|
||||
since: 3.2.2
|
||||
return: bool
|
||||
end note
|
||||
|
||||
note right of JoomlaPower::setNamespaceAndType
|
||||
Set namespace and type for the active power
|
||||
|
||||
since: 3.2.2
|
||||
return: void
|
||||
end note
|
||||
|
||||
note left of JoomlaPower::setClassAndNamespace
|
||||
Set class name and namespace for the active power
|
||||
|
||||
since: 3.2.2
|
||||
return: void
|
||||
end note
|
||||
|
||||
note right of JoomlaPower::handlePowerNotFound
|
||||
Handle power not found scenario
|
||||
|
||||
since: 3.2.2
|
||||
return: bool
|
||||
end note
|
||||
|
||||
note left 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
|
||||
note right of JoomlaPower::removeLastNameFromNamespace
|
||||
Removes the last name from the namespace.
|
||||
|
||||
since: 3.2.1
|
||||
return: string
|
||||
end note
|
||||
|
||||
note right of JoomlaPower::isPowerSet
|
||||
note left of JoomlaPower::isPowerSet
|
||||
Check if the power is already set
|
||||
|
||||
since: 3.2.1
|
||||
return: bool
|
||||
end note
|
||||
|
||||
note left of JoomlaPower::isGuidValid
|
||||
note right of JoomlaPower::isGuidValid
|
||||
Validate the GUID
|
||||
|
||||
since: 3.2.1
|
||||
return: bool
|
||||
end note
|
||||
|
||||
note right of JoomlaPower::getPowerData
|
||||
note left of JoomlaPower::getPowerData
|
||||
Get the power data from the database
|
||||
|
||||
since: 3.2.1
|
||||
return: ?object
|
||||
end note
|
||||
|
||||
note left of JoomlaPower::getCleanNamespace
|
||||
note right of JoomlaPower::getCleanNamespace
|
||||
Get Clean Namespace without use or ; as part of the name space
|
||||
|
||||
since: 3.2.1
|
||||
return: string
|
||||
end note
|
||||
|
||||
note right of JoomlaPower::getUseNamespace
|
||||
note left of JoomlaPower::getUseNamespace
|
||||
Get [use Namespace\Class;]
|
||||
|
||||
since: 3.2.1
|
||||
return: string
|
||||
end note
|
||||
|
||||
note left of JoomlaPower::setSuperPowers
|
||||
note right of JoomlaPower::setSuperPowers
|
||||
Set the super powers of this power
|
||||
|
||||
since: 3.2.1
|
||||
|
@ -207,7 +207,7 @@ final class JoomlaPower implements PowerInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a power
|
||||
* Set a Joomla power
|
||||
*
|
||||
* @param string $guid The global unique id of the power
|
||||
*
|
||||
@ -216,77 +216,175 @@ final class JoomlaPower implements PowerInterface
|
||||
*/
|
||||
private function set(string $guid): bool
|
||||
{
|
||||
// check if we have been here before
|
||||
// Check if power is already set
|
||||
if ($this->isPowerSet($guid))
|
||||
{
|
||||
return $this->state[$guid];
|
||||
}
|
||||
elseif ($this->isGuidValid($guid))
|
||||
{
|
||||
// get the power data
|
||||
$this->active[$guid] = $this->getPowerData($guid);
|
||||
|
||||
if (is_object($this->active[$guid]))
|
||||
// Validate GUID
|
||||
if (!$this->isGuidValid($guid))
|
||||
{
|
||||
// make sure that in recursion we
|
||||
// don't try to load this power again
|
||||
// since during the load of a power we also load
|
||||
// all powers linked to it
|
||||
$this->state[$guid] = true;
|
||||
|
||||
// convert settings to an array
|
||||
if (JsonHelper::check($this->active[$guid]->settings))
|
||||
{
|
||||
$this->active[$guid]->settings = $settings
|
||||
= json_decode($this->active[$guid]->settings, true);
|
||||
$this->state[$guid] = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// set a target version
|
||||
// Get the power data
|
||||
$this->active[$guid] = $this->getPowerData($guid);
|
||||
|
||||
// Validate power data object
|
||||
if ($this->active[$guid] === null)
|
||||
{
|
||||
return $this->handlePowerNotFound($guid);
|
||||
}
|
||||
|
||||
// Prevent recursive loading of the same power
|
||||
$this->state[$guid] = true;
|
||||
|
||||
// Convert settings to array if valid JSON
|
||||
$settings = $this->convertSettingsToArray(
|
||||
$this->active[$guid]->settings
|
||||
);
|
||||
|
||||
// Set the target version if settings array is valid
|
||||
if (!$this->setTargetVersion($guid, $settings))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set class name and namespace
|
||||
$this->setClassAndNamespace($guid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert settings JSON string to array
|
||||
*
|
||||
* @param string $settingsJson
|
||||
*
|
||||
* @return array|null
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function convertSettingsToArray(string $settingsJson): ?array
|
||||
{
|
||||
if (JsonHelper::check($settingsJson))
|
||||
{
|
||||
return json_decode($settingsJson, true);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the target version based on Joomla version and settings
|
||||
*
|
||||
* @param string $guid
|
||||
* @param array|null $settings
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function setTargetVersion(string $guid, ?array $settings): bool
|
||||
{
|
||||
$joomla_version = $this->config->joomla_version;
|
||||
|
||||
if ($joomla_version && ArrayHelper::check($settings))
|
||||
if (!$joomla_version || !ArrayHelper::check($settings))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$joomla_version_target = null;
|
||||
$target_found = false;
|
||||
|
||||
foreach ($settings as $namespace)
|
||||
{
|
||||
if ($joomla_version == $namespace['joomla_version'] ||
|
||||
$namespace['joomla_version'] == 0)
|
||||
// Set default values for all versions
|
||||
if ($namespace['joomla_version'] == 0)
|
||||
{
|
||||
$this->active[$guid]->namespace = $namespace['namespace'];
|
||||
$this->active[$guid]->type = $namespace['type'] ?? 'class';
|
||||
$this->setNamespaceAndType($guid, $namespace);
|
||||
$target_found = true;
|
||||
}
|
||||
|
||||
// Check for direct target version
|
||||
if ($joomla_version == $namespace['joomla_version'])
|
||||
{
|
||||
$joomla_version_target = $namespace;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->active[$guid]->class_name =
|
||||
$this->extractLastNameFromNamespace($this->active[$guid]->namespace);
|
||||
if ($joomla_version_target)
|
||||
{
|
||||
$this->setNamespaceAndType($guid, $joomla_version_target);
|
||||
$target_found = true;
|
||||
}
|
||||
|
||||
$this->active[$guid]->_namespace =
|
||||
$this->removeLastNameFromNamespace($this->active[$guid]->namespace);
|
||||
if (!$target_found)
|
||||
{
|
||||
$this->app->enqueueMessage(
|
||||
Text::sprintf('COM_COMPONENTBUILDER_PJOOMLA_POWER_BGUIDSB_WAS_FOUND_BUT_MISSING_A_NAMESPACE_VALUE_FOR_JOOMLA_SP', $guid, $joomla_version),
|
||||
'Error'
|
||||
);
|
||||
|
||||
// set the approved super power values
|
||||
$this->setSuperPowers($guid);
|
||||
$this->state[$guid] = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set namespace and type for the active power
|
||||
*
|
||||
* @param string $guid
|
||||
* @param array $namespace
|
||||
*
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function setNamespaceAndType(string $guid, array $namespace): void
|
||||
{
|
||||
$this->active[$guid]->namespace = $namespace['namespace'];
|
||||
$this->active[$guid]->type = $namespace['type'] ?? 'class';
|
||||
}
|
||||
|
||||
// we failed to get the power,
|
||||
// so we raise an error message
|
||||
// only if guid is valid
|
||||
if ($this->isGuidValid($guid))
|
||||
/**
|
||||
* Set class name and namespace for the active power
|
||||
*
|
||||
* @param string $guid
|
||||
*
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function setClassAndNamespace(string $guid): void
|
||||
{
|
||||
$this->active[$guid]->class_name = $this->extractLastNameFromNamespace(
|
||||
$this->active[$guid]->namespace
|
||||
);
|
||||
|
||||
$this->active[$guid]->_namespace = $this->removeLastNameFromNamespace(
|
||||
$this->active[$guid]->namespace
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle power not found scenario
|
||||
*
|
||||
* @param string $guid
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function handlePowerNotFound(string $guid): bool
|
||||
{
|
||||
// now we search for it via the super power paths
|
||||
if (empty($this->retry[$guid]) && $this->superpower->load($guid, ['remote', 'local']))
|
||||
{
|
||||
// we found it and it was loaded into the database
|
||||
// Retry loading the power
|
||||
unset($this->state[$guid]);
|
||||
unset($this->active[$guid]);
|
||||
|
||||
// we make sure that this retry only happen once! (just in-case...)
|
||||
$this->retry[$guid] = true;
|
||||
|
||||
// so we try to load it again
|
||||
return $this->set($guid);
|
||||
}
|
||||
|
||||
@ -294,9 +392,7 @@ final class JoomlaPower implements PowerInterface
|
||||
Text::sprintf('COM_COMPONENTBUILDER_PJOOMLA_POWER_BGUIDSB_NOT_FOUNDP', $guid),
|
||||
'Error'
|
||||
);
|
||||
}
|
||||
|
||||
// let's not try again
|
||||
$this->state[$guid] = false;
|
||||
|
||||
return false;
|
||||
|
@ -171,7 +171,7 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a power
|
||||
* Set a Joomla power
|
||||
*
|
||||
* @param string $guid The global unique id of the power
|
||||
*
|
||||
@ -180,77 +180,175 @@
|
||||
*/
|
||||
private function set(string $guid): bool
|
||||
{
|
||||
// check if we have been here before
|
||||
// Check if power is already set
|
||||
if ($this->isPowerSet($guid))
|
||||
{
|
||||
return $this->state[$guid];
|
||||
}
|
||||
elseif ($this->isGuidValid($guid))
|
||||
{
|
||||
// get the power data
|
||||
$this->active[$guid] = $this->getPowerData($guid);
|
||||
|
||||
if (is_object($this->active[$guid]))
|
||||
// Validate GUID
|
||||
if (!$this->isGuidValid($guid))
|
||||
{
|
||||
// make sure that in recursion we
|
||||
// don't try to load this power again
|
||||
// since during the load of a power we also load
|
||||
// all powers linked to it
|
||||
$this->state[$guid] = true;
|
||||
|
||||
// convert settings to an array
|
||||
if (JsonHelper::check($this->active[$guid]->settings))
|
||||
{
|
||||
$this->active[$guid]->settings = $settings
|
||||
= json_decode($this->active[$guid]->settings, true);
|
||||
$this->state[$guid] = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// set a target version
|
||||
// Get the power data
|
||||
$this->active[$guid] = $this->getPowerData($guid);
|
||||
|
||||
// Validate power data object
|
||||
if ($this->active[$guid] === null)
|
||||
{
|
||||
return $this->handlePowerNotFound($guid);
|
||||
}
|
||||
|
||||
// Prevent recursive loading of the same power
|
||||
$this->state[$guid] = true;
|
||||
|
||||
// Convert settings to array if valid JSON
|
||||
$settings = $this->convertSettingsToArray(
|
||||
$this->active[$guid]->settings
|
||||
);
|
||||
|
||||
// Set the target version if settings array is valid
|
||||
if (!$this->setTargetVersion($guid, $settings))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set class name and namespace
|
||||
$this->setClassAndNamespace($guid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert settings JSON string to array
|
||||
*
|
||||
* @param string $settingsJson
|
||||
*
|
||||
* @return array|null
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function convertSettingsToArray(string $settingsJson): ?array
|
||||
{
|
||||
if (JsonHelper::check($settingsJson))
|
||||
{
|
||||
return json_decode($settingsJson, true);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the target version based on Joomla version and settings
|
||||
*
|
||||
* @param string $guid
|
||||
* @param array|null $settings
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function setTargetVersion(string $guid, ?array $settings): bool
|
||||
{
|
||||
$joomla_version = $this->config->joomla_version;
|
||||
|
||||
if ($joomla_version && ArrayHelper::check($settings))
|
||||
if (!$joomla_version || !ArrayHelper::check($settings))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$joomla_version_target = null;
|
||||
$target_found = false;
|
||||
|
||||
foreach ($settings as $namespace)
|
||||
{
|
||||
if ($joomla_version == $namespace['joomla_version'] ||
|
||||
$namespace['joomla_version'] == 0)
|
||||
// Set default values for all versions
|
||||
if ($namespace['joomla_version'] == 0)
|
||||
{
|
||||
$this->active[$guid]->namespace = $namespace['namespace'];
|
||||
$this->active[$guid]->type = $namespace['type'] ?? 'class';
|
||||
$this->setNamespaceAndType($guid, $namespace);
|
||||
$target_found = true;
|
||||
}
|
||||
|
||||
// Check for direct target version
|
||||
if ($joomla_version == $namespace['joomla_version'])
|
||||
{
|
||||
$joomla_version_target = $namespace;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$this->active[$guid]->class_name =
|
||||
$this->extractLastNameFromNamespace($this->active[$guid]->namespace);
|
||||
if ($joomla_version_target)
|
||||
{
|
||||
$this->setNamespaceAndType($guid, $joomla_version_target);
|
||||
$target_found = true;
|
||||
}
|
||||
|
||||
$this->active[$guid]->_namespace =
|
||||
$this->removeLastNameFromNamespace($this->active[$guid]->namespace);
|
||||
if (!$target_found)
|
||||
{
|
||||
$this->app->enqueueMessage(
|
||||
Text::sprintf('<p>Joomla Power <b>guid:%s</b> was found, but missing a namespace value for Joomla %s!</p>', $guid, $joomla_version),
|
||||
'Error'
|
||||
);
|
||||
|
||||
// set the approved super power values
|
||||
$this->setSuperPowers($guid);
|
||||
$this->state[$guid] = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set namespace and type for the active power
|
||||
*
|
||||
* @param string $guid
|
||||
* @param array $namespace
|
||||
*
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function setNamespaceAndType(string $guid, array $namespace): void
|
||||
{
|
||||
$this->active[$guid]->namespace = $namespace['namespace'];
|
||||
$this->active[$guid]->type = $namespace['type'] ?? 'class';
|
||||
}
|
||||
|
||||
// we failed to get the power,
|
||||
// so we raise an error message
|
||||
// only if guid is valid
|
||||
if ($this->isGuidValid($guid))
|
||||
/**
|
||||
* Set class name and namespace for the active power
|
||||
*
|
||||
* @param string $guid
|
||||
*
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function setClassAndNamespace(string $guid): void
|
||||
{
|
||||
$this->active[$guid]->class_name = $this->extractLastNameFromNamespace(
|
||||
$this->active[$guid]->namespace
|
||||
);
|
||||
|
||||
$this->active[$guid]->_namespace = $this->removeLastNameFromNamespace(
|
||||
$this->active[$guid]->namespace
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle power not found scenario
|
||||
*
|
||||
* @param string $guid
|
||||
*
|
||||
* @return bool
|
||||
* @since 3.2.2
|
||||
*/
|
||||
private function handlePowerNotFound(string $guid): bool
|
||||
{
|
||||
// now we search for it via the super power paths
|
||||
if (empty($this->retry[$guid]) && $this->superpower->load($guid, ['remote', 'local']))
|
||||
{
|
||||
// we found it and it was loaded into the database
|
||||
// Retry loading the power
|
||||
unset($this->state[$guid]);
|
||||
unset($this->active[$guid]);
|
||||
|
||||
// we make sure that this retry only happen once! (just in-case...)
|
||||
$this->retry[$guid] = true;
|
||||
|
||||
// so we try to load it again
|
||||
return $this->set($guid);
|
||||
}
|
||||
|
||||
@ -258,9 +356,7 @@
|
||||
Text::sprintf('<p>Joomla Power <b>guid:%s</b> not found!</p>', $guid),
|
||||
'Error'
|
||||
);
|
||||
}
|
||||
|
||||
// let's not try again
|
||||
$this->state[$guid] = false;
|
||||
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user