Bearsampp 2026.9.23
Loading...
Searching...
No Matches
PEAR_PackageFile Class Reference

Public Member Functions

 __construct (&$config, $debug=false)
 _extractErrors ($err=null)
& factory ($version)
& fromAnyFile ($info, $state)
& fromArray ($arr)
& fromPackageFile ($descfile, $state, $archive=false)
& fromTgzFile ($file, $state)
& fromXmlString ($data, $state, $file, $archive=false)
 getClassPrefix ()
& parserFactory ($version)
 rawReturn ()
 setLogger (&$l)

Static Public Member Functions

static addTempFile ($file)

Data Fields

 $_config
 $_debug
 $_extractErrors = array()
 $_logger = false
 $_rawReturn = false

Detailed Description

Definition at line 41 of file PackageFile.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( & $config,
$debug = false )
Parameters
PEAR_Config$config
?$debug
string@tmpdir Optional temporary directory for uncompressing files

Definition at line 69 of file PackageFile.php.

70 {
71 $this->_config = $config;
72 $this->_debug = $debug;
73 }
$config
Definition pearcmd.php:182

References $config.

Member Function Documentation

◆ _extractErrors()

_extractErrors ( $err = null)

helper callback for extracting Archive_Tar errors

Parameters
PEAR_Error | null$err
Returns
array @access private

Definition at line 388 of file PackageFile.php.

389 {
390 static $errors = array();
391 if ($err === null) {
392 $e = $errors;
393 $errors = array();
394 return $e;
395 }
396 $errors[] = $err->getMessage();
397 }

Referenced by fromTgzFile().

Here is the caller graph for this function:

◆ addTempFile()

addTempFile ( $file)
static

Register a temporary file or directory. When the destructor is executed, all registered temporary files and directories are removed.

Parameters
string$filename of file or directory
Returns
void

Definition at line 286 of file PackageFile.php.

287 {
288 $GLOBALS['_PEAR_Common_tempfiles'][] = $file;
289 }
$GLOBALS['_PEAR_default_error_mode']
Definition PEAR.php:46

References $GLOBALS.

Referenced by fromTgzFile().

Here is the caller graph for this function:

◆ factory()

& factory ( $version)

Create a PEAR_PackageFile_v* of a given version.

Parameters
int$version
Returns
PEAR_PackageFile_v1|PEAR_PackageFile_v1

Definition at line 123 of file PackageFile.php.

124 {
125 if (!in_array($version[0], array('1', '2'))) {
126 $a = false;
127 return $a;
128 }
129
130 include_once 'PEAR/PackageFile/v' . $version[0] . '.php';
131 $version = $version[0];
132 $class = $this->getClassPrefix() . $version;
133 $a = new $class;
134 return $a;
135 }

References getClassPrefix().

Referenced by fromArray().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromAnyFile()

& fromAnyFile ( $info,
$state )

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.

@access public

Parameters
string$infofile name
int$statepackage state (one of PEAR_VALIDATE_* constants)
Returns
PEAR_PackageFile_v1|PEAR_PackageFile_v2 @uses fromPackageFile() if the file appears to be XML @uses fromTgzFile() to load all non-XML files

Definition at line 445 of file PackageFile.php.

446 {
447 if (is_dir($info)) {
448 $dir_name = realpath($info);
449 if (file_exists($dir_name . '/package.xml')) {
450 $info = PEAR_PackageFile::fromPackageFile($dir_name . '/package.xml', $state);
451 } elseif (file_exists($dir_name . '/package2.xml')) {
452 $info = PEAR_PackageFile::fromPackageFile($dir_name . '/package2.xml', $state);
453 } else {
454 $info = PEAR::raiseError("No package definition found in '$info' directory");
455 }
456
457 return $info;
458 }
459
460 $fp = false;
461 if (is_string($info) && strlen($info) < 255 &&
462 (file_exists($info) || ($fp = @fopen($info, 'r')))
463 ) {
464
465 if ($fp) {
466 fclose($fp);
467 }
468
469 $tmp = substr($info, -4);
470 if ($tmp == '.xml') {
471 $info = &PEAR_PackageFile::fromPackageFile($info, $state);
472 } elseif ($tmp == '.tar' || $tmp == '.tgz') {
473 $info = &PEAR_PackageFile::fromTgzFile($info, $state);
474 } else {
475 $fp = fopen($info, 'r');
476 $test = fread($fp, 5);
477 fclose($fp);
478 if ($test == '<?xml') {
479 $info = &PEAR_PackageFile::fromPackageFile($info, $state);
480 } else {
481 $info = &PEAR_PackageFile::fromTgzFile($info, $state);
482 }
483 }
484
485 return $info;
486 }
487
488 $info = PEAR::raiseError("Cannot open '$info' for parsing");
489 return $info;
490 }
& fromPackageFile($descfile, $state, $archive=false)
& fromTgzFile($file, $state)

