summaryrefslogtreecommitdiffstats
path: root/examples/librpm.php
blob: 90082e0c8a045f8b51b5619fa691eeec4ce6bf55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
 * Minimal userland OO library
 **/
namespace Remi\RPM;

abstract class Common {
	protected $info = NULL;

	public function __get($name) {
		switch ($name) {
			case 'EVR':
			case 'NEVR':
			case 'NEVRA':
				$ret = '';
				if (substr($name, 0, 1) === 'N') {
					$ret = $this->info['Name'] . '-';
				}
				if (isset($this->info['Epoch'])) {
					$ret .= $this->info['Epoch'] . ':';
				}
				$ret .= $this->info['Version'] . '-' . $this->info['Release'];

				if (substr($name, -1) === 'A') {
					$ret .= '.' . $this->info['Arch'];
				}
				return $ret;
			default:
				if (isset($this->info[$name])) {
					return $this->info[$name];
				}
				return NULL;
		}
	}
}

/**
 * From a RPM file
 *
 * $a = new \Remi\RPM\File(dirname(__DIR__) . '/tests/bidon.rpm');
 * var_dump($a->Vendor);
 * var_dump($a->NEVRA);
 **/
class File extends Common {

	public function __construct($path) {
		$info = rpminfo($path, true, $error);

		if ($error) {
			throw new \RuntimeException($error);
		} else {
			$this->info = $info;
		}
	}
}

/**
 * From an installed RPM
 *
 * $a = new \Remi\RPM\Package('php');
 * var_dump($a->License);
 * var_dump($a->NEVRA);
 **/
class Package extends Common {

	public function __construct($name, $index=0) {
		$info = rpmdbinfo($name, true);

		if (!$info) {
			throw new \RuntimeException("$name not found");
		} else if ($index < 0 || $index >= count($info)) {
			throw new \OutOfBoundsException("$index not in range");
		} else {
			$this->info = $info[$index];
		}
	}
}