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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | <?php /** * Parses Git configuration files * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2011 Christopher Han * @package GitPHP * @subpackage Git */ class GitPHP_GitConfig { /** * Default config value type (no conversion) * * @var int */ const TypeDefault = 1; /** * Integer config value type * * @var int */ const TypeInteger = 2; /** * Boolean config value type * * @var int */ const TypeBoolean = 3; /** * The config file path * * @var string */ protected $configPath = null; /** * Whether the config file has been loaded * * @var string */ protected $configRead = false; /** * Config values * * @var array */ protected $config = array(); /** * Instantiates object * * @param string $configPath config file path */ public function __construct($configPath) { if (!is_readable($configPath)) { throw new Exception('Git config file not readable'); } $this->configPath = $configPath; } /** * Gets a config value * * @param string $key config key * @param boolean $multiValue true to support multivalue config variables * @param int $forceType force interpretation as a certain type * @return mixed config value */ public function GetValue($key, $multiValue = false, $forceType = null) { if (empty($key)) { return null; } if (!$this->configRead) $this->LoadConfig(); if (!isset($this->config[$key])) { return null; } $values = array(); $valueType = null; if ($forceType !== null) { $valueType = $forceType; } else { $valueType = GitPHP_GitConfig::GetType($key); } if ($valueType != GitPHP_GitConfig::TypeDefault) { // type conversion foreach ($this->config[$key] as $value) { switch ($valueType) { case GitPHP_GitConfig::TypeInteger: $value = intval($value); break; case GitPHP_GitConfig::TypeBoolean: $value = GitPHP_GitConfig::ToBool($value); break; } $values[] = $value; } } else { $values = $this->config[$key]; } if (!$multiValue) { // single value return $values[0]; } else { // multivalue return $values; } } /** * Tests if a config value exists * * @param string $key config key * @return boolean true if value exists */ public function HasValue($key) { if (empty($key)) { return false; } if (!$this->configRead) $this->LoadConfig(); return isset($this->config[$key]); } /** * Loads the config data */ private function LoadConfig() { $this->configRead = true; $data = explode("\n", file_get_contents($this->configPath)); $currentSection = ''; $currentSetting = ''; foreach ($data as $line) { $trimmed = trim($line); if (empty($trimmed)) { continue; } if (preg_match('/^\[(.+)\]$/', $trimmed, $regs)) { // section $currentSection = ''; $currentSetting = ''; $trimmedSection = trim($regs[1]); if (preg_match('/^([0-9A-Za-z\.\-]+)( "(.+)")?$/', $trimmedSection, $subRegs)) { $currentSection = strtolower($subRegs[1]); if (!empty($subRegs[3])) { // subsection $currentSection .= '.' . $subRegs[3]; } } } else if (!empty($currentSection)) { // variable $currentSetting .= $trimmed; if (substr($trimmed, -1) === '\\') { // continued on next line continue; } $key = ''; $value = null; $eq = strpos($currentSetting, '='); if ($eq !== false) { // key value pair $key = GitPHP_GitConfig::Unescape(trim(substr($currentSetting, 0, $eq))); $value = GitPHP_GitConfig::Unescape(trim(substr($currentSetting, $eq+1))); } else { // no equals is assumed true $key = GitPHP_GitConfig::Unescape($currentSetting); $value = "true"; } if (!empty($key)) { $fullSetting = $currentSection . '.' . strtolower($key); $this->config[$fullSetting][] = $value; } $currentSetting = ''; } } } /** * Gets the default type of a config value * * @param string $key config key * @return int config value type */ protected static function GetType($key) { switch ($key) { case 'gitphp.compat': return GitPHP_GitConfig::TypeBoolean; case 'core.abbrev': return GitPHP_GitConfig::TypeInteger; } return GitPHP_GitConfig::TypeDefault; } /** * Interpret git config boolean values * * @param mixed $value value to interpret * @return boolean boolean interpretation */ protected static function ToBool($value) { // true/false if (strncasecmp($value, 'true', 4) === 0) { return true; } if (strncasecmp($value, 'false', 5) === 0) { return false; } // on/off if (strncasecmp($value, 'on', 2) === 0) { return true; } if (strncasecmp($value, 'off', 3) === 0) { return false; } // 1/0 is handled by normal type conversion return (bool)$value; } /** * Parses escaped values and comments from git configs * * @param string $value value * @return string unescaped value */ protected static function Unescape($value) { if (strlen($value) == 0) { return ''; } if ((substr($value, 0, 1) === '"') && (substr($value, -1) === '"')) { // escaped with quotes $value = substr($value, 1, -1); } else { // not quoted, strip comments $value = preg_replace('/(#|;).*$/', '', $value); } // replace backslashed chars $value = str_replace(array('\\\\', '\\"'), array('\\', '"'), $value); return $value; } } |