1: <?php
2: /**
3: * A parser for a dependency list from a PEAR server.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Pear
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Pear
12: */
13:
14: /**
15: * A parser for a dependency list from a PEAR server.
16: *
17: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Horde
23: * @package Pear
24: * @author Gunnar Wrobel <wrobel@pardus.de>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Pear
27: */
28: class Horde_Pear_Rest_Dependencies
29: {
30: /**
31: * The dependency list.
32: *
33: * @var array
34: */
35: private $_deps;
36:
37: /**
38: * Constructor.
39: *
40: * @param resource|string $txt The text document received from the server.
41: */
42: public function __construct($txt)
43: {
44: if (is_resource($txt)) {
45: rewind($txt);
46: $txt = stream_get_contents($txt);
47: }
48: if ($txt === false) {
49: $this->_deps = array();
50: } else {
51: $deps = @unserialize($txt);
52: if ($deps === false && $txt !== 'b:0;') {
53: throw new Horde_Pear_Exception(
54: sprintf('Unable to parse dependency response "%s"!', $txt)
55: );
56: }
57: $result = array();
58: if (isset($deps['required'])) {
59: foreach ($deps['required'] as $type => $required) {
60: $this->_convert($type, $required, 'no', $result);
61: }
62: }
63: if (isset($deps['optional'])) {
64: foreach ($deps['optional'] as $type => $optional) {
65: $this->_convert($type, $optional, 'yes', $result);
66: }
67: }
68: $this->_deps = $result;
69: }
70: }
71:
72: /**
73: * Convert the PEAR server response into an array that we would get when
74: * accessing the dependencies of a local package.xml via PEAR.
75: *
76: * @param string $type The dependency type.
77: * @param array $input The input array.
78: * @param string $optional Indicates if it is an optional dependency.
79: * @param array &$result The result array.
80: *
81: * @return NULL
82: */
83: private function _convert($type, $input, $optional, &$result)
84: {
85: if (in_array($type, array('package', 'extension'))
86: && !isset($input['name'])) {
87: foreach ($input as $element) {
88: $this->_convert($type, $element, $optional, $result);
89: }
90: } else {
91: Horde_Pear_Package_Dependencies::addDependency(
92: $input, $type, $optional, $result
93: );
94: }
95:
96: }
97:
98: /**
99: * Return the package name.
100: *
101: * @return string The package name.
102: */
103: public function getDependencies()
104: {
105: return $this->_deps;
106: }
107: }