ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Extractor.php
Go to the documentation of this file.
1<?php
2
3namespace Gettext\Extractors;
4
5use Exception;
6use InvalidArgumentException;
8
9abstract class Extractor
10{
19 public static function fromFile($file, Translations $translations = null)
20 {
21 if ($translations === null) {
22 $translations = new Translations();
23 }
24
25 foreach (self::getFiles($file) as $file) {
26 static::fromString(self::readFile($file), $translations, $file);
27 }
28
29 return $translations;
30 }
31
39 protected static function getFiles($file)
40 {
41 if (empty($file)) {
42 throw new InvalidArgumentException('There is not any file defined');
43 }
44
45 if (is_string($file)) {
46 if (!is_file($file)) {
47 throw new InvalidArgumentException("'$file' is not a valid file");
48 }
49
50 if (!is_readable($file)) {
51 throw new InvalidArgumentException("'$file' is not a readable file");
52 }
53
54 return array($file);
55 }
56
57 if (is_array($file)) {
58 $files = array();
59
60 foreach ($file as $f) {
61 $files = array_merge($files, self::getFiles($f));
62 }
63
64 return $files;
65 }
66
67 throw new InvalidArgumentException('The first argumet must be string or array');
68 }
69
77 protected static function readFile($file)
78 {
79 $length = filesize($file);
80
81 if (!($fd = fopen($file, 'rb'))) {
82 throw new Exception("Cannot read the file '$file', probably permissions");
83 }
84
85 $content = $length ? fread($fd, $length) : '';
86 fclose($fd);
87
88 return $content;
89 }
90}
An exception for terminatinating execution or to throw for unit testing.
static fromFile($file, Translations $translations=null)
Extract the translations from a file.
Definition: Extractor.php:19
static getFiles($file)
Checks and returns all files.
Definition: Extractor.php:39
static readFile($file)
Reads and returns the content of a file.
Definition: Extractor.php:77
Class to manage a collection of translations.
$files
Definition: metarefresh.php:49