As a continuation of the part 1 of exploring Pear registry, Demian suggested me to continue talking about more issues of Pear Registry class inspite of abording more pear packages. In fact, there is more than listing of packages and package info in pear registry, but it was the most useful part I think.

Pear registry is an administration class, used to maintain the installed package database. So in this second part I’ll try to explore all other features in PEAR registry.

Pear registry is based on 3 main elements : the registry folder, a mapping file and a lock file. The registry is a folder called ‘.registry’ by default and contain .reg files for each installed Pear package. Each registry file contain a serialized array which contain the different information about the package.

The map file contain a serialized array of the file list available in the PEAR install grouped with the path and filename as key and the package name as value for example :


include 'PEAR/Registry.php';
$registry = new PEAR_Registry("c:/php4/PEAR");
$registry->readFileMap();
print_r($registry->filemap_cache);

Will return

Array
(
[ArchiveTar.php] => archive_tar
[dirtree] => xml_rpc
[Console/Getopt.php] => console_getopt
[PEAR.php] => pear
[System.php] => pear
[PEAR/Autoloader.php] => pear
[PEAR/Command.php] => pear
[PEAR/Command/Auth.php] => pear
[PEAR/Command/Build.php] => pear
.........

If the map file is still empty you can generate the map file using rebuildFileMap

include 'PEAR/Registry.php';
$registry = new PEAR_Registry("c:/php4/PEAR");
$registry->rebuildFileMap();

The next useful feature in the PEAR registry is the locking system. It allow the system to lock a file for an exclusive write permission. Its a very important feature in a registry class so you cannot change the registry database while while it is doing a write action.

We talked last time about methods to retrieve informations, There is also methods to write into the registry database : addPackage(), delete a package : deletePackage(), and updage a pear package : updatePackage();

The last method is checkFileMap() which test whether a file belongs to a package or no. By entering file path, absolute or relative to the pear install dir, it returns which package the file belongs to, or an empty string if the file does not belong to an installed package.


include 'PEAR/Registry.php';
$registry = new PEAR_Registry("c:/php4/PEAR");
$package = $registry->checkFileMap('Console/Getopt.php');
echo $package;

will print

console_getopt

I’ll come back again to PEAR Registry when i’ll talk about PEAR Manager, but for now I think that I have all said about this small and very useful class.