Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
47.37% |
9 / 19 |
CRAP | |
53.73% |
36 / 67 |
| File | |
0.00% |
0 / 1 |
|
47.37% |
9 / 19 |
181.03 | |
53.73% |
36 / 67 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getAbsoluteFileName | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getFileName | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| isFile | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| isDir | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| delete | |
0.00% |
0 / 1 |
2.15 | |
66.67% |
2 / 3 |
|||
| createDirForFile | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| deleteRecursively | |
0.00% |
0 / 1 |
4.25 | |
75.00% |
6 / 8 |
|||
| exists | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| createDir | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
| isType | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| scanDir | |
0.00% |
0 / 1 |
5.03 | |
90.00% |
9 / 10 |
|||
| findFile | |
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 10 |
|||
| findFileList | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 10 |
|||
| loadAsXML | |
0.00% |
0 / 1 |
2.21 | |
62.50% |
5 / 8 |
|||
| loadAsJSONArray | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getContents | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| putContents | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| __toString | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| <?php | |
| declare(strict_types = 1); | |
| namespace Siesta\Util; | |
| use Siesta\Exception\XMLNotValidException; | |
| /** | |
| * @author Gregor Müller | |
| */ | |
| class File | |
| { | |
| /** | |
| * absolute path to file | |
| * @var string | |
| */ | |
| protected $absoluteFileName; | |
| /** | |
| * @param string $absoluteFileName | |
| */ | |
| public function __construct(string $absoluteFileName) | |
| { | |
| $this->absoluteFileName = rtrim($absoluteFileName, '/'); | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getAbsoluteFileName() : string | |
| { | |
| return $this->absoluteFileName; | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getFileName() : string | |
| { | |
| return basename($this->absoluteFileName); | |
| } | |
| /** | |
| * tells if the file is a file | |
| * @return bool | |
| */ | |
| public function isFile() : bool | |
| { | |
| return is_file($this->absoluteFileName); | |
| } | |
| /** | |
| * tells if the file is a directory | |
| * @return bool | |
| */ | |
| public function isDir() : bool | |
| { | |
| return is_dir($this->absoluteFileName); | |
| } | |
| /** | |
| * tries to delete a file | |
| * @return bool | |
| */ | |
| public function delete() | |
| { | |
| if (!$this->exists()) { | |
| return false; | |
| } | |
| return unlink($this->absoluteFileName); | |
| } | |
| /** | |
| * | |
| */ | |
| public function createDirForFile() | |
| { | |
| $dir = StringUtil::getStartBeforeLast($this->absoluteFileName, DIRECTORY_SEPARATOR); | |
| $dirFile = new File($dir); | |
| $dirFile->createDir(); | |
| } | |
| /** | |
| * | |
| */ | |
| public function deleteRecursively() | |
| { | |
| if (!$this->isDir()) { | |
| return $this->delete(); | |
| } | |
| $fileList = $this->scanDir(); | |
| foreach ($fileList as $file) { | |
| if ($file->isDir()) { | |
| $file->deleteRecursively(); | |
| } else { | |
| $file->delete(); | |
| } | |
| } | |
| return rmdir($this->absoluteFileName); | |
| } | |
| /** | |
| * tells if the file exists | |
| * @return bool | |
| */ | |
| public function exists() : bool | |
| { | |
| return file_exists($this->absoluteFileName); | |
| } | |
| /** | |
| * creates needed directories recursively | |
| * | |
| * @param int $mode | |
| * | |
| * @return bool | |
| */ | |
| public function createDir($mode = 0777) : bool | |
| { | |
| return is_dir($this->absoluteFileName) || mkdir($this->absoluteFileName, $mode, true); | |
| } | |
| /** | |
| * tells if the file is of type | |
| * | |
| * @param string $type | |
| * | |
| * @return bool | |
| */ | |
| public function isType($type) : bool | |
| { | |
| return StringUtil::endsWith($this->absoluteFileName, $type); | |
| } | |
| /** | |
| * scans a directory and returns a list of Files | |
| * @return File[] | |
| */ | |
| public function scanDir() : array | |
| { | |
| // only possible in directories | |
| if (!$this->isDir()) { | |
| return []; | |
| } | |
| $fileList = []; | |
| $fileNameList = scandir($this->absoluteFileName); | |
| foreach ($fileNameList as $fileName) { | |
| // do not add . and .. | |
| if ($fileName === "." || $fileName === "..") { | |
| continue; | |
| } | |
| $absoluteFileName = $this->absoluteFileName . "/" . $fileName; | |
| $fileList [] = new File ($absoluteFileName); | |
| } | |
| return $fileList; | |
| } | |
| /** | |
| * Finds the first occurence of the given filename | |
| * | |
| * @param string $fileName | |
| * | |
| * @return null|File | |
| */ | |
| public function findFile($fileName) | |
| { | |
| // only possible in directories | |
| if (!$this->isDir()) { | |
| return null; | |
| } | |
| foreach ($this->scanDir() as $file) { | |
| if ($file->getFileName() === $fileName) { | |
| return $file; | |
| } | |
| if ($file->isDir()) { | |
| $result = $file->findFile($fileName); | |
| if ($result !== null) { | |
| return $result; | |
| } | |
| } | |
| } | |
| return null; | |
| } | |
| /** | |
| * @param $fileSuffix | |
| * | |
| * @return File[] | |
| */ | |
| public function findFileList(string $fileSuffix) : array | |
| { | |
| if (!$this->isDir()) { | |
| return []; | |
| } | |
| $result = []; | |
| foreach ($this->scanDir() as $file) { | |
| if ($file->isDir()) { | |
| $result = array_merge($result, $file->findFileList($fileSuffix)); | |
| continue; | |
| } | |
| if ($file->isType($fileSuffix)) { | |
| $result[] = $file; | |
| } | |
| } | |
| return $result; | |
| } | |
| /** | |
| * loads the file as XML DOMDocument | |
| * @return \DomDocument | |
| * @throws XMLNotValidException | |
| */ | |
| public function loadAsXML() | |
| { | |
| $xml = new \DomDocument (); | |
| libxml_use_internal_errors(true); | |
| $result = $xml->load($this->absoluteFileName); | |
| if ($result) { | |
| return $xml; | |
| } | |
| $e = new XMLNotValidException(libxml_get_errors()); | |
| libxml_clear_errors(); | |
| throw $e; | |
| } | |
| /** | |
| * @return array | |
| */ | |
| public function loadAsJSONArray() | |
| { | |
| return json_decode($this->getContents(), true); | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getContents() | |
| { | |
| return file_get_contents($this->absoluteFileName); | |
| } | |
| /** | |
| * @param $data | |
| * | |
| * @return void | |
| */ | |
| public function putContents($data) | |
| { | |
| file_put_contents($this->absoluteFileName, $data); | |
| } | |
| /** | |
| * The __toString method allows a class to decide how it will react when it is converted to a string. | |
| * @return string | |
| * @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring | |
| */ | |
| function __toString() | |
| { | |
| return $this->absoluteFileName; | |
| } | |
| } |