How to get component version from Joomla?

2019-06-09 13:40发布

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...

6条回答
男人必须洒脱
2楼-- · 2019-06-09 13:51

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'];
查看更多
Summer. ? 凉城
3楼-- · 2019-06-09 13:59

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

查看更多
我只想做你的唯一
4楼-- · 2019-06-09 14:16

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'];
}
查看更多
神经病院院长
5楼-- · 2019-06-09 14:16

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楼-- · 2019-06-09 14:16

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.

查看更多
地球回转人心会变
7楼-- · 2019-06-09 14:18

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.

查看更多
登录 后发表回答