22
33namespace Gettext \Extractors ;
44
5+ use Exception ;
56use Gettext \Translations ;
67use Gettext \Utils \PhpFunctionsScanner ;
78
89/**
910 * Class to get gettext strings from php files returning arrays.
1011 */
11- class PhpCode extends Extractor implements ExtractorInterface
12+ class PhpCode extends Extractor implements ExtractorInterface, ExtractorMultiInterface
1213{
1314 public static $ options = [
14- // - false: to not extract comments
15- // - empty string: to extract all comments
16- // - non-empty string: to extract comments that start with that string
17- // - array with strings to extract comments format.
15+ // - false: to not extract comments
16+ // - empty string: to extract all comments
17+ // - non-empty string: to extract comments that start with that string
18+ // - array with strings to extract comments format.
1819 'extractComments ' => false ,
1920
2021 'constants ' => [],
@@ -43,8 +44,18 @@ class PhpCode extends Extractor implements ExtractorInterface
4344
4445 /**
4546 * {@inheritdoc}
47+ * @throws Exception
4648 */
4749 public static function fromString ($ string , Translations $ translations , array $ options = [])
50+ {
51+ self ::fromStringMultiple ($ string , [$ translations ], $ options );
52+ }
53+
54+ /**
55+ * @inheritDoc
56+ * @throws Exception
57+ */
58+ public static function fromStringMultiple ($ string , array $ translations , array $ options = [])
4859 {
4960 $ options += static ::$ options ;
5061
@@ -57,6 +68,18 @@ public static function fromString($string, Translations $translations, array $op
5768 $ functions ->saveGettextFunctions ($ translations , $ options );
5869 }
5970
71+ /**
72+ * @inheritDoc
73+ */
74+ public static function fromFileMultiple ($ file , array $ translations , array $ options = [])
75+ {
76+ foreach (self ::getFiles ($ file ) as $ file ) {
77+ $ options ['file ' ] = $ file ;
78+ static ::fromStringMultiple (self ::readFile ($ file ), $ translations , $ options );
79+ }
80+ }
81+
82+
6083 /**
6184 * Decodes a T_CONSTANT_ENCAPSED_STRING string.
6285 *
@@ -110,7 +133,11 @@ function ($match) {
110133 );
111134 }
112135
113- //http://php.net/manual/en/function.chr.php#118804
136+ /**
137+ * @param $dec
138+ * @return string|null
139+ * @see http://php.net/manual/en/function.chr.php#118804
140+ */
114141 private static function unicodeChar ($ dec )
115142 {
116143 if ($ dec < 0x80 ) {
@@ -119,20 +146,22 @@ private static function unicodeChar($dec)
119146
120147 if ($ dec < 0x0800 ) {
121148 return chr (0xC0 + ($ dec >> 6 ))
122- .chr (0x80 + ($ dec & 0x3f ));
149+ . chr (0x80 + ($ dec & 0x3f ));
123150 }
124151
125152 if ($ dec < 0x010000 ) {
126153 return chr (0xE0 + ($ dec >> 12 ))
127- . chr (0x80 + (($ dec >> 6 ) & 0x3f ))
128- . chr (0x80 + ($ dec & 0x3f ));
154+ . chr (0x80 + (($ dec >> 6 ) & 0x3f ))
155+ . chr (0x80 + ($ dec & 0x3f ));
129156 }
130157
131158 if ($ dec < 0x200000 ) {
132159 return chr (0xF0 + ($ dec >> 18 ))
133- . chr (0x80 + (($ dec >> 12 ) & 0x3f ))
134- . chr (0x80 + (($ dec >> 6 ) & 0x3f ))
135- . chr (0x80 + ($ dec & 0x3f ));
160+ . chr (0x80 + (($ dec >> 12 ) & 0x3f ))
161+ . chr (0x80 + (($ dec >> 6 ) & 0x3f ))
162+ . chr (0x80 + ($ dec & 0x3f ));
136163 }
164+
165+ return null ;
137166 }
138167}
0 commit comments