55 */
66namespace Magento \Setup \Module \I18n \Parser \Adapter ;
77
8+ use Magento \Framework \Exception \FileSystemException ;
9+ use Magento \Framework \Filesystem \Driver \File ;
10+
811/**
912 * Js parser adapter
1013 */
1114class Js extends AbstractAdapter
1215{
16+ /**
17+ * @var \Magento\Framework\Filesystem
18+ */
19+ protected $ _filesystem ;
20+
21+ /**
22+ * Adapter construct
23+ *
24+ * @param File $fileSystem
25+ */
26+ public function __construct (
27+ File $ fileSystem
28+ ) {
29+ $ this ->_fileSystem = $ fileSystem ;
30+ }
1331 /**
1432 * Covers
1533 * $.mage.__('Example text')
@@ -32,27 +50,59 @@ class Js extends AbstractAdapter
3250 */
3351 protected function _parse ()
3452 {
35- $ fileHandle = @fopen ($ this ->_file , 'r ' );
53+
54+ $ fileHandle = $ this ->_fileSystem ->fileOpen ($ this ->_file , 'r ' );
3655 $ lineNumber = 0 ;
37- while (!feof ($ fileHandle )) {
38- $ lineNumber ++;
39- $ fileRow = fgets ($ fileHandle , 4096 );
40- $ results = [];
41- $ regexes = [
42- static ::REGEX_MAGE_TRANSLATE ,
43- static ::REGEX_TRANSLATE_FUNCTION
44- ];
56+ try {
57+ while (($ line = $ this ->fileReadLine ($ fileHandle , 0 )) !== false ) {
58+ $ lineNumber ++;
59+ $ fileRow = preg_replace ('/"\s+\+"|"\s+\+\s+\"|"\+\s+\"|"\+"/ ' , '' , $ line );
60+ $ fileRow = preg_replace ("/'\s+\+'|'\s+\+\s+\'|'\+\s+\'|'\+'/ " , "" , $ fileRow );
61+ $ results = [];
62+ $ regexes = [
63+ static ::REGEX_MAGE_TRANSLATE ,
64+ static ::REGEX_TRANSLATE_FUNCTION
65+ ];
4566
46- foreach ($ regexes as $ regex ) {
47- preg_match_all ($ regex , $ fileRow , $ results , PREG_SET_ORDER );
48- for ($ i = 0 , $ count = count ($ results ); $ i < $ count ; $ i ++) {
49- if (isset ($ results [$ i ][2 ])) {
50- $ quote = $ results [$ i ][1 ];
51- $ this ->_addPhrase ($ quote . $ results [$ i ][2 ] . $ quote , $ lineNumber );
67+ foreach ($ regexes as $ regex ) {
68+ preg_match_all ($ regex , $ fileRow , $ results , PREG_SET_ORDER );
69+ for ($ i = 0 , $ count = count ($ results ); $ i < $ count ; $ i ++) {
70+ if (isset ($ results [$ i ][2 ])) {
71+ $ quote = $ results [$ i ][1 ];
72+ $ this ->_addPhrase ($ quote . $ results [$ i ][2 ] . $ quote , $ lineNumber );
73+ }
5274 }
5375 }
5476 }
77+ } catch (\Exception $ e ) {
78+ $ this ->_fileSystem ->fileClose ($ fileHandle );
79+ throw new FileSystemException (
80+ new \Magento \Framework \Phrase ('Stream get line failed %1 ' , [$ e ->getMessage ()])
81+ );
82+ }
83+ $ this ->_fileSystem ->fileClose ($ fileHandle );
84+ }
85+
86+ /**
87+ * Reads the line content from file pointer (with specified number of bytes from the current position).
88+ *
89+ * @param resource $resource
90+ * @param int $length
91+ * @param string $ending [optional]
92+ * @return string
93+ * @throws FileSystemException
94+ */
95+ public function fileReadLine ($ resource , $ length , $ ending = null )
96+ {
97+ try {
98+ // phpcs:disable
99+ $ result = @stream_get_line ($ resource , $ length , $ ending );
100+ // phpcs:enable
101+ } catch (\Exception $ e ) {
102+ throw new FileSystemException (
103+ new \Magento \Framework \Phrase ('Stream get line failed %1 ' , [$ e ->getMessage ()])
104+ );
55105 }
56- fclose ( $ fileHandle ) ;
106+ return $ result ;
57107 }
58108}
0 commit comments