References fromPackageFile(), and fromTgzFile().

Here is the call graph for this function:

◆ fromArray()

& fromArray ( $arr)

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.

Parameters
array$arr
Returns
PEAR_PackageFileManager_v1|PEAR_PackageFileManager_v2 @uses factory() to construct the returned object.

Definition at line 146 of file PackageFile.php.

147 {
148 if (isset($arr['xsdversion'])) {
149 $obj = &$this->factory($arr['xsdversion']);
150 if ($this->_logger) {
151 $obj->setLogger($this->_logger);
152 }
153
154 $obj->setConfig($this->_config);
155 $obj->fromArray($arr);
156 return $obj;
157 }
158
159 if (isset($arr['package']['attribs']['version'])) {
160 $obj = &$this->factory($arr['package']['attribs']['version']);
161 } else {
162 $obj = &$this->factory('1.0');
163 }
164
165 if ($this->_logger) {
166 $obj->setLogger($this->_logger);
167 }
168
169 $obj->setConfig($this->_config);
170 $obj->fromArray($arr);
171 return $obj;
172 }
& factory($version)

References factory().

Here is the call graph for this function:

◆ fromPackageFile()

& fromPackageFile ( $descfile,
$state,
$archive = false )

Create a PEAR_PackageFile_v* from a package.xml file.

@access public

Parameters
string$descfilename of package xml file
int$statepackage state (one of PEAR_VALIDATE_* constants)
string | false$archivename of the archive this package.xml came from, if any
Returns
PEAR_PackageFile_v1|PEAR_PackageFile_v2 @uses PEAR_PackageFile\fromXmlString to create the oject after the XML is loaded from the package.xml file.

Definition at line 411 of file PackageFile.php.

412 {
413 $fp = false;
414 if (is_string($descfile) && strlen($descfile) < 255 &&
415 (
416 !file_exists($descfile) || !is_file($descfile) || !is_readable($descfile)
417 || (!$fp = @fopen($descfile, 'r'))
418 )
419 ) {
420 $a = PEAR::raiseError("Unable to open $descfile");
421 return $a;
422 }
423
424 // read the whole thing so we only get one cdata callback
425 // for each block of cdata
426 fclose($fp);
427 $data = file_get_contents($descfile);
428 $ret = &PEAR_PackageFile::fromXmlString($data, $state, $descfile, $archive);
429 return $ret;
430 }
& fromXmlString($data, $state, $file, $archive=false)

References fromXmlString().

Referenced by fromAnyFile(), and fromTgzFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromTgzFile()

& fromTgzFile ( $file,
$state )

Create a PEAR_PackageFile_v* from a compressed Tar or Tgz file. @access public

Parameters
stringcontents of package.xml file
intpackage state (one of PEAR_VALIDATE_* constants)
Returns
PEAR_PackageFile_v1|PEAR_PackageFile_v2 @using Archive_Tar to extract the files @using fromPackageFile() to load the package after the package.xml file is extracted.

Definition at line 301 of file PackageFile.php.

