Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor parsing subsystem and clean up code and responsibilities qui…
…te a bit
- Loading branch information
Showing
7 changed files
with
329 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
namespace TheSeer\Autoload { | ||
|
||
use TheSeer\DirectoryScanner\PHPFilterIterator; | ||
|
||
class Collector { | ||
|
||
/** | ||
* @var Parser | ||
*/ | ||
private $parser; | ||
|
||
/** | ||
* @var CollectorResult | ||
*/ | ||
private $collectorResult; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $toleranceMode; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $mimeCheck; | ||
|
||
/** | ||
* @param Parser $parser | ||
* @param bool $toleranceMode | ||
* @param bool $mimeCheck | ||
*/ | ||
public function __construct(Parser $parser, $toleranceMode = false, $mimeCheck = false) { | ||
$this->parser = $parser; | ||
$this->toleranceMode = $toleranceMode; | ||
$this->mimeCheck = $mimeCheck; | ||
$this->collectorResult = new CollectorResult(); | ||
} | ||
|
||
public function getResult() { | ||
return $this->collectorResult; | ||
} | ||
|
||
public function addDirectory(\Iterator $sources) { | ||
$worker = $this->mimeCheck ? new PHPFilterIterator($sources) : $sources; | ||
foreach($worker as $file) { | ||
try { | ||
$parseResult = $this->parser->parse(new SourceFile($file->getRealpath())); | ||
if ($parseResult->hasRedeclarations() && !$this->toleranceMode) { | ||
throw new CollectorException( | ||
sprintf( | ||
'The file "%s" conains duplicate (potentially conditional) definitions of the following unit(s): %s', | ||
$file->getRealPath(), | ||
join(', ', $parseResult->getRedeclarations()) | ||
), | ||
CollectorException::InFileRedeclarationFound | ||
); | ||
} | ||
$this->collectorResult->addParseResult($file, $parseResult); | ||
} catch(ParserException $e) { | ||
throw new CollectorException( | ||
sprintf( | ||
'Could not process file "%s" due to parse errors', | ||
$file->getRealPath() | ||
), | ||
CollectorException::ParseErrror, | ||
$e | ||
); | ||
} catch(CollectorResultException $e) { | ||
throw new CollectorException( | ||
$e->getMessage(), | ||
CollectorException::RedeclarationFound | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class CollectorException extends \Exception { | ||
const ParseErrror = 1; | ||
const RedeclarationFound = 2; | ||
const InFileRedeclarationFound = 3; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
namespace TheSeer\Autoload { | ||
|
||
class CollectorResult { | ||
|
||
private $units = array(); | ||
private $dependencies = array(); | ||
|
||
public function addParseResult(\SplFileInfo $file, ParseResult $result) { | ||
if (!$result->hasUnits()) { | ||
return; | ||
} | ||
$filename = $file->getRealPath(); | ||
foreach($result->getUnits() as $unit) { | ||
if (isset($this->units[$unit])) { | ||
throw new CollectorResultException( | ||
sprintf( | ||
'Redeclaration of unit "%s" in file "%s" (first occurance was in file "%s")', | ||
$unit, | ||
$filename, | ||
$this->units[$unit] | ||
), | ||
CollectorResultException::DuplicateUnitName | ||
); | ||
} | ||
$this->units[$unit] = $filename; | ||
$this->dependencies[$unit] = $result->getDependenciesForUnit($unit); | ||
} | ||
|
||
} | ||
|
||
public function hasUnits() { | ||
return count($this->units) > 0; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getDependencies() { | ||
return $this->dependencies; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getUnits() { | ||
return $this->units; | ||
} | ||
} | ||
|
||
class CollectorResultException extends \Exception { | ||
const DuplicateUnitName = 1; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
namespace TheSeer\Autoload { | ||
|
||
class ParseResult { | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $units = array(); | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $dependencies = array(); | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $redeclarations = array(); | ||
|
||
public function __construct(Array $units, Array $dependencies, Array $redeclarations) { | ||
$this->units = $units; | ||
$this->dependencies = $dependencies; | ||
$this->redeclarations = $redeclarations; | ||
} | ||
|
||
public function hasUnits() { | ||
return count($this->units) > 0; | ||
} | ||
|
||
public function hasRedeclarations() { | ||
return count($this->redeclarations) > 0; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getDependenciesForUnit($unit) { | ||
if (!isset($this->dependencies[$unit])) { | ||
return array(); | ||
} | ||
return $this->dependencies[$unit]; | ||
} | ||
|
||
/** | ||
* @return \string[] | ||
*/ | ||
public function getRedeclarations() { | ||
return $this->redeclarations; | ||
} | ||
|
||
/** | ||
* @return \string[] | ||
*/ | ||
public function getUnits() { | ||
return $this->units; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.