home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / PEAR / Registry.php < prev    next >
PHP Script  |  2001-11-13  |  5KB  |  176 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Registry.php,v 1.9.2.1 2001/11/13 01:26:49 ssb Exp $
  21.  
  22. require_once "System.php";
  23.  
  24. /**
  25.  * Administration class used to maintain the installed package database.
  26.  */
  27. class PEAR_Registry
  28. {
  29.     // {{{ properties
  30.  
  31.     var $statedir;
  32.  
  33.     // }}}
  34.  
  35.     // {{{ PEAR_Registry
  36.  
  37.     function PEAR_Registry()
  38.     {
  39.         $this->statedir = PEAR_INSTALL_DIR . "/.registry";
  40.     }
  41.  
  42.     // }}}
  43.  
  44.     // {{{ _assertStateDir()
  45.  
  46.     function _assertStateDir()
  47.     {
  48.         if (!@is_dir($this->statedir)) {
  49.             System::mkdir("-p {$this->statedir}");
  50.         }
  51.     }
  52.  
  53.     // }}}
  54.     // {{{ _packageFileName()
  55.  
  56.     function _packageFileName($package)
  57.     {
  58.         return "{$this->statedir}/{$package}.reg";
  59.     }
  60.  
  61.     // }}}
  62.     // {{{ _openPackageFile()
  63.  
  64.     function _openPackageFile($package, $mode)
  65.     {
  66.         $this->_assertStateDir();
  67.         $file = $this->_packageFileName($package);
  68.         $fp = @fopen($file, $mode);
  69.         if (!$fp) {
  70.             return null;
  71.         }
  72.         return $fp;
  73.     }
  74.  
  75.     // }}}
  76.     // {{{ _closePackageFile()
  77.  
  78.     function _closePackageFile($fp)
  79.     {
  80.         fclose($fp);
  81.     }
  82.  
  83.     // }}}
  84.  
  85.     // {{{ packageExists()
  86.  
  87.     function packageExists($package)
  88.     {
  89.         return file_exists($this->_packageFileName($package));
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ addPackage()
  94.  
  95.     function addPackage($package, $info)
  96.     {
  97.         if ($this->packageExists($package)) {
  98.             return false;
  99.         }
  100.         $fp = $this->_openPackageFile($package, "w");
  101.         if ($fp === null) {
  102.             return false;
  103.         }
  104.         fwrite($fp, serialize($info));
  105.         $this->_closePackageFile($fp);
  106.         return true;
  107.     }
  108.  
  109.     // }}}
  110.     // {{{ packageInfo()
  111.  
  112.     function packageInfo($package = null)
  113.     {
  114.         if ($package === null) {
  115.             return array_map(array($this, "packageInfo"),
  116.                              $this->listPackages());
  117.         }
  118.         $fp = $this->_openPackageFile($package, "r");
  119.         if ($fp === null) {
  120.             return null;
  121.         }
  122.         $data = fread($fp, filesize($this->_packageFileName($package)));
  123.         $this->_closePackageFile($fp);
  124.         return unserialize($data);
  125.     }
  126.  
  127.     // }}}
  128.     // {{{ deletePackage()
  129.  
  130.     function deletePackage($package)
  131.     {
  132.         $file = $this->_packageFileName($package);
  133.         return @unlink($file);
  134.     }
  135.  
  136.     // }}}
  137.     // {{{ updatePackage()
  138.  
  139.     function updatePackage($package, $info)
  140.     {
  141.         $oldinfo = $this->packageInfo($package);
  142.         if (empty($oldinfo)) {
  143.             return false;
  144.         }
  145.         $fp = $this->_openPackageFile($package, "w");
  146.         if ($fp === null) {
  147.             return false;
  148.         }
  149.         fwrite($fp, serialize(array_merge($oldinfo, $info)));
  150.         $this->_closePackageFile($fp);
  151.         return true;
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ listPackages()
  156.  
  157.     function listPackages()
  158.     {
  159.         $pkglist = array();
  160.         $dp = @opendir($this->statedir);
  161.         if (!$dp) {
  162.             return $pkglist;
  163.         }
  164.         while ($ent = readdir($dp)) {
  165.             if ($ent{0} == "." || substr($ent, -4) != ".reg") {
  166.                 continue;
  167.             }
  168.             $pkglist[] = substr($ent, 0, -4);
  169.         }
  170.         return $pkglist;
  171.     }
  172.  
  173.     // }}}
  174. }
  175.  
  176. ?>