Aggregate Code Coverage for all tests
1 : <?php 2 : /** 3 : * PEAR2_Pyrus_ChannelRegistry 4 : * 5 : * PHP version 5 6 : * 7 : * @category PEAR2 8 : * @package PEAR2_Pyrus 9 : * @author Greg Beaver <cellog@php.net> 10 : * @copyright 2008 The PEAR Group 11 : * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 12 : * @version SVN: $Id$ 13 : * @link http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/ 14 : */ 15 : 16 : /** 17 : * Base class for Pyrus. 18 : * 19 : * @category PEAR2 20 : * @package PEAR2_Pyrus 21 : * @author Greg Beaver <cellog@php.net> 22 : * @copyright 2008 The PEAR Group 23 : * @license http://www.opensource.org/licenses/bsd-license.php New BSD License 24 : * @link http://svn.pear.php.net/wsvn/PEARSVN/Pyrus/ 25 : */ 26 1 : class PEAR2_Pyrus_ChannelRegistry implements ArrayAccess, IteratorAggregate, PEAR2_Pyrus_IChannelRegistry 27 : { 28 : /** 29 : * Class to instantiate for singleton. 30 : * 31 : * This is useful for unit-testing and for extending the registry 32 : * @var string 33 : */ 34 : static public $className = 'PEAR2_Pyrus_ChannelRegistry'; 35 : /** 36 : * The parent registry 37 : * 38 : * This is used to implement cascading registries 39 : * @var PEAR2_Pyrus_ChannelRegistry 40 : */ 41 : protected $parent; 42 : protected $path; 43 : protected $readonly; 44 : private $_registries = array(); 45 : 46 : public function __construct($path, $registries = array('Sqlite3', 'Xml'), $readonly = false) 47 : { 48 1 : $this->path = $path; 49 1 : $this->readonly = $readonly; 50 1 : $exceptions = array(); 51 1 : foreach ($registries as $registry) { 52 : try { 53 1 : $registry = ucfirst($registry); 54 1 : $registry = "PEAR2_Pyrus_ChannelRegistry_$registry"; 55 1 : if (!class_exists($registry, true)) { 56 : $exceptions[] = new PEAR2_Pyrus_ChannelRegistry_Exception( 57 : 'Unknown channel registry type: ' . $registry); 58 : continue; 59 : } 60 1 : $this->_registries[] = new $registry($path, $readonly); 61 1 : } catch (PEAR2_Pyrus_ChannelRegistry_Exception $e) { 62 1 : $exceptions[] = $e; 63 : } 64 1 : } 65 1 : if (!count($this->_registries)) { 66 : throw new PEAR2_Pyrus_Registry_Exception( 67 : 'Unable to initialize registry for path "' . $path . '"', 68 : $exceptions); 69 : } 70 1 : } 71 : 72 : public function setParent(PEAR2_Pyrus_ChannelRegistry $parent = null) 73 : { 74 1 : $this->parent = $parent; 75 1 : } 76 : 77 : /** 78 : * Add a channel to the registry. 79 : * 80 : * @param PEAR2_Pyrus_IChannel $channel Channel to add. 81 : */ 82 : public function add(PEAR2_Pyrus_IChannel $channel) 83 : { 84 : if ($this->readonly) { 85 : throw new PEAR2_Pyrus_ChannelRegistry_Exception('Cannot add channel, registry is read-only'); 86 : } 87 : foreach ($this->_registries as $reg) { 88 : $reg->add($channel); 89 : } 90 : } 91 : 92 : public function update(PEAR2_Pyrus_IChannel $channel) 93 : { 94 : if ($this->readonly) { 95 : throw new PEAR2_Pyrus_ChannelRegistry_Exception('Cannot update channel, registry is read-only'); 96 : } 97 : foreach ($this->_registries as $reg) { 98 : $reg->update($channel); 99 : } 100 : } 101 : 102 : public function delete(PEAR2_Pyrus_IChannel $channel) 103 : { 104 : if ($this->readonly) { 105 : throw new PEAR2_Pyrus_ChannelRegistry_Exception('Cannot delete channel, registry is read-only'); 106 : } 107 : switch ($channel->name) { 108 : case 'pear.php.net' : 109 : case 'pear2.php.net' : 110 : case 'pecl.php.net' : 111 : case '__uri' : 112 : throw new PEAR2_Pyrus_ChannelRegistry_Exception('Cannot delete channel ' . 113 : $channel->name); 114 : } 115 : foreach ($this->_registries as $reg) { 116 : $reg->delete($channel); 117 : } 118 : } 119 : 120 : public function get($channel, $strict = true) 121 : { 122 : try { 123 : return $this->_registries[0]->get($channel, $strict); 124 : } catch (Exception $e) { 125 : // don't fail on the default channels, these should always exist 126 : switch ($channel) { 127 : case 'pear.php.net' : 128 : return $this->_registries[0]->getPearChannel(); 129 : case 'pear2.php.net' : 130 : return $this->_registries[0]->getPear2Channel(); 131 : case 'pecl.php.net' : 132 : return $this->_registries[0]->getPeclChannel(); 133 : case '__uri' : 134 : return $this->_registries[0]->getUriChannel(); 135 : } 136 : throw $e; 137 : } 138 : } 139 : 140 : /** 141 : * Check if channel has been discovered and in the registry. 142 : * 143 : * @param string $channel Channel name or alias: pear.php.net, pear 144 : * @param bool $strict Do not check aliases. 145 : * 146 : * @return bool 147 : */ 148 : public function exists($channel, $strict = true) 149 : { 150 : if (!$this->_registries[0]->exists($channel, $strict)) { 151 : switch ($channel) { 152 : case 'pear.php.net' : 153 : case 'pear2.php.net' : 154 : case 'pecl.php.net' : 155 : case '__uri' : 156 : return true; 157 : } 158 : if (!$strict) { 159 : switch ($channel) { 160 : case 'pear' : 161 : case 'pear2' : 162 : case 'pecl' : 163 : return true; 164 : } 165 : } 166 : return false; 167 : } 168 : return true; 169 : } 170 : 171 : public function parseName($name) 172 : { 173 : foreach ($this->_registries as $reg) { 174 : try { 175 : return $reg->parseName($name); 176 : } catch (Exception $e) { 177 : continue; 178 : } 179 : } 180 : if ($this->parent) { 181 : return $this->parent->parseName($name); 182 : } 183 : // recycle last exception 184 : throw new PEAR2_Pyrus_ChannelRegistry_Exception('Unable to process package name', $e); 185 : } 186 : 187 : public function parsedNameToString($name) 188 : { 189 : foreach ($this->_registries as $reg) { 190 : try { 191 : return $reg->parsedNameToString($name); 192 : } catch (Exception $e) { 193 : continue; 194 : } 195 : } 196 : if ($this->parent) { 197 : return $this->parent->parsedNameToString($name); 198 : } 199 : // recycle last exception 200 : throw new PEAR2_Pyrus_ChannelRegistry_Exception('Unable to convert to package name string', $e); 201 : } 202 : 203 : public function listChannels() 204 : { 205 : return $this->_registries[0]->listChannels(); 206 : } 207 : 208 : public function offsetGet($offset) 209 : { 210 : return $this->get($offset); 211 : } 212 : 213 : public function offsetSet($offset, $value) 214 : { 215 : if ($this->readonly) { 216 : throw new PEAR2_Pyrus_ChannelRegistry_Exception('Cannot add channel, registry is read-only'); 217 : } 218 : foreach ($this->_registries as $reg) { 219 : $reg->add($offset, $value); 220 : } 221 : } 222 : 223 : public function offsetExists($offset) 224 : { 225 : return $this->exists($offset); 226 : } 227 : 228 : public function offsetUnset($offset) 229 : { 230 : if ($this->readonly) { 231 : throw new PEAR2_Pyrus_ChannelRegistry_Exception('Cannot delete channel, registry is read-only'); 232 : } 233 : foreach ($this->_registries as $reg) { 234 : $reg->delete($offset); 235 : } 236 : } 237 : 238 : public function __call($method, $args) 239 : { 240 : return call_user_func_array(array($this->_registries[0], $method), $args); 241 : } 242 : 243 : public function getIterator() 244 : { 245 : return $this->_registries[0]; 246 : } 247 : 248 : public function getParent() 249 : { 250 : return $this->parent; 251 : } 252 : 253 : public function getPath() 254 : { 255 : return $this->path; 256 : } 257 : }