FetcherResult.php in Feeds 8.3
File
src/Result/FetcherResult.php
View source
<?php
namespace Drupal\feeds\Result;
use Drupal\Component\Render\FormattableMarkup;
class FetcherResult implements FetcherResultInterface {
protected $filePath;
public function __construct($file_path) {
$this->filePath = $file_path;
}
public function getRaw() {
$this
->checkFile();
return $this
->sanitizeRaw(file_get_contents($this->filePath));
}
public function getFilePath() {
$this
->checkFile();
return $this
->sanitizeFile();
}
protected function checkFile() {
if (!file_exists($this->filePath)) {
throw new \RuntimeException(new FormattableMarkup('File %filepath does not exist.', [
'%filepath' => $this->filePath,
]));
}
if (!is_readable($this->filePath)) {
throw new \RuntimeException(new FormattableMarkup('File %filepath is not readable.', [
'%filepath' => $this->filePath,
]));
}
}
protected function sanitizeRaw($raw) {
if (substr($raw, 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf)) {
$raw = substr($raw, 3);
}
return $raw;
}
protected function sanitizeFile() {
$handle = fopen($this->filePath, 'r');
$line = fgets($handle);
fclose($handle);
if (substr($line, 0, 3) !== pack('CCC', 0xef, 0xbb, 0xbf)) {
return $this->filePath;
}
if (!is_writable($this->filePath)) {
throw new \RuntimeException(new FormattableMarkup('File %filepath is not writable.', [
'%filepath' => $this->filePath,
]));
}
$contents = file_get_contents($this->filePath);
$contents = substr($contents, 3);
$status = file_put_contents($this->filePath, $contents);
return $this->filePath;
}
}