Source for file PackageFile.php
Documentation is available at PackageFile.php
* PEAR_PackageFile, package.xml parsing utility class
* @author Greg Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: PackageFile.php 313024 2011-07-06 19:51:24Z dufuz $
* @link http://pear.php.net/package/PEAR
* @since File available since Release 1.4.0a1
* needed for PEAR_VALIDATE_* constants
require_once 'PEAR/Validate.php';
* Error code if the package.xml <package> tag does not contain a valid version
define('PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION', 1 );
* Error code if the package.xml <package> tag version is not supported (version 1.0 and 1.1 are the only supported versions,
define('PEAR_PACKAGEFILE_ERROR_INVALID_PACKAGEVERSION', 2 );
* Abstraction for the package.xml package description file
* @author Greg Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version Release: 1.9.4
* @link http://pear.php.net/package/PEAR
* @since Class available since Release 1.4.0a1
* helper for extracting Archive_Tar errors
var $_extractErrors = array ();
* @param PEAR_Config $config
* @param string @tmpdir Optional temporary directory for uncompressing
$this->_config = $config;
* Turn off validation - return a parsed package.xml without checking it
* This is used by the package-validate command
$this->_rawReturn = true;
* Create a PEAR_PackageFile_Parser_v* of a given version.
* @return PEAR_PackageFile_Parser_v1|PEAR_PackageFile_Parser_v1
if (!in_array($version{0 }, array ('1', '2'))) {
include_once 'PEAR/PackageFile/Parser/v' . $version{0 } . '.php';
$class = " PEAR_PackageFile_Parser_v$version";
* For simpler unit-testing
return 'PEAR_PackageFile_v';
* Create a PEAR_PackageFile_v* of a given version.
* @return PEAR_PackageFile_v1|PEAR_PackageFile_v1
if (!in_array($version{0 }, array ('1', '2'))) {
include_once 'PEAR/PackageFile/v' . $version{0 } . '.php';
* Create a PEAR_PackageFile_v* from its toArray() method
* WARNING: no validation is performed, the array is assumed to be valid,
* always parse from xml if you want validation.
* @return PEAR_PackageFileManager_v1|PEAR_PackageFileManager_v2
* @uses factory() to construct the returned object.
if (isset ($arr['xsdversion'])) {
$obj = &$this->factory($arr['xsdversion']);
$obj->setLogger ($this->_logger);
$obj->setConfig ($this->_config);
if (isset ($arr['package']['attribs']['version'])) {
$obj = &$this->factory($arr['package']['attribs']['version']);
$obj->setLogger ($this->_logger);
$obj->setConfig ($this->_config);
* Create a PEAR_PackageFile_v* from an XML string.
* @param string $data contents of package.xml file
* @param int $state package state (one of PEAR_VALIDATE_* constants)
* @param string $file full path to the package.xml file (and the files
* @param string $archive optional name of the archive that the XML was
* @return PEAR_PackageFile_v1|PEAR_PackageFile_v2
* @uses parserFactory() to construct a parser to load the package.
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->setLogger ($this->_logger);
$object->setConfig ($this->_config);
$pf = $object->parse ($data, $file, $archive);
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 ());
if ($this->_logger && $pf->getValidationWarnings (false )) {
foreach ($pf->getValidationWarnings () as $warning) {
$this->_logger->log (0 , 'WARNING: ' . $warning['message']);
$pf->flattenFilelist (); // for v2
} elseif (preg_match('/<package[^>]+version=[\'"]([^"\']+)[\'"]/', $data, $packageversion)) {
'" has unsupported package.xml <package> version "' . $packageversion[1 ] . '"');
require_once 'PEAR/ErrorStack.php';
'warning', array ('xml' => $data), 'package.xml "' . $file .
'" has no package.xml <package> version');
$object->setConfig ($this->_config);
$pf = $object->parse ($data, $file, $archive);
if (!$pf->validate ($state)) {
$a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
2 , null , null , $pf->getValidationWarnings ());
if ($this->_logger && $pf->getValidationWarnings (false )) {
foreach ($pf->getValidationWarnings () as $warning) {
$this->_logger->log (0 , 'WARNING: ' . $warning['message']);
$pf->flattenFilelist (); // for v2
* Register a temporary file or directory. When the destructor is
* executed, all registered temporary files and directories are
* @param string $file name of file or directory
$GLOBALS['_PEAR_Common_tempfiles'][] = $file;
* Create a PEAR_PackageFile_v* from a compresed Tar or Tgz file.
* @param string contents of package.xml file
* @param int package state (one of PEAR_VALIDATE_* constants)
* @return PEAR_PackageFile_v1|PEAR_PackageFile_v2
* @using Archive_Tar to extract the files
* @using fromPackageFile() to load the package after the package.xml
require_once 'Archive/Tar.php';
$tar = new Archive_Tar ($file);
if ($this->_debug <= 1 ) {
$content = $tar->listContent ();
if ($this->_debug <= 1 ) {
$tar->popErrorHandling ();
$ret = PEAR::raiseError(" Could not get contents of package \"$file\"".
foreach ($content as $file) {
$name = $file['filename'];
if ($name == 'package2.xml') { // allow a .tgz to distribute both versions
if ($name == 'package.xml') {
} elseif (preg_match('/package.xml$/', $name, $match)) {
$tmpdir = System::mktemp('-t "' . $this->_config->get('temp_dir') . '" -d pear');
$ret = PEAR::raiseError("there was a problem with getting the configured temp directory");
if (!$xml || !$tar->extractList (array ($xml), $tmpdir)) {
$extra = implode("\n", $this->_extractErrors ());
$ret = PEAR::raiseError('could not extract the package.xml file from "' .
$origfile . '"' . $extra);
* helper callback for extracting Archive_Tar errors
* @param PEAR_Error|null$err
function _extractErrors ($err = null )
static $errors = array ();
$errors[] = $err->getMessage ();
* Create a PEAR_PackageFile_v* from a package.xml file.
* @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
* @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.
|| (!$fp = @fopen($descfile, 'r'))
// read the whole thing so we only get one cdata callback
// for each block of cdata
* Create a PEAR_PackageFile_v* from a .tgz archive or package.xml file.
* This method is able to extract information about a package from a .tgz
* archive or from a XML package definition file.
* @param string $info file name
* @param int $state package state (one of PEAR_VALIDATE_* constants)
* @return PEAR_PackageFile_v1|PEAR_PackageFile_v2
* @uses fromPackageFile() if the file appears to be XML
* @uses fromTgzFile() to load all non-XML files
$info = PEAR::raiseError(" No package definition found in '$info' directory" );
} elseif ($tmp == '.tar' || $tmp == '.tgz') {
Documentation generated on Wed, 06 Jul 2011 23:31:03 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|