302 {
303 if (!class_exists('Archive_Tar')) {
304 require_once 'Archive/Tar.php';
305 }
306
307 $tar = new Archive_Tar($file);
308 if ($this->_debug <= 1) {
309 $tar->pushErrorHandling(PEAR_ERROR_RETURN);
310 }
311
312 $content = $tar->listContent();
313 if ($this->_debug <= 1) {
314 $tar->popErrorHandling();
315 }
316
317 if (!is_array($content)) {
318 if (is_string($file) && strlen($file) < 255 &&
319 (!file_exists($file) || !@is_file($file))) {
320 $ret = PEAR::raiseError("could not open file \"$file\"");
321 return $ret;
322 }
323
324 $file = realpath($file);
325 $ret = PEAR::raiseError("Could not get contents of package \"$file\"".
326 '. Invalid tgz file.');
327 return $ret;
328 }
329
330 if (!count($content) && !@is_file($file)) {
331 $ret = PEAR::raiseError("could not open file \"$file\"");
332 return $ret;
333 }
334
335 $xml = null;
336 $origfile = $file;
337 foreach ($content as $file) {
338 $name = $file['filename'];
339 if ($name == 'package2.xml') { // allow a .tgz to distribute both versions
340 $xml = $name;
341 break;
342 }
343
344 if ($name == 'package.xml') {
345 $xml = $name;
346 break;
347 } elseif (preg_match('/package.xml$/', $name, $match)) {
348 $xml = $name;
349 break;
350 }
351 }
352
353 $tmpdir = System::mktemp('-t "' . $this->_config->get('temp_dir') . '" -d pear');
354 if ($tmpdir === false) {
355 $ret = PEAR::raiseError("there was a problem with getting the configured temp directory");
356 return $ret;
357 }
358
360
361 $this->_extractErrors();
362 PEAR::staticPushErrorHandling(PEAR_ERROR_CALLBACK, array($this, '_extractErrors'));
363
364 if (!$xml || !$tar->extractList(array($xml), $tmpdir)) {
365 $extra = implode("\n", $this->_extractErrors());
366 if ($extra) {
367 $extra = ' ' . $extra;
368 }
369
371 $ret = PEAR::raiseError('could not extract the package.xml file from "' .
372 $origfile . '"' . $extra);
373 return $ret;
374 }
375
377 $ret = &PEAR_PackageFile::fromPackageFile("$tmpdir/$xml", $state, $origfile);
378 return $ret;
379 }
const PEAR_ERROR_RETURN
Definition PEAR.php:24
const PEAR_ERROR_CALLBACK
Definition PEAR.php:28
_extractErrors($err=null)
static addTempFile($file)
static staticPopErrorHandling()
Definition PEAR.php:654
static staticPushErrorHandling($mode, $options=null)
Definition PEAR.php:619
static mktemp($args=null)
Definition System.php:395

References _extractErrors(), addTempFile(), fromPackageFile(), PEAR_ERROR_CALLBACK, and PEAR_ERROR_RETURN.

Referenced by fromAnyFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromXmlString()

& fromXmlString ( $data,
$state,
$file,
$archive = false )

Create a PEAR_PackageFile_v* from an XML string. @access public

Parameters
string$datacontents of package.xml file
int$statepackage state (one of PEAR_VALIDATE_* constants)
string$filefull path to the package.xml file (and the files it references)
string$archiveoptional name of the archive that the XML was extracted from, if any
Returns
PEAR_PackageFile_v1|PEAR_PackageFile_v2 @uses parserFactory() to construct a parser to load the package.

Definition at line 186 of file PackageFile.php.

