PEAR_PackageFile::fromXmlString PHP Метод

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

Create a PEAR_PackageFile_v* from an XML string.
public fromXmlString ( string $data, integer $state, string $file, string $archive = false ) : PEAR_PackageFile_v1 | PEAR_PackageFile_v2
$data string contents of package.xml file
$state integer package state (one of PEAR_VALIDATE_* constants)
$file string full path to the package.xml file (and the files it references)
$archive string optional name of the archive that the XML was extracted from, if any
Результат PEAR_PackageFile_v1 | PEAR_PackageFile_v2
    function &fromXmlString($data, $state, $file, $archive = false)
    {
        if (preg_match('/<package[^>]+version="([0-9]+\\.[0-9]+)"/', $data, $packageversion)) {
            if (!in_array($packageversion[1], array('1.0', '2.0', '2.1'))) {
                return PEAR::raiseError('package.xml version "' . $packageversion[1] . '" is not supported, only 1.0, 2.0, and 2.1 are supported.');
            }
            $object =& $this->parserFactory($packageversion[1]);
            if ($this->_logger) {
                $object->setLogger($this->_logger);
            }
            $object->setConfig($this->_config);
            $pf = $object->parse($data, $file, $archive);
            if (PEAR::isError($pf)) {
                return $pf;
            }
            if ($this->_rawReturn) {
                return $pf;
            }
            if (!$pf->validate($state)) {
                if ($this->_config->get('verbose') > 0 && $this->_logger && $pf->getValidationWarnings(false)) {
                    foreach ($pf->getValidationWarnings(false) as $warning) {
                        $this->_logger->log(0, 'ERROR: ' . $warning['message']);
                    }
                }
                $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed', 2, null, null, $pf->getValidationWarnings());
                return $a;
            }
            if ($this->_logger && $pf->getValidationWarnings(false)) {
                foreach ($pf->getValidationWarnings() as $warning) {
                    $this->_logger->log(0, 'WARNING: ' . $warning['message']);
                }
            }
            if (method_exists($pf, 'flattenFilelist')) {
                $pf->flattenFilelist();
                // for v2
            }
            return $pf;
        } elseif (preg_match('/<package[^>]+version="([^"]+)"/', $data, $packageversion)) {
            $a = PEAR::raiseError('package.xml file "' . $file . '" has unsupported package.xml <package> version "' . $packageversion[1] . '"');
            return $a;
        } else {
            if (!class_exists('PEAR_ErrorStack')) {
                require_once 'PEAR/ErrorStack.php';
            }
            PEAR_ErrorStack::staticPush('PEAR_PackageFile', PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION, 'warning', array('xml' => $data), 'package.xml "' . $file . '" has no package.xml <package> version');
            $object =& $this->parserFactory('1.0');
            $object->setConfig($this->_config);
            $pf = $object->parse($data, $file, $archive);
            if (PEAR::isError($pf)) {
                return $pf;
            }
            if ($this->_rawReturn) {
                return $pf;
            }
            if (!$pf->validate($state)) {
                $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed', 2, null, null, $pf->getValidationWarnings());
                return $a;
            }
            if ($this->_logger && $pf->getValidationWarnings(false)) {
                foreach ($pf->getValidationWarnings() as $warning) {
                    $this->_logger->log(0, 'WARNING: ' . $warning['message']);
                }
            }
            if (method_exists($pf, 'flattenFilelist')) {
                $pf->flattenFilelist();
                // for v2
            }
            return $pf;
        }
    }

Usage Example

Пример #1
0
 /**
  * Create a PEAR_PackageFile_v* from a package.xml file.
  *
  * @access public
  * @param   string  $descfile  name of package xml file
  * @param   int     $state package state (one of PEAR_VALIDATE_* constants)
  * @param   string|false $archive name of the archive this package.xml came
  *          from, if any
  * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
  * @uses    PEAR_PackageFile::fromXmlString to create the oject after the
  *          XML is loaded from the package.xml file.
  */
 function &fromPackageFile($descfile, $state, $archive = false)
 {
     if (is_string($descfile) && strlen($descfile) < 255 && (!file_exists($descfile) || !is_file($descfile) || !is_readable($descfile) || !($fp = @fopen($descfile, 'r')))) {
         $a = PEAR::raiseError("Unable to open {$descfile}");
         return $a;
     }
     // read the whole thing so we only get one cdata callback
     // for each block of cdata
     fclose($fp);
     $data = file_get_contents($descfile);
     $ret =& PEAR_PackageFile::fromXmlString($data, $state, $descfile, $archive);
     return $ret;
 }
All Usage Examples Of PEAR_PackageFile::fromXmlString