Esempio n. 1
0
 /**
  * @param string
  * @param string file name of the channel.xml
  *
  * @return \Pyrus\ChannelFile\v1
  */
 function parse($data, $class = 'Pyrus\\ChannelFile\\v1')
 {
     $ret = new $class();
     if (!$ret instanceof \Pyrus\ChannelFile\v1) {
         throw new \Pyrus\ChannelFile\Exception('Class ' . $class . ' passed to parse() must be a child class of \\Pyrus\\ChannelFile\\v1');
     }
     $schema = \Pyrus\Main::getDataPath() . '/channel-1.0.xsd';
     try {
         $data = trim($data);
         if (substr($data, 0, 5) !== '<?xml' && file_exists($data)) {
             $ret->fromArray(parent::parse($data, $schema));
         } else {
             $ret->fromArray(parent::parseString($data, $schema));
         }
     } catch (\Exception $e) {
         throw new \Pyrus\ChannelFile\Exception('Invalid channel.xml', null, $e);
     }
     return $ret;
 }
Esempio n. 2
0
 /**
  * Parses a string containing package xml and returns an object
  *
  * @param string       $data  data to parse
  * @param string|false $file  name of the archive this package.xml came from, if any
  * @param string       $class class name to instantiate and return.
  *                            This must be Pyrus\PackageFile\v2 or a subclass
  * @param int          $state what state we are currently in
  *
  * @return \Pyrus\PackageFile\v2
  */
 function parse($data, $file = false, $class = 'Pyrus\\PackageFile\\v2', $state = \Pyrus\Validate::NORMAL)
 {
     $this->_inContents = false;
     $this->_path = '';
     $this->_files = array();
     $this->_lastDepth = $this->_lastFileDepth = 0;
     $this->_inFile = 0;
     $ret = new $class();
     if (!$ret instanceof \Pyrus\PackageFile\v2) {
         throw new \Pyrus\PackageFile\Exception('Class ' . $class . ' passed to parse() must be a child class of \\Pyrus\\PackageFile\\v2');
     }
     if (preg_match('/<package[^>]+version="2.1"/', $data)) {
         $schema = \Pyrus\Main::getDataPath() . '/package-2.1.xsd';
     } elseif (preg_match('/<package[^>]+version="2.0"/', $data)) {
         $schema = \Pyrus\Main::getDataPath() . '/package-2.0.xsd';
     } else {
         throw new \Pyrus\PackageFile\Exception('Cannot process package.xml version 1.0', -3);
     }
     try {
         $ret->fromArray(parent::parseString($data, $schema));
     } catch (\Exception $e) {
         throw new \Pyrus\PackageFile\Exception('Invalid package.xml', $e);
     }
     $ret->setFileList($this->_files);
     $ret->setBaseInstallDirs($this->_baseinstalldirs);
     $ret->setPackagefile($file);
     return $ret;
 }