187 {
188 if (preg_match('/<package[^>]+version=[\'"]([0-9]+\.[0-9]+)[\'"]/', $data, $packageversion)) {
189 if (!in_array($packageversion[1], array('1.0', '2.0', '2.1'))) {
190 return PEAR::raiseError('package.xml version "' . $packageversion[1] .
191 '" is not supported, only 1.0, 2.0, and 2.1 are supported.');
192 }
193
194 $object = &$this->parserFactory($packageversion[1]);
195 if ($this->_logger) {
196 $object->setLogger($this->_logger);
197 }
198
199 $object->setConfig($this->_config);
200 $pf = $object->parse($data, $file, $archive);
201 if (PEAR::isError($pf)) {
202 return $pf;
203 }
204
205 if ($this->_rawReturn) {
206 return $pf;
207 }
208
209 if (!$pf->validate($state)) {;
210 if ($this->_config->get('verbose') > 0
211 && $this->_logger && $pf->getValidationWarnings(false)
212 ) {
213 foreach ($pf->getValidationWarnings(false) as $warning) {
214 $this->_logger->log(0, 'ERROR: ' . $warning['message']);
215 }
216 }
217
218 $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
219 2, null, null, $pf->getValidationWarnings());
220 return $a;
221 }
222
223 if ($this->_logger && $pf->getValidationWarnings(false)) {
224 foreach ($pf->getValidationWarnings() as $warning) {
225 $this->_logger->log(0, 'WARNING: ' . $warning['message']);
226 }
227 }
228
229 if (method_exists($pf, 'flattenFilelist')) {
230 $pf->flattenFilelist(); // for v2
231 }
232
233 return $pf;
234 } elseif (preg_match('/<package[^>]+version=[\'"]([^"\']+)[\'"]/', $data, $packageversion)) {
235 $a = PEAR::raiseError('package.xml file "' . $file .
236 '" has unsupported package.xml <package> version "' . $packageversion[1] . '"');
237 return $a;
238 } else {
239 if (!class_exists('PEAR_ErrorStack')) {
240 require_once 'PEAR/ErrorStack.php';
241 }
242
243 PEAR_ErrorStack::staticPush('PEAR_PackageFile',
245 'warning', array('xml' => $data), 'package.xml "' . $file .
246 '" has no package.xml <package> version');
247 $object = &$this->parserFactory('1.0');
248 $object->setConfig($this->_config);
249 $pf = $object->parse($data, $file, $archive);
250 if (PEAR::isError($pf)) {
251 return $pf;
252 }
253
254 if ($this->_rawReturn) {
255 return $pf;
256 }
257
258 if (!$pf->validate($state)) {
259 $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
260 2, null, null, $pf->getValidationWarnings());
261 return $a;
262 }
263
264 if ($this->_logger && $pf->getValidationWarnings(false)) {
265 foreach ($pf->getValidationWarnings() as $warning) {
266 $this->_logger->log(0, 'WARNING: ' . $warning['message']);
267 }
268 }
269
270 if (method_exists($pf, 'flattenFilelist')) {
271 $pf->flattenFilelist(); // for v2
272 }
273
274 return $pf;
275 }
276 }
const PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION
static staticPush( $package, $code, $level='error', $params=array(), $msg=false, $repackage=false, $backtrace=false)
& parserFactory($version)

References parserFactory(), and PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION.

Referenced by fromPackageFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getClassPrefix()

getClassPrefix ( )

For simpler unit-testing

Returns
string

Definition at line 113 of file PackageFile.php.

114 {
115 return 'PEAR_PackageFile_v';
116 }

Referenced by factory().

Here is the caller graph for this function:

◆ parserFactory()

& parserFactory ( $version)

Create a PEAR_PackageFile_Parser_v* of a given version.

Parameters
int$version
Returns
PEAR_PackageFile_Parser_v1|PEAR_PackageFile_Parser_v1

Definition at line 95 of file PackageFile.php.

96 {
97 if (!in_array($version[0], array('1', '2'))) {
98 $a = false;
99 return $a;
100 }
101
102 include_once 'PEAR/PackageFile/Parser/v' . $version[0] . '.php';
103 $version = $version[0];
104 $class = "PEAR_PackageFile_Parser_v$version";
105 $a = new $class;
106 return $a;
107 }

Referenced by fromXmlString().

Here is the caller graph for this function:

◆ rawReturn()

rawReturn ( )

Turn off validation - return a parsed package.xml without checking it

This is used by the package-validate command

Definition at line 80 of file PackageFile.php.

81 {
82 $this->_rawReturn = true;
83 }

◆ setLogger()

setLogger ( & $l)

Definition at line 85 of file PackageFile.php.

86 {
87 $this->_logger = &$l;
88 }

Field Documentation

◆ $_config

$_config

Definition at line 46 of file PackageFile.php.

◆ $_debug

$_debug

Definition at line 47 of file PackageFile.php.

◆ $_extractErrors

$_extractErrors = array()

Definition at line 60 of file PackageFile.php.

◆ $_logger

$_logger = false

Definition at line 49 of file PackageFile.php.

◆ $_rawReturn

$_rawReturn = false

Definition at line 53 of file PackageFile.php.


The documentation for this class was generated from the following file:
  • E:/Bearsampp-development/sandbox/core/libs/php/pear/pear/PEAR/PackageFile.php