Aggregate Code Coverage for all tests
1 : <?php 2 : /** 3 : * PEAR2_Pyrus_Package_Base 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 representing a package in 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 9 : abstract class PEAR2_Pyrus_Package_Base implements PEAR2_Pyrus_IPackage 27 : { 28 : protected $packagefile; 29 : /** 30 : * The original source of this package 31 : * 32 : * This is a chain documenting the steps it took to get this 33 : * package instantiated, for instance Tar->Abstract 34 : * @var PEAR2_Pyrus_IPackage 35 : */ 36 : protected $from; 37 : 38 : function __construct(PEAR2_Pyrus_PackageFile $packagefile, $parent = null) 39 : { 40 9 : $this->packagefile = $packagefile; 41 9 : $this->from = $parent; 42 9 : } 43 : 44 : /** 45 : * Used to determine whether <install as> tags are necessary for 46 : * PEAR2-style packages 47 : * 48 : * @return bool 49 : */ 50 : function isNewPackage() 51 : { 52 1 : return true; 53 : } 54 : 55 : function setFrom(PEAR2_Pyrus_IPackage $from) 56 : { 57 : $this->from = $from; 58 : } 59 : 60 : function getFrom() 61 : { 62 1 : if ($this->from) { 63 1 : return $this->from->getFrom(); 64 : } 65 : 66 : return $this; 67 : } 68 : 69 : /** 70 : * Sort files/directories for removal 71 : * 72 : * Files are always removed first, followed by directories in 73 : * path order 74 : * @param unknown_type $a 75 : * @param unknown_type $b 76 : * @return unknown 77 : */ 78 : static function sortstuff($a, $b) 79 : { 80 : // files can be removed in any order 81 : if (is_file($a) && is_file($b)) return 0; 82 : if (is_dir($a) && is_file($b)) return 1; 83 : if (is_dir($b) && is_file($a)) return -1; 84 : $countslasha = substr_count($a, DIRECTORY_SEPARATOR); 85 : $countslashb = substr_count($b, DIRECTORY_SEPARATOR); 86 : if ($countslasha > $countslashb) return -1; 87 : if ($countslashb > $countslasha) return 1; 88 : // if not subdirectories, tehy can be removed in any order 89 : return 0; 90 : } 91 : 92 : /** 93 : * Create vertices/edges of a directed graph for dependencies of this package 94 : * 95 : * Iterate over dependencies and create edges from this package to those it 96 : * depends upon 97 : * @param PEAR2_Pyrus_DirectedGraph $graph 98 : * @param array $packages channel/package indexed array of PEAR2_Pyrus_Package objects 99 : */ 100 : function makeConnections(PEAR2_Pyrus_DirectedGraph $graph, array $packages) 101 : { 102 1 : $graph->add($this->getFrom()); 103 1 : foreach (array('required', 'optional') as $required) { 104 1 : foreach (array('package', 'subpackage') as $package) { 105 1 : foreach ($this->dependencies->$required->$package as $d) { 106 : if (isset($d['conflicts'])) { 107 : continue; 108 : } 109 : 110 : $dchannel = isset($d['channel']) ? $d['channel'] : '__uri'; 111 : if (isset($packages[$dchannel . '/' . $d['name']])) { 112 : $graph->connect($this, $packages[$dchannel . '/' . $d['name']]); 113 : } 114 1 : } 115 1 : } 116 1 : } 117 : 118 1 : foreach ($this->dependencies->group as $group) { 119 : foreach (array('package', 'subpackage') as $package) { 120 : foreach ($group->$package as $d) { 121 : if (isset($d['conflicts'])) { 122 : continue; 123 : } 124 : 125 : $dchannel = isset($d['channel']) ? $d['channel'] : '__uri'; 126 : if (isset($packages[$dchannel . '/' . $d['name']])) { 127 : $graph->connect($this, $packages[$dchannel . '/' . $d['name']]); 128 : } 129 : } 130 : } 131 1 : } 132 1 : } 133 : 134 : function offsetExists($offset) 135 : { 136 : return $this->packagefile->info->hasFile($offset); 137 : } 138 : 139 : function offsetGet($offset) 140 : { 141 : if (strpos($offset, 'contents://') === 0) { 142 : return $this->getFileContents(substr($offset, 11)); 143 : } 144 : 145 : return $this->packagefile->info->getFile($offset); 146 : } 147 : 148 : function offsetSet($offset, $value) 149 : { 150 : return; 151 : } 152 : 153 : function offsetUnset($offset) 154 : { 155 : return; 156 : } 157 : 158 : function current() 159 : { 160 : return key($this->packagefile->info->_packageInfo['filelist']); 161 : } 162 : 163 : function key() 164 : { 165 : return 1; 166 : } 167 : 168 : function next() 169 : { 170 : next($this->packagefile->info->_packageInfo['filelist']); 171 : } 172 : 173 : function rewind() 174 : { 175 : reset($this->packagefile->info->_packageInfo['filelist']); 176 : } 177 : 178 : function getPackageFile() 179 : { 180 9 : return $this->packagefile; 181 : } 182 : 183 : function __call($func, $args) 184 : { 185 : // delegate to the internal object 186 3 : return call_user_func_array(array($this->packagefile->info, $func), $args); 187 : } 188 : 189 : function __get($var) 190 : { 191 8 : return $this->packagefile->info->$var; 192 : } 193 : 194 : function __toString() 195 : { 196 : return $this->packagefile->__toString(); 197 : } 198 : 199 : function valid() 200 : { 201 : return key($this->packagefile->info->_packageInfo['filelist']); 202 : } 203 : 204 : function getFileContents($file, $asstream = false) 205 : { 206 : return $asstream ? 207 : fopen($this->getFilePath($file), 'rb') : 208 : file_get_contents($this->getFilePath($file)); 209 : } 210 : }