summaryrefslogtreecommitdiffstats
path: root/examples/librpm.php
blob: b012e549a4e803e145aa7b7ba31029843a9dc320 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * Minimal userland OO library
 **/
namespace Remi\RPM;

abstract class Common {
	protected $info = NULL;

	protected function _dep(Array $names, Array $flags, Array $vers) {
		$ret = [];
		$signs = [
			RPMSENSE_EQUAL                    => ' = ',
			RPMSENSE_LESS                     => ' < ',
			RPMSENSE_GREATER                  => ' > ',
			RPMSENSE_LESS    | RPMSENSE_EQUAL => ' <= ',
			RPMSENSE_GREATER | RPMSENSE_EQUAL => ' >= ',
		];

		if (count($names) == count($vers) && count($vers) == count($flags)) {
			for ($i=0 ; $i<count($names) ; $i++) {
				if (isset($signs[$flags[$i] & 15])) {
					$ret[] = $names[$i] . $signs[$flags[$i] & 15] . $vers[$i];
				} else {
					$ret[] = $names[$i] . '('. ($flags[$i] & 15) .')' . $vers[$i];
				}
			}
		}
		return $ret;
	}

	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;
			case 'Requires':
				if (isset($this->info['Requirename'])) {
					return $this->_dep($this->info['Requirename'], $this->info['Requireflags'], $this->info['Requireversion']);
				}
			case 'Conflicts':
				if (isset($this->info['Conflictname'])) {
					return $this->_dep($this->info['Conflictname'], $this->info['Conflictflags'], $this->info['Conflictversion']);
				}
			case 'Obsoletes':
				if (isset($this->info['Obsoletename'])) {
					return $this->_dep($this->info['Obsoletename'], $this->info['Obsoleteflags'], $this->info['Obsoleteversion']);
				}
			case 'Provides':
				if (isset($this->info['Providename'])) {
					return $this->_dep($this->info['Providename'], $this->info['Provideflags'], $this->info['Provideversion']);
				}
			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];
		}
	}
}


/*
$a = new File(dirname(__DIR__).'/tests/bidon.rpm');
echo "Requires: ";  print_r($a->Requires);
echo "Provides: ";  print_r($a->Provides);
echo "Conflicts: "; print_r($a->Conflicts);
echo "Obsoletes: "; print_r($a->Obsoletes);
*/