UpdateModel::parseInfoArray PHP Method

parseInfoArray() public static method

Offers a quick and dirty way of parsing an addon's info array without using eval().
public static parseInfoArray ( string $Path, string $Variable = false ) : array | false
$Path string The path to the info array.
$Variable string The name of variable containing the information.
return array | false The info array or false if the file could not be parsed.
    public static function parseInfoArray($Path, $Variable = false)
    {
        $fp = fopen($Path, 'rb');
        $Lines = array();
        $InArray = false;
        $GlobalKey = '';
        // Get all of the lines in the info array.
        while (($Line = fgets($fp)) !== false) {
            // Remove comments from the line.
            $Line = preg_replace('`\\s//.*$`', '', $Line);
            if (!$Line) {
                continue;
            }
            if (!$InArray && preg_match('`\\$([A-Za-z]+Info)\\s*\\[`', trim($Line), $Matches)) {
                $Variable = $Matches[1];
                if (preg_match('`\\[\\s*[\'"](.+?)[\'"]\\s*\\]`', $Line, $Matches)) {
                    $GlobalKey = $Matches[1];
                    $InArray = true;
                }
            } elseif ($InArray && StringEndsWith(trim($Line), ';')) {
                break;
            } elseif ($InArray) {
                $Lines[] = trim($Line);
            }
        }
        fclose($fp);
        if (count($Lines) == 0) {
            return false;
        }
        // Parse the name/value information in the arrays.
        $Result = array();
        foreach ($Lines as $Line) {
            // Get the name from the line.
            if (!preg_match('`[\'"](.+?)[\'"]\\s*=>`', $Line, $Matches) || !substr($Line, -1) == ',') {
                continue;
            }
            $Key = $Matches[1];
            // Strip the key from the line.
            $Line = trim(trim(substr(strstr($Line, '=>'), 2)), ',');
            if (strlen($Line) == 0) {
                continue;
            }
            $Value = null;
            if (is_numeric($Line)) {
                $Value = $Line;
            } elseif (strcasecmp($Line, 'TRUE') == 0 || strcasecmp($Line, 'FALSE') == 0) {
                $Value = $Line;
            } elseif (in_array($Line[0], array('"', "'")) && substr($Line, -1) == $Line[0]) {
                $Quote = $Line[0];
                $Value = trim($Line, $Quote);
                $Value = str_replace('\\' . $Quote, $Quote, $Value);
            } elseif (stringBeginsWith($Line, 'array(') && substr($Line, -1) == ')') {
                // Parse the line's array.
                $Line = substr($Line, 6, strlen($Line) - 7);
                $Items = explode(',', $Line);
                $Array = array();
                foreach ($Items as $Item) {
                    $SubItems = explode('=>', $Item);
                    if (count($SubItems) == 1) {
                        $Array[] = trim(trim($SubItems[0]), '"\'');
                    } elseif (count($SubItems) == 2) {
                        $SubKey = trim(trim($SubItems[0]), '"\'');
                        $SubValue = trim(trim($SubItems[1]), '"\'');
                        $Array[$SubKey] = $SubValue;
                    }
                }
                $Value = $Array;
            }
            if ($Value != null) {
                $Result[$Key] = $Value;
            }
        }
        $Result = array($GlobalKey => $Result, 'Variable' => $Variable);
        return $Result;
    }