FOF30\Update\Collection::getAllUpdates PHP Метод

getAllUpdates() публичный Метод

Reads a "collection" XML update source and returns the complete tree of categories and extensions applicable for platform version $jVersion
public getAllUpdates ( string $url, string $jVersion = null ) : array
$url string The collection XML update source URL to read from
$jVersion string Joomla! version to fetch updates for, or null to use JVERSION
Результат array A list of update sources applicable to $jVersion
    public function getAllUpdates($url, $jVersion = null)
    {
        // Get the target platform
        if (is_null($jVersion)) {
            $jVersion = JVERSION;
        }
        // Initialise return value
        $updates = array('metadata' => array('name' => '', 'description' => ''), 'categories' => array(), 'extensions' => array());
        // Download and parse the XML file
        $container = Container::getInstance('com_foobar');
        $downloader = new Download($container);
        $xmlSource = $downloader->getFromURL($url);
        try {
            $xml = new SimpleXMLElement($xmlSource, LIBXML_NONET);
        } catch (Exception $e) {
            return $updates;
        }
        // Sanity check
        if ($xml->getName() != 'extensionset') {
            unset($xml);
            return $updates;
        }
        // Initialise return value with the stream metadata (name, description)
        $rootAttributes = $xml->attributes();
        foreach ($rootAttributes as $k => $v) {
            $updates['metadata'][$k] = (string) $v;
        }
        // Initialise the raw list of updates
        $rawUpdates = array('categories' => array(), 'extensions' => array());
        // Segregate the raw list to a hierarchy of extension and category entries
        /** @var SimpleXMLElement $extension */
        foreach ($xml->children() as $extension) {
            switch ($extension->getName()) {
                case 'category':
                    // These are the parameters we expect in a category
                    $params = array('name' => '', 'description' => '', 'category' => '', 'ref' => '', 'targetplatformversion' => $jVersion);
                    // These are the attributes of the element
                    $attributes = $extension->attributes();
                    // Merge them all
                    foreach ($attributes as $k => $v) {
                        $params[$k] = (string) $v;
                    }
                    // We can't have a category with an empty category name
                    if (empty($params['category'])) {
                        continue;
                    }
                    // We can't have a category with an empty ref
                    if (empty($params['ref'])) {
                        continue;
                    }
                    if (empty($params['description'])) {
                        $params['description'] = $params['category'];
                    }
                    if (!array_key_exists($params['category'], $rawUpdates['categories'])) {
                        $rawUpdates['categories'][$params['category']] = array();
                    }
                    $rawUpdates['categories'][$params['category']][] = $params;
                    break;
                case 'extension':
                    // These are the parameters we expect in a category
                    $params = array('element' => '', 'type' => '', 'version' => '', 'name' => '', 'detailsurl' => '', 'targetplatformversion' => $jVersion);
                    // These are the attributes of the element
                    $attributes = $extension->attributes();
                    // Merge them all
                    foreach ($attributes as $k => $v) {
                        $params[$k] = (string) $v;
                    }
                    // We can't have an extension with an empty element
                    if (empty($params['element'])) {
                        continue;
                    }
                    // We can't have an extension with an empty type
                    if (empty($params['type'])) {
                        continue;
                    }
                    // We can't have an extension with an empty version
                    if (empty($params['version'])) {
                        continue;
                    }
                    if (empty($params['name'])) {
                        $params['name'] = $params['element'] . ' ' . $params['version'];
                    }
                    if (!array_key_exists($params['type'], $rawUpdates['extensions'])) {
                        $rawUpdates['extensions'][$params['type']] = array();
                    }
                    if (!array_key_exists($params['element'], $rawUpdates['extensions'][$params['type']])) {
                        $rawUpdates['extensions'][$params['type']][$params['element']] = array();
                    }
                    $rawUpdates['extensions'][$params['type']][$params['element']][] = $params;
                    break;
                default:
                    break;
            }
        }
        unset($xml);
        if (!empty($rawUpdates['categories'])) {
            foreach ($rawUpdates['categories'] as $category => $entries) {
                $update = $this->filterListByPlatform($entries, $jVersion);
                $updates['categories'][$category] = $update;
            }
        }
        if (!empty($rawUpdates['extensions'])) {
            foreach ($rawUpdates['extensions'] as $type => $extensions) {
                $updates['extensions'][$type] = array();
                if (!empty($extensions)) {
                    foreach ($extensions as $element => $entries) {
                        $update = $this->filterListByPlatform($entries, $jVersion);
                        $updates['extensions'][$type][$element] = $update;
                    }
                }
            }
        }
        return $updates;
    }