How to get component version from Joomla?

2019-06-09 13:29发布

问题:

Is there some method to get components version by it's name? I tried a lot of examples but didn't find any that would work. The thing like JComponentHelper::getParams('com_mycomponent')->get('version') doesn't work as it retrieves values from jos_extensions.params row. My components version is only in jos_extensions.manifest_cache row...

回答1:

You can use the following:

Joomla 2.5:

$parser = JFactory::getXMLParser('Simple');
$xml = JPATH_SITE .'/components/com_wrapper/wrapper.xml';
$parser->loadFile($xml);
$doc = $parser->document;
$element = $doc->getElementByPath('version');
$version = $element->data();

echo $version;

Joomla 3.x (Platform 13.3 and below)

$xml = JFactory::getXML(JPATH_SITE .'/components/com_wrapper/wrapper.xml');
$version = (string)$xml->version;

echo $version;

Joomla 3.2+:

getXML was made deprecated as of 2 weeks after this answer. Instead, use SimpleXML


Obviously this is an example for the Wrapper component so change the paths to whatever suits your needs.

Hope this helps



回答2:

If you are looking for a listing of all components within your Joomla site, you can find them by using the Extension Manager -> Manage and select type filter "Components". This will give you a listing with a Version column showing the versions of all the Components. That's an answer to your original question without coding.



回答3:

Hi I use this function to return versions from the extensions table based on element (I find its more reliable than name) but you could easily substitute element for name.

I have this in a plugin and the function only needs the element($ext) to check against the current extension in the table.

function extversion($ext) {
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('manifest_cache');
    $query->from($db->quoteName('#__extensions'));
    $query->where('element = "' . $ext . '"');
    $db->setQuery($query);

    $manifest = json_decode($db->loadResult(), true);
    return $manifest['version'];
}


回答4:

Based on the answer of CoalaWeb, but with less code (more code use from the framework):

/** @var JTableExtension $extension */
$extension = JTable::getInstance('extension');
$id = $extension->find(array('element' => $extension));
$extension->load($id);
$componentInfo = json_decode($extension->manifest_cache, true);

return $componentInfo['version'];


回答5:

Joomla 1.5, 2.5, 3.X.

We are using different XML files for Joomla 1.5 (com_ponent.xml) and for 2.5, 3.X (_manifest.xml) versions. Using SimpleXML :

public static function getVbssoVersion() {
   $xml_path = JPATH_ADMINISTRATOR . '/components/com_ponent/';
   $xml_path .= (defined('JVER') && JVER == '1.5') ? 'com_ponent.xml' : '_manifest.xml';
   $xml_obj = new SimpleXMLElement(file_get_contents($xml_path));

   return strval($xml_obj->version); 
}


回答6:

To fully leverage the Joomla API, use the JComponentHelper, \Joomla\Registry\Registry and JTable classes:

$component = \JComponentHelper::getComponent('com_myextension');
$extension = \JTable::getInstance('extension');
$extension->load($component->id);
$manifest = new \Joomla\Registry\Registry($extension->manifest_cache);

echo $manifest->get('version');

This applies mainly to newer 3.x versions of Joomla; 3.6+ probably.