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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | <?php /** * Base class that all projectlist classes extend * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2010 Christopher Han * @package GitPHP * @subpackage Git\ProjectList */ abstract class GitPHP_ProjectListBase implements Iterator, GitPHP_Observable_Interface { /** * Project name sort * * @const */ const ProjectSort = 'project'; /** * Project description sort * * @const */ const DescriptionSort = 'descr'; /** * Project owner sort * * @const */ const OwnerSort = 'owner'; /** * Project age sort * * @const */ const AgeSort = 'age'; /** * Project list * * @var GitPHP_Project[] */ protected $projects; /** * Whether the list of projects has been loaded * * @var boolean */ protected $projectsLoaded = false; /** * The project configuration * * @var string */ protected $projectConfig = null; /** * Project settings * * @var array */ protected $projectSettings = null; /** * The project root * * @var string */ protected $projectRoot = null; /** * Object cache instance for all projects * * @var GitPHP_Cache */ protected $cache = null; /** * Memory cache instance for all projects * * @var GitPHP_MemoryCache */ protected $memoryCache = null; /** * Observers * * @var GitPHP_Observer_Interface[] */ protected $observers = array(); /** * Constructor * * @param string $projectRoot project root */ public function __construct($projectRoot) { $this->projects = array(); $this->projectRoot = GitPHP_Util::AddSlash($projectRoot); if (empty($this->projectRoot)) { throw new GitPHP_MessageException(__('A projectroot must be set in the config'), true, 500); } if (!is_dir($this->projectRoot)) { throw new GitPHP_MessageException(sprintf(__('%1$s is not a directory'), $this->projectRoot)); } $this->memoryCache = new GitPHP_MemoryCache(GitPHP_Config::GetInstance()->GetValue('objectmemory', 0)); if (GitPHP_Config::GetInstance()->GetValue('objectcache', false)) { $this->cache = new GitPHP_Cache(); $this->cache->SetServers(GitPHP_Config::GetInstance()->GetValue('memcache', null)); $this->cache->SetEnabled(true); $this->cache->SetLifetime(GitPHP_Config::GetInstance()->GetValue('objectcachelifetime', 86400)); } } /** * Get memory cache instance * * @access public * @return GitPHP_MemoryCache|null */ public function GetMemoryCache() { return $this->memoryCache; } /** * Set memory cache instance * * @access public * @param GitPHP_MemoryCache|null $memoryCache memory cache instance */ public function SetMemoryCache($memoryCache) { $this->memoryCache = $memoryCache; } /** * Get object cache instance * * @access public * @return GitPHP_Cache|null object cache */ public function GetCache() { return $this->cache; } /** * Set object cache instance * * @access public * @param GitPHP_Cache|null $cache object cache instance */ public function SetCache($cache) { $this->cache = $cache; } /** * Test if the projectlist contains the given project * * @return boolean true if project exists in list * @param string $project the project to find */ public function HasProject($project) { if (empty($project)) return false; return isset($this->projects[$project]); } /** * Gets a particular project * * @return GitPHP_Project|null project object or null * @param string $project the project to find */ public function GetProject($project) { if (empty($project)) return null; if (isset($this->projects[$project])) return $this->projects[$project]; if (!$this->projectsLoaded) { $projObj = $this->InstantiateProject($project); $this->projects[$project] = $projObj; return $projObj; } return null; } /** * Instantiates a project object * * @param string $proj project * @return return GitPHP_Project project object */ protected function InstantiateProject($proj) { $project = new GitPHP_Project(GitPHP_Util::AddSlash($this->projectRoot), $proj); $this->InjectProjectDependencies($project); $this->ApplyGlobalConfig($project); $this->ApplyGitConfig($project); if ($this->projectSettings && isset($this->projectSettings[$proj])) { $this->ApplyProjectSettings($project, $this->projectSettings[$proj]); } return $project; } /** * Inject project dependency objects * * @param GitPHP_Project $project project object */ protected function InjectProjectDependencies($project) { if (!$project) return; if ($this->memoryCache) { $project->GetObjectManager()->SetMemoryCache($this->memoryCache); } if ($this->cache) { $project->GetObjectManager()->SetCache($this->cache); } } /** * Gets the config defined for this ProjectList * * @return mixed project config */ public function GetConfig() { return $this->projectConfig; } /** * Gets the settings applied to this projectlist * * @return array */ public function GetSettings() { return $this->projectSettings; } /** * Reads the project's git config settings and applies them to the project * * @param GitPHP_Project $project project */ protected function ApplyGitConfig($project) { if (!$project) return; $config = null; try { $config = new GitPHP_GitConfig($project->GetPath() . '/config'); } catch (Exception $e) { return; } if ($config->HasValue('gitphp.owner')) { $project->SetOwner($config->GetValue('gitphp.owner')); } else if ($config->HasValue('gitweb.owner')) { $project->SetOwner($config->GetValue('gitweb.owner')); } if ($config->HasValue('gitphp.description')) { $project->SetDescription($config->GetValue('gitphp.description')); } if ($config->HasValue('gitphp.category')) { $project->SetCategory($config->GetValue('gitphp.category')); } if ($config->HasValue('gitphp.cloneurl')) { $project->SetCloneUrl($config->GetValue('gitphp.cloneurl')); } if ($config->HasValue('gitphp.pushurl')) { $project->SetPushUrl($config->GetValue('gitphp.pushurl')); } if ($config->HasValue('gitphp.bugurl')) { $project->SetBugUrl($config->GetValue('gitphp.bugurl')); } if ($config->HasValue('gitphp.bugpattern')) { $project->SetBugPattern($config->GetValue('gitphp.bugpattern')); } if ($config->HasValue('gitphp.website')) { $project->SetWebsite($config->GetValue('gitphp.website')); } if ($config->HasValue('gitphp.compat')) { $project->SetCompat($config->GetValue('gitphp.compat')); } if ($config->HasValue('core.abbrev')) { $project->SetAbbreviateLength($config->GetValue('core.abbrev')); } } /** * Applies global config settings to a project * * @param GitPHP_Project $project project */ protected function ApplyGlobalConfig($project) { if (!$project) return; $config = GitPHP_Config::GetInstance(); if ($config->HasKey('cloneurl')) { $project->SetCloneUrl(GitPHP_Util::AddSlash($config->GetValue('cloneurl'), false) . $project->GetProject()); } if ($config->HasKey('pushurl')) { $project->SetPushUrl(GitPHP_Util::AddSlash($config->GetValue('pushurl'), false) . $project->GetProject()); } if ($config->HasKey('bugpattern')) { $project->SetBugPattern($config->GetValue('bugpattern')); } if ($config->HasKey('bugurl')) { $project->SetBugUrl($config->GetValue('bugurl')); } if ($config->HasKey('compat')) { $project->SetCompat($config->GetValue('compat')); } if ($config->HasKey('uniqueabbrev')) { $project->SetUniqueAbbreviation($config->GetValue('uniqueabbrev')); } } /** * Loads all projects in the list */ public function LoadProjects() { $this->PopulateProjects(); $this->projectsLoaded = true; $this->Sort(); $this->ApplySettings(); } /** * Populates the internal list of projects */ abstract protected function PopulateProjects(); /** * Rewinds the iterator * * @return GitPHP_Project */ function rewind() { return reset($this->projects); } /** * Returns the current element in the array * * @return GitPHP_Project */ function current() { return current($this->projects); } /** * Returns the current key * * @return string */ function key() { return key($this->projects); } /** * Advance the pointer * * @return GitPHP_Project */ function next() { return next($this->projects); } /** * Test for a valid pointer * * @return boolean */ function valid() { return key($this->projects) !== null; } /** * Sorts the project list * * @param string $sortBy sort method */ public function Sort($sortBy = GitPHP_ProjectListBase::ProjectSort) { switch ($sortBy) { case GitPHP_ProjectListBase::DescriptionSort: uasort($this->projects, array('GitPHP_Project', 'CompareDescription')); break; case GitPHP_ProjectListBase::OwnerSort: uasort($this->projects, array('GitPHP_Project', 'CompareOwner')); break; case GitPHP_ProjectListBase::AgeSort: uasort($this->projects, array('GitPHP_Project', 'CompareAge')); break; case GitPHP_ProjectListBase::ProjectSort: default: uasort($this->projects, array('GitPHP_Project', 'CompareProject')); break; } } /** * Gets the count of projects * * @return integer number of projects */ public function Count() { return count($this->projects); } /** * Returns a filtered list of projects * * @param string $pattern filter pattern * @return GitPHP_Project[] array of filtered projects */ public function Filter($pattern = null) { if (empty($pattern)) return $this->projects; $matches = array(); foreach ($this->projects as $proj) { if ((stripos($proj->GetProject(), $pattern) !== false) || (stripos($proj->GetDescription(), $pattern) !== false) || (stripos($proj->GetOwner(), $pattern) !== false)) { $matches[] = $proj; } } return $matches; } /** * Applies override settings for a project * * @param GitPHP_Project $project the project object * @param array $projData project data array */ protected function ApplyProjectSettings($project, $projData) { if (!$project) return; if (isset($projData['category']) && is_string($projData['category'])) { $project->SetCategory($projData['category']); } if (isset($projData['owner']) && is_string($projData['owner'])) { $project->SetOwner($projData['owner']); } if (isset($projData['description']) && is_string($projData['description'])) { $project->SetDescription($projData['description']); } if (isset($projData['cloneurl']) && is_string($projData['cloneurl'])) { $project->SetCloneUrl($projData['cloneurl']); } if (isset($projData['pushurl']) && is_string($projData['pushurl'])) { $project->SetPushUrl($projData['pushurl']); } if (isset($projData['bugpattern']) && is_string($projData['bugpattern'])) { $project->SetBugPattern($projData['bugpattern']); } if (isset($projData['bugurl']) && is_string($projData['bugurl'])) { $project->SetBugUrl($projData['bugurl']); } if (isset($projData['compat'])) { $project->SetCompat($projData['compat']); } if (isset($projData['website']) && is_string($projData['website'])) { $project->SetWebsite($projData['website']); } } /** * Sets a list of settings for the project list * * @param array $settings the array of settings */ public function SetSettings($settings) { if ((!$settings) || (count($settings) < 1)) return; $this->projectSettings = $settings; $this->ApplySettings(); } /** * Applies project settings to project list */ protected function ApplySettings() { if (!$this->projectSettings) return; if (count($this->projects) > 0) { foreach ($this->projectSettings as $proj => $setting) { if (empty($proj)) { if (isset($setting['project']) && !empty($setting['project'])) { $proj = $setting['project']; } } if (!isset($this->projects[$proj])) break; $this->ApplyProjectSettings($this->projects[$proj], $setting); } } } /** * Add a new observer * * @param GitPHP_Observer_Interface $observer observer */ public function AddObserver($observer) { if (!$observer) return; if (array_search($observer, $this->observers) !== false) return; $this->observers[] = $observer; } /** * Remove an observer * * @param GitPHP_Observer_Interface $observer observer */ public function RemoveObserver($observer) { if (!$observer) return; $key = array_search($observer, $this->observers); if ($key === false) return; unset($this->observers[$key]); } /** * Log a message to observers * * @param string $message message */ protected function Log($message) { if (empty($message)) return; foreach ($this->observers as $observer) { $observer->ObjectChanged($this, GitPHP_Observer_Interface::LoggableChange, array($message)); } } } |