Skip to content

Commit

Permalink
Config option added to allow specification of source file encoding in…
Browse files Browse the repository at this point in the history
… case auto detection doesn't work. Fixed #212
  • Loading branch information
theseer committed Apr 27, 2015
1 parent 83c76b6 commit 853e8a9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/collector/Collector.php
Expand Up @@ -67,15 +67,22 @@ class Collector {
*/
private $backend;

/**
* @var string
*/
private $encoding;

/**
* @param ProgressLogger $logger
* @param Project $project
* @param ProgressLogger $logger
* @param Project $project
* @param BackendInterface $backend
* @param $encoding
*/
public function __construct(ProgressLogger $logger, Project $project, BackendInterface $backend) {
public function __construct(ProgressLogger $logger, Project $project, BackendInterface $backend, $encoding) {
$this->logger = $logger;
$this->project = $project;
$this->backend = $backend;
$this->encoding = $encoding;
}

/**
Expand All @@ -88,7 +95,7 @@ public function run(DirectoryScanner $scanner) {
$srcDir = $this->project->getSourceDir();
$this->logger->log("Scanning directory '{$srcDir}' for files to process\n");

$iterator = new SourceFileIterator($scanner($srcDir), $srcDir);
$iterator = new SourceFileIterator($scanner($srcDir), $srcDir, $this->encoding);
foreach($iterator as $file) {
$needsProcessing = $this->project->addFile($file);
if (!$needsProcessing) {
Expand Down
11 changes: 9 additions & 2 deletions src/collector/SourceFileIterator.php
Expand Up @@ -7,17 +7,24 @@ class SourceFileIterator implements \Iterator {

private $iterator;
private $srcDir;
private $encoding;

public function __construct(\Iterator $iterator, $srcDir) {
/**
* @param \Iterator $iterator
* @param FileInfo $srcDir
* @param string $encoding
*/
public function __construct(\Iterator $iterator, FileInfo $srcDir, $encoding) {
$this->iterator = $iterator;
$this->srcDir = $srcDir;
$this->encoding = $encoding;
}

/**
* @return SourceFile
*/
public function current() {
return new SourceFile($this->iterator->current()->getPathname(), $this->srcDir);
return new SourceFile($this->iterator->current()->getPathname(), $this->srcDir, $this->encoding);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/collector/project/SourceFile.php
Expand Up @@ -23,9 +23,15 @@ class SourceFile extends FileInfo {
*/
private $srcDir;

public function __construct($file_name, FileInfo $srcDir = NULL) {
/**
* @var
*/
private $encoding;

public function __construct($file_name, FileInfo $srcDir = NULL, $encoding = 'auto') {
parent::__construct($file_name);
$this->srcDir = $srcDir;
$this->encoding = $encoding;
}

/**
Expand All @@ -44,10 +50,12 @@ public function getSource() {
return '';
}

$info = new \finfo();
$encoding = $info->file((string)$this, FILEINFO_MIME_ENCODING);
if ($this->encoding == 'auto') {
$info = new \finfo();
$this->encoding = $info->file((string)$this, FILEINFO_MIME_ENCODING);
}
try {
$source = iconv($encoding, 'UTF-8//TRANSLIT', $source);
$source = iconv($this->encoding, 'UTF-8//TRANSLIT', $source);
} catch (\ErrorException $e) {
throw new SourceFileException('Encoding error - conversion to UTF-8 failed', SourceFileException::BadEncoding, $e);
}
Expand Down
7 changes: 7 additions & 0 deletions src/config/CollectorConfig.php
Expand Up @@ -75,6 +75,13 @@ public function getSourceDirectory() {
return $this->project->getSourceDirectory();
}

/**
* @return string
*/
public function getFileEncoding() {
return $this->ctx->getAttribute('encoding', 'auto');
}

public function isPublicOnlyMode() {
if ($this->ctx->hasAttribute('publiconly')) {
return $this->ctx->getAttribute('publiconly', 'false') === 'true';
Expand Down
3 changes: 2 additions & 1 deletion src/config/skeleton.xml
Expand Up @@ -35,9 +35,10 @@
-->

<!-- Additional configuration for the collecting process (parsing of php code, generation of xml data) -->
<collector publiconly="false" backend="parser">
<collector publiconly="false" backend="parser" encoding="auto">
<!-- @publiconly - Flag to disable/enable processing of non public methods and members -->
<!-- @backend - The collector backend to use, currently only shipping with 'parser' -->
<!-- @encoding - Charset encoding of source files (overwrite default 'auto' if detection fails) -->

<!-- <include / exclude filter for filelist generator, mask must follow fnmatch() requirements -->
<include mask="*.php" />
Expand Down
3 changes: 2 additions & 1 deletion src/shared/Factory.php
Expand Up @@ -199,7 +199,8 @@ public function getCollector(CollectorConfig $config) {
$config->getSourceDirectory(),
$config->getWorkDirectory()
),
$this->getBackendFactory()->getInstanceFor($config->getBackend())
$this->getBackendFactory()->getInstanceFor($config->getBackend()),
$config->getFileEncoding()
);
}

Expand Down

0 comments on commit 853e8a9

Please sign in to comment.