Skip to content

Classes

Mark Weiman edited this page Dec 30, 2017 · 9 revisions

This extension allows use of three types from libalpm directly. They are alpm_handle_t, alpm_db_t, and alpm_pkg_t. These three are used in the four classes of AlpmHandle, AlpmDb, AlpmPkg, and AlpmTransaction. Each class handles one of the types from libalpm.

php-alpm Class libalpm Type Used
AlpmHandle alpm_handle_t
AlpmDb alpm_db_t
AlpmPkg alpm_pkg_t
AlpmTransaction alpm_handle_t

AlpmHandle

This is the core of php-alpm, this class is where you start from. When you want to start using alpm, you create a new AlpmHandle object.

$handle_object = new AlpmHandle("/", "/var/lib/pacman");

In the constructor, it takes either two or zero parameters. The two parameters that are passed are the root directory of the pacman installation and the second is the database directory. On a default Arch Linux installation, this is / and /var/lib/pacman respectively. If you pass zero parameters, it assumes those two values.

/* these two statements do the same thing */
$handle_object = new AlpmHandle("/", "/var/lib/pacman");
$handle_object = new AlpmHandle();

More Information on AlpmHandle

AlpmDb

When the three functions AlpmHandle::get_localdb(), AlpmHandle::register_syncdb(), or AlpmHandle::get_syncdbs() are called, one or more AlpmDb objects may be created. This class manages the databases that libalpm uses to organize packages like [core], [extra], [community], and [local].

If you want information on installed packages on the system, you would use AlpmHandle::get_localdb() to get an AlpmDb object that manages it. There are some functions in AlpmDb that can only be used on local databases.

$handle_object = new AlpmHandle("/", "/var/lib/pacman");
$localdb = $handle_object->get_localdb();

Other functions are only available with databases that are "sync" databases. These databases include [core] and [extra]. To get a list of databases that are available from your shell, run the following shell command:

$ ls /var/lib/pacman/sync | grep .db$ | awk -F\. '{ print $1 }'

Example output:

alucryd-multilib
community
core
extra
markzz
multilib
xyne-x86_64

If you then want to use one of these databases, just run AlpmHandle::register_syncdb().

$handle_object = new AlpmHandle("/", "/var/lib/pacman");
$db_object = $handle_object->register_syncdb("core", ALPM_SIG_USE_DEFAULT);

Once you register a database, you cannot register it again otherwise AlpmHandle::register_syncdb() will throw an AlpmHandleException. If you lose a database object (if you overwrite it or delete it), you have to use AlpmHandle::get_syncdbs() which returns an array of AlpmDb objects.

$handle_object = new AlpmHandle("/", "/var/lib/pacman");
$db = $handle_object->register_syncdb("core", ALPM_SIG_USE_DEFAULT);
$db = $handle_object->register_syncdb("extra", ALPM_SIG_USE_DEFAULT);
/* oh, we need [core] back */
$db_list = $handle_object->get_syncdbs();
foreach ($db_list as $item) {
    if ($item->name == "core") {
        $db = $item; /* this sets $db back to the [core] database */
        break;
    }
}

More Information on AlpmDb

AlpmPkg

This class represents a package. Some functions from AlpmPkg are dependent on how the package is loaded. Some require you to have created the object from [local] and others don't.

There are many ways to create an AlpmPkg object, the main two are AlpmHandle::load_pkg() and AlpmDb::get_pkg() and are covered more on the AlpmPkg specific page.

More Information on AlpmPkg

AlpmTransaction

This class is to perform transactions (package installing, removing, etc). You must have permission to perform changes to the disk or AlpmTransactionException is thrown.

More Information on AlpmTransaction

Clone this wiki locally