Skip to content

Instantly share code, notes, and snippets.

@shiwork
Last active December 27, 2015 13:29
Show Gist options
  • Save shiwork/7333675 to your computer and use it in GitHub Desktop.
Save shiwork/7333675 to your computer and use it in GitHub Desktop.
phpDocumentor2で日本語が扱えない問題の修正。 対象のファイルの文字コードに合わせて変換したいが、php.iniのlanguageやらmb_languageに依存するみたい。
/**
* Opens the file and retrieves its contents.
*
* During construction the given file is checked whether it is readable and
* if the $validate argument is true a PHP Lint action is executed to
* check whether the there are no parse errors.
*
* By default the Lint check is disabled because of the performance hit
* introduced by this action.
*
* If the validation checks out, the file's contents are read, converted to
* UTF-8 and the object is created from those contents.
*
* @param string $file Name of the file.
* @param boolean $validate Whether to check the file using PHP Lint.
* @param string $encoding The encoding of the file.
*
* @throws Exception\UnreadableFile If the filename is incorrect or
* the file cannot be opened
* @throws Exception\UnparsableFile If the file fails PHP lint checking
* (this can only happen when $validate is set to true)
*/
public function __construct($file, $validate = false, $encoding = 'utf-8')
{
if (!is_string($file) || (!is_readable($file))) {
throw new Exception\UnreadableFile(
'The given file should be a string, should exist on the filesystem and should be readable'
);
}
if ($validate) {
exec('php -l ' . escapeshellarg($file), $output, $result);
if ($result != 0) {
throw new Exception\UnparsableFile(
'The given file could not be interpreted as it contains errors: '
. implode(PHP_EOL, $output)
);
}
}
$this->filename = $file;
$this->contents = mb_convert_encoding(file_get_contents($file), 'UTF-8', "ASCII,JIS,UTF-8,EUC-JP,SJIS");
$this->context = new Context();
if (strtolower($encoding) !== 'utf-8') {
$this->contents = iconv(
strtolower($encoding),
'utf-8//IGNORE//TRANSLIT',
$this->contents
);
}
// filemtime($file) is sometimes between 0.00001 and 0.00005 seconds
// faster but md5 is more accurate. It can also result in false
// positives or false negatives after copying or checking out a codebase.
$this->hash = md5($this->contents);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment