Add specific exception for project list file read failure
[gitphp.git] / include / git / projectlist / ProjectListFile.class.php
blob:a/include/git/projectlist/ProjectListFile.class.php -> blob:b/include/git/projectlist/ProjectListFile.class.php
<?php <?php
/** /**
* Lists all projects in a given file * Lists all projects in a given file
* *
* @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\ProjectList * @subpackage Git\ProjectList
*/ */
class GitPHP_ProjectListFile extends GitPHP_ProjectListBase class GitPHP_ProjectListFile extends GitPHP_ProjectListBase
{ {
   
/** /**
* The contents of the project list file * The contents of the project list file
* *
* @var string[] * @var string[]
*/ */
protected $fileContents = array(); protected $fileContents = array();
   
/** /**
* Whether the file has been read * Whether the file has been read
* *
* @var boolean * @var boolean
*/ */
protected $fileRead = false; protected $fileRead = false;
/** /**
* constructor * constructor
* *
* @param string $projectRoot project root * @param string $projectRoot project root
* @param string $projectFile file to read * @param string $projectFile file to read
* @throws Exception if parameter is not a readable file * @throws Exception if parameter is not a readable file
*/ */
public function __construct($projectRoot, $projectFile) public function __construct($projectRoot, $projectFile)
{ {
if (!(is_string($projectFile) && is_file($projectFile))) { if (!(is_string($projectFile) && is_file($projectFile))) {
throw new GitPHP_InvalidFileException($projectFile); throw new GitPHP_InvalidFileException($projectFile);
} }
   
$this->projectConfig = $projectFile; $this->projectConfig = $projectFile;
   
parent::__construct($projectRoot); parent::__construct($projectRoot);
} }
   
/** /**
* Populates the internal list of projects * Populates the internal list of projects
*/ */
protected function PopulateProjects() protected function PopulateProjects()
{ {
if (!$this->fileRead) if (!$this->fileRead)
$this->ReadFile(); $this->ReadFile();
   
foreach ($this->fileContents as $lineData) { foreach ($this->fileContents as $lineData) {
if (isset($lineData['project'])) { if (isset($lineData['project'])) {
$projObj = $this->LoadProject($lineData['project']); $projObj = $this->LoadProject($lineData['project']);
if ($projObj) { if ($projObj) {
$this->projects[$lineData['project']] = $projObj; $this->projects[$lineData['project']] = $projObj;
unset($projObj); unset($projObj);
} }
} }
} }
} }
   
/** /**
* Loads a project * Loads a project
* *
* @param string $proj project * @param string $proj project
* @return GitPHP_Project project object * @return GitPHP_Project project object
*/ */
protected function LoadProject($proj) protected function LoadProject($proj)
{ {
if (!$this->fileRead) if (!$this->fileRead)
$this->ReadFile(); $this->ReadFile();
   
$found = false; $found = false;
$owner = null; $owner = null;
foreach ($this->fileContents as $lineData) { foreach ($this->fileContents as $lineData) {
if (isset($lineData['project']) && ($lineData['project'] == $proj)) { if (isset($lineData['project']) && ($lineData['project'] == $proj)) {
$projectRoot = GitPHP_Util::AddSlash($this->projectRoot); $projectRoot = GitPHP_Util::AddSlash($this->projectRoot);
if (is_file($projectRoot . $proj . '/HEAD')) { if (is_file($projectRoot . $proj . '/HEAD')) {
$found = true; $found = true;
if (isset($lineData['owner'])) { if (isset($lineData['owner'])) {
$owner = $lineData['owner']; $owner = $lineData['owner'];
} }
} else { } else {
$this->Log(sprintf('%1$s is not a git project', $projectRoot . $proj)); $this->Log(sprintf('%1$s is not a git project', $projectRoot . $proj));
} }
break; break;
} }
} }
   
if (!$found) if (!$found)
return null; return null;
   
$projectObj = new GitPHP_Project($this->projectRoot, $proj); $projectObj = new GitPHP_Project($this->projectRoot, $proj);
   
$this->ApplyGlobalConfig($projectObj); $this->ApplyGlobalConfig($projectObj);
   
$this->ApplyGitConfig($projectObj); $this->ApplyGitConfig($projectObj);
   
if (!empty($owner)) if (!empty($owner))
$projectObj->SetOwner($owner); $projectObj->SetOwner($owner);
   
if ($this->projectSettings && isset($this->projectSettings[$proj])) { if ($this->projectSettings && isset($this->projectSettings[$proj])) {
$this->ApplyProjectSettings($projectObj, $this->projectSettings[$proj]); $this->ApplyProjectSettings($projectObj, $this->projectSettings[$proj]);
} }
   
$this->InjectProjectDependencies($projectObj); $this->InjectProjectDependencies($projectObj);
   
return $projectObj; return $projectObj;
} }
   
/** /**
* Reads the file contents * Reads the file contents
*/ */
protected function ReadFile() protected function ReadFile()
{ {
$this->fileRead = true; $this->fileRead = true;
   
$fileString = file_get_contents($this->projectConfig); $fileString = file_get_contents($this->projectConfig);
if ($fileString === false) { if ($fileString === false) {
throw new GitPHP_MessageException(sprintf(__('Failed to open project list file %1$s'), $this->projectConfig)); throw new GitPHP_ProjectListFileReadException($this->projectConfig);
} }
   
$this->fileContents = array(); $this->fileContents = array();
   
$fileLines = explode("\n", $fileString); $fileLines = explode("\n", $fileString);
foreach ($fileLines as $line) { foreach ($fileLines as $line) {
if (preg_match('/^([^\s]+)(\s.+)?$/', $line, $regs)) { if (preg_match('/^([^\s]+)(\s.+)?$/', $line, $regs)) {
$data = array(); $data = array();
$data['project'] = $regs[1]; $data['project'] = $regs[1];
$owner = trim($regs[2]); $owner = trim($regs[2]);
if (!empty($owner)) { if (!empty($owner)) {
$data['owner'] = $owner; $data['owner'] = $owner;
} }
$this->fileContents[] = $data; $this->fileContents[] = $data;
} }
} }
} }
   
} }
   
comments