From: Tomas V.V.Cox Date: Thu, 19 Jul 2001 11:10:28 +0000 (+0000) Subject: This class will handle the pear packages database (it's experimental now) X-Git-Tag: PRE_TSRM_MERGE_PATCH~77 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e78a175fc03aef0fc3899e02c044db03e9bef2f3;p=php This class will handle the pear packages database (it's experimental now) --- diff --git a/pear/PEAR/Packlist.php b/pear/PEAR/Packlist.php new file mode 100644 index 0000000000..f898968f1b --- /dev/null +++ b/pear/PEAR/Packlist.php @@ -0,0 +1,254 @@ + | +// | | +// +----------------------------------------------------------------------+ +// +// $Id$ + +require_once "PEAR/Common.php"; + +/** + * Administration class used to maintain the installed package database. + * + * THIS CLASS IS EXPERIMENTAL + */ +class PEAR_Packlist +{ + /** directory where system state information goes */ + var $statedir; + + /** list of installed packages */ + var $pkglist = array(); + + /** temporary directory */ + var $tmpdir; + + /** file pointer for package list file if open */ + var $pkglist_fp; + + // {{{ constructor + + function PEAR_Packlist($phpdir = PEAR_INSTALL_DIR, + $extdir = PEAR_EXTENSION_DIR, + $docdir = '') + { + $this->PEAR(); + $this->phpdir = $phpdir; + $this->extdir = $extdir; + $this->docdir = $docdir; + $this->statedir = "/var/lib/php"; // XXX FIXME Windows + } + + // }}} + // {{{ destructor + + function _PEAR_Packlist() + { + if ($this->tmpdir && is_dir($this->tmpdir)) { + system("rm -rf $this->tmpdir"); // XXX FIXME Windows + } + if (isset($this->pkglist_fp) && is_resource($this->pkglist_fp)) { + flock($this->pkglist_fp, LOCK_UN); + fclose($this->pkglist_fp); + } + $this->tmpdir = null; + $this->pkglist_fp = null; + $this->_PEAR_Common(); + } + + // }}} + + // {{{ lockPackageList() + + function lockPackageList() + { + $fp = $this->pkglist_fp; + if (!is_resource($fp)) { + $this->pkglist_fp = $fp = fopen($this->pkglist_file, "r"); + } + return flock($fp, LOCK_EX); + } + + // }}} + // {{{ unlockPackageList() + + function unlockPackageList() + { + $fp = $this->pkglist_fp; + if (!is_resource($fp)) { + $this->pkglist_fp = $fp = fopen($this->pkglist_file, "r"); + $doclose = true; + } + $ret = flock($fp, LOCK_EX); + if ($doclose) { + fclose($fp); + } + return $ret; + } + + // }}} + // {{{ loadPackageList() + + function loadPackageList($file) + { + $this->pkglist_file = $file; + $this->pkglist = array(); + if (!file_exists($file)) { + if (!@touch($file)) { + return $this->raiseError("touch($file): $php_errormsg"); + } + } + $fp = $this->pkglist_fp = @fopen($file, "r"); + if (!is_resource($fp)) { + return $this->raiseError("fopen($file): $php_errormsg"); + } + $this->lockPackageList(); + $versionline = trim(fgets($fp, 2048)); + if ($versionline == ";1") { + while ($line = fgets($fp, 2048)) { + list($name, $version, $file) = explode(";", trim($line)); + $this->pkglist[$name]["version"] = $version; + $this->pkglist[$name]["files"][] = $file; + } + } + $this->unlockPackageList(); + } + + // }}} + // {{{ savePackageList() + + function savePackageList() + { + $fp = $this->pkglist_fp; + $wfp = @fopen($this->pkglist_file, "w"); + if (!is_resource($wfp)) { + return $this->raiseError("could not write $this->pkglist_file"); + } + if (is_resource($fp)) { + fclose($fp); + } + $this->pkglist_fp = $fp = $wfp; + fwrite($fp, ";1\n"); + foreach ($this->pkglist as $name => $entry) { + $ver = $entry["version"]; + foreach ($entry["files"] as $file) { + fwrite($fp, "$name;$ver;$file\n"); + } + } + fclose($fp); + $this->pkglist_fp = $fp = null; + } + + // }}} + // {{{ updatePackageListFrom() + + function updatePackageListFrom($file) + { + /* + $new = $this->classesDeclaredBy($file); + reset($new); + while (list($i, $name) = each($new)) { + $this->pkglist['class'][$name] = $file; + } + */ + } + + // }}} + // {{{ classesDeclaredBy() + + /** + * Find out which new classes are defined by a file. + * + * @param $file file name passed to "include" + * + * @return array classes that were defined + */ + function classesDeclaredBy($file) + { + $before = get_declared_classes(); + ob_start(); + include($file); + ob_end_clean(); + $after = get_declared_classes(); + // using array_slice to renumber array + $diff = array_slice(array_diff($after, $before), 0); + return $diff; + } + + // }}} + + // {{{ declaredWhenIncluding() + + /** + * Find out which new classes are defined by a file. + * + * @param $file file name passed to "include" + * + * @return array classes that were defined + */ + function &declaredWhenIncluding($file) + { + $classes_before = get_declared_classes(); + $funcs_before = get_defined_functions(); +// $vars_before = $GLOBALS; + ob_start(); + include($file); + ob_end_clean(); + $classes_after = get_declared_classes(); + $funcs_after = get_defined_functions(); +// $vars_after = $GLOBALS; + //using array_slice to renumber array + return array( + "classes" => array_slice(array_diff($classes_after, $classes_before), 0), + "functions" => array_slice(array_diff($funcs_after, $funcs_before), 0), +// "globals" => array_slice(array_diff($vars_after, $vars_before), 0) + ); + } + + // }}} + + // {{{ lockDir() + + /** + * Uses advisory locking (flock) to temporarily claim $dir as its + * own. + * + * @param $dir the directory to lock + * + * @return bool true if successful, false if not + */ + function lockDir($dir) + { + $lockfile = "$dir/.lock"; + if (!file_exists($lockfile)) { + if (!touch($lockfile)) { + // could not create lockfile! + return false; + } + } + $fp = fopen($lockfile, "r"); + if (!flock($fp, LOCK_EX)) { + // could not create lock! + return false; + } + return true; + } + + // }}} + +} +?> \ No newline at end of file