Skip to content

Commit 853e8a9

Browse files
committed
Config option added to allow specification of source file encoding in case auto detection doesn't work. Fixed #212
1 parent 83c76b6 commit 853e8a9

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

src/collector/Collector.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,22 @@ class Collector {
6767
*/
6868
private $backend;
6969

70+
/**
71+
* @var string
72+
*/
73+
private $encoding;
7074

7175
/**
72-
* @param ProgressLogger $logger
73-
* @param Project $project
76+
* @param ProgressLogger $logger
77+
* @param Project $project
78+
* @param BackendInterface $backend
79+
* @param $encoding
7480
*/
75-
public function __construct(ProgressLogger $logger, Project $project, BackendInterface $backend) {
81+
public function __construct(ProgressLogger $logger, Project $project, BackendInterface $backend, $encoding) {
7682
$this->logger = $logger;
7783
$this->project = $project;
7884
$this->backend = $backend;
85+
$this->encoding = $encoding;
7986
}
8087

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

91-
$iterator = new SourceFileIterator($scanner($srcDir), $srcDir);
98+
$iterator = new SourceFileIterator($scanner($srcDir), $srcDir, $this->encoding);
9299
foreach($iterator as $file) {
93100
$needsProcessing = $this->project->addFile($file);
94101
if (!$needsProcessing) {

src/collector/SourceFileIterator.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@ class SourceFileIterator implements \Iterator {
77

88
private $iterator;
99
private $srcDir;
10+
private $encoding;
1011

11-
public function __construct(\Iterator $iterator, $srcDir) {
12+
/**
13+
* @param \Iterator $iterator
14+
* @param FileInfo $srcDir
15+
* @param string $encoding
16+
*/
17+
public function __construct(\Iterator $iterator, FileInfo $srcDir, $encoding) {
1218
$this->iterator = $iterator;
1319
$this->srcDir = $srcDir;
20+
$this->encoding = $encoding;
1421
}
1522

1623
/**
1724
* @return SourceFile
1825
*/
1926
public function current() {
20-
return new SourceFile($this->iterator->current()->getPathname(), $this->srcDir);
27+
return new SourceFile($this->iterator->current()->getPathname(), $this->srcDir, $this->encoding);
2128
}
2229

2330
/**

src/collector/project/SourceFile.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ class SourceFile extends FileInfo {
2323
*/
2424
private $srcDir;
2525

26-
public function __construct($file_name, FileInfo $srcDir = NULL) {
26+
/**
27+
* @var
28+
*/
29+
private $encoding;
30+
31+
public function __construct($file_name, FileInfo $srcDir = NULL, $encoding = 'auto') {
2732
parent::__construct($file_name);
2833
$this->srcDir = $srcDir;
34+
$this->encoding = $encoding;
2935
}
3036

3137
/**
@@ -44,10 +50,12 @@ public function getSource() {
4450
return '';
4551
}
4652

47-
$info = new \finfo();
48-
$encoding = $info->file((string)$this, FILEINFO_MIME_ENCODING);
53+
if ($this->encoding == 'auto') {
54+
$info = new \finfo();
55+
$this->encoding = $info->file((string)$this, FILEINFO_MIME_ENCODING);
56+
}
4957
try {
50-
$source = iconv($encoding, 'UTF-8//TRANSLIT', $source);
58+
$source = iconv($this->encoding, 'UTF-8//TRANSLIT', $source);
5159
} catch (\ErrorException $e) {
5260
throw new SourceFileException('Encoding error - conversion to UTF-8 failed', SourceFileException::BadEncoding, $e);
5361
}

src/config/CollectorConfig.php

+7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public function getSourceDirectory() {
7575
return $this->project->getSourceDirectory();
7676
}
7777

78+
/**
79+
* @return string
80+
*/
81+
public function getFileEncoding() {
82+
return $this->ctx->getAttribute('encoding', 'auto');
83+
}
84+
7885
public function isPublicOnlyMode() {
7986
if ($this->ctx->hasAttribute('publiconly')) {
8087
return $this->ctx->getAttribute('publiconly', 'false') === 'true';

src/config/skeleton.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
-->
3636

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

4243
<!-- <include / exclude filter for filelist generator, mask must follow fnmatch() requirements -->
4344
<include mask="*.php" />

src/shared/Factory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ public function getCollector(CollectorConfig $config) {
199199
$config->getSourceDirectory(),
200200
$config->getWorkDirectory()
201201
),
202-
$this->getBackendFactory()->getInstanceFor($config->getBackend())
202+
$this->getBackendFactory()->getInstanceFor($config->getBackend()),
203+
$config->getFileEncoding()
203204
);
204205
}
205206

0 commit comments

Comments
 (0)