Don't cache entire project object
Don't cache entire project object

Serialization works recursively, which means everything contained inside
the project object is also serialized, which is a bunch of wasted space
and may not be safe.
The only thing we need to find a project is its path.

<?php <?php
/** /**
* GitPHP ProjectListDirectory * GitPHP ProjectListDirectory
* *
* Lists all projects in a given directory * Lists all projects in a given directory
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Git * @subpackage Git
*/ */
   
require_once(GITPHP_INCLUDEDIR . 'Config.class.php'); require_once(GITPHP_INCLUDEDIR . 'Config.class.php');
require_once(GITPHP_GITOBJECTDIR . 'ProjectListBase.class.php'); require_once(GITPHP_GITOBJECTDIR . 'ProjectListBase.class.php');
require_once(GITPHP_GITOBJECTDIR . 'Project.class.php'); require_once(GITPHP_GITOBJECTDIR . 'Project.class.php');
   
/** /**
* ProjectListDirectory class * ProjectListDirectory class
* *
* @package GitPHP * @package GitPHP
* @subpackage Git * @subpackage Git
*/ */
class GitPHP_ProjectListDirectory extends GitPHP_ProjectListBase class GitPHP_ProjectListDirectory extends GitPHP_ProjectListBase
{ {
/** /**
* projectDir * projectDir
* *
* Stores projectlist directory internally * Stores projectlist directory internally
* *
* @access protected * @access protected
*/ */
protected $projectDir; protected $projectDir;
   
/** /**
* __construct * __construct
* *
* constructor * constructor
* *
* @param string $projectDir directory to search * @param string $projectDir directory to search
* @throws Exception if parameter is not a directory * @throws Exception if parameter is not a directory
* @access public * @access public
*/ */
public function __construct($projectDir) public function __construct($projectDir)
{ {
if (!is_dir($projectDir)) { if (!is_dir($projectDir)) {
throw new Exception(sprintf(__('%1$s is not a directory'), $projectDir)); throw new Exception(sprintf(__('%1$s is not a directory'), $projectDir));
} }
   
$this->projectDir = GitPHP_Util::AddSlash($projectDir); $this->projectDir = GitPHP_Util::AddSlash($projectDir);
   
parent::__construct(); parent::__construct();
} }
   
/** /**
* PopulateProjects * PopulateProjects
* *
* Populates the internal list of projects * Populates the internal list of projects
* *
* @access protected * @access protected
*/ */
protected function PopulateProjects() protected function PopulateProjects()
{ {
$key = 'projectdir|' . $this->projectDir . '|projectlist|directory'; $key = 'projectdir|' . $this->projectDir . '|projectlist|directory';
$cached = GitPHP_Cache::GetObjectCacheInstance()->Get($key); $cached = GitPHP_Cache::GetObjectCacheInstance()->Get($key);
if ($cached && (count($cached) > 0)) { if ($cached && (count($cached) > 0)) {
GitPHP_Log::GetInstance()->Log('Loaded ' . count($cached) . ' projects from cache'); foreach ($cached as $proj) {
$this->projects = $cached; $this->AddProject($proj);
  }
  GitPHP_Log::GetInstance()->Log('Loaded ' . count($this->projects) . ' projects from cache');
return; return;
} }
   
$this->RecurseDir($this->projectDir); $this->RecurseDir($this->projectDir);
   
GitPHP_Cache::GetObjectCacheInstance()->Set($key, $this->projects, GitPHP_Config::GetInstance()->GetValue('cachelifetime', 3600)); if (count($this->projects) > 0) {
  $projects = array();
  foreach ($this->projects as $proj) {
  $projects[] = $proj->GetProject();;
  }
  GitPHP_Cache::GetObjectCacheInstance()->Set($key, $projects, GitPHP_Config::GetInstance()->GetValue('cachelifetime', 3600));
  }
} }
   
/** /**
* RecurseDir * RecurseDir
* *
* Recursively searches for projects * Recursively searches for projects
* *
* @param string $dir directory to recurse into * @param string $dir directory to recurse into
*/ */
private function RecurseDir($dir) private function RecurseDir($dir)
{ {
if (!is_dir($dir)) if (!is_dir($dir))
return; return;
   
GitPHP_Log::GetInstance()->Log(sprintf('Searching directory %1$s', $dir)); GitPHP_Log::GetInstance()->Log(sprintf('Searching directory %1$s', $dir));
   
if ($dh = opendir($dir)) { if ($dh = opendir($dir)) {
$trimlen = strlen($this->projectDir) + 1; $trimlen = strlen($this->projectDir) + 1;
while (($file = readdir($dh)) !== false) { while (($file = readdir($dh)) !== false) {
$fullPath = $dir . '/' . $file; $fullPath = $dir . '/' . $file;
if ((strpos($file, '.') !== 0) && is_dir($fullPath)) { if ((strpos($file, '.') !== 0) && is_dir($fullPath)) {
if (is_file($fullPath . '/HEAD')) { if (is_file($fullPath . '/HEAD')) {
GitPHP_Log::GetInstance()->Log(sprintf('Found project %1$s', $fullPath)); GitPHP_Log::GetInstance()->Log(sprintf('Found project %1$s', $fullPath));
$projectPath = substr($fullPath, $trimlen); $projectPath = substr($fullPath, $trimlen);
try { $this->AddProject($projectPath);
$proj = new GitPHP_Project($this->projectDir, $projectPath);  
$proj->SetCategory(trim(substr($dir, strlen($this->projectDir)), '/'));  
if ((!GitPHP_Config::GetInstance()->GetValue('exportedonly', false)) || $proj->GetDaemonEnabled()) {  
$this->projects[$projectPath] = $proj;  
}  
} catch (Exception $e) {  
GitPHP_Log::GetInstance()->Log($e->getMessage());  
}  
} else { } else {
$this->RecurseDir($fullPath); $this->RecurseDir($fullPath);
} }
} else { } else {
GitPHP_Log::GetInstance()->Log(sprintf('Skipping %1$s', $fullPath)); GitPHP_Log::GetInstance()->Log(sprintf('Skipping %1$s', $fullPath));
} }
} }
closedir($dh); closedir($dh);
} }
} }
   
  /**
  * AddProject
  *
  * Add project to collection
  *
  * @access private
  */
  private function AddProject($projectPath)
  {
  try {
  $proj = new GitPHP_Project($this->projectDir, $projectPath);
  $category = trim(dirname($projectPath));
  if (!(empty($category) || (strpos($category, '.') === 0))) {
  $proj->SetCategory($category);
  }
  if ((!GitPHP_Config::GetInstance()->GetValue('exportedonly', false)) || $proj->GetDaemonEnabled()) {
  $this->projects[$projectPath] = $proj;
  }
  } catch (Exception $e) {
  GitPHP_Log::GetInstance()->Log($e->getMessage());
  }
  }
   
} }
   
comments