@@ -103,27 +103,6 @@ public static function fromReflection(FileReflection $fileReflection)
103
103
}
104
104
105
105
$ file ->setClass ($ phpClass );
106
-
107
- $ classStartLine = $ class ->getStartLine (true );
108
- $ classEndLine = $ class ->getEndLine ();
109
-
110
- $ bodyLines = explode ("\n" , $ body );
111
- $ bodyReturn = array ();
112
- for ($ lineNum = 1 , $ count = count ($ bodyLines ); $ lineNum <= $ count ; $ lineNum ++) {
113
- if ($ lineNum == $ classStartLine ) {
114
- $ bodyReturn [] = str_replace (
115
- '? ' ,
116
- $ class ->getName (),
117
- '/* Zend_Code_Generator_Php_File-ClassMarker: {?} */ '
118
- );
119
-
120
- $ lineNum = $ classEndLine ;
121
- } else {
122
- $ bodyReturn [] = $ bodyLines [$ lineNum - 1 ]; // adjust for index -> line conversion
123
- }
124
- }
125
- $ body = implode ("\n" , $ bodyReturn );
126
- unset($ bodyLines , $ bodyReturn , $ classStartLine , $ classEndLine );
127
106
}
128
107
129
108
$ namespace = $ fileReflection ->getNamespace ();
@@ -139,27 +118,8 @@ public static function fromReflection(FileReflection $fileReflection)
139
118
if (($ fileReflection ->getDocComment () != '' )) {
140
119
$ docBlock = $ fileReflection ->getDocBlock ();
141
120
$ file ->setDocBlock (DocBlockGenerator::fromReflection ($ docBlock ));
142
-
143
- $ bodyLines = explode ("\n" , $ body );
144
- $ bodyReturn = array ();
145
- for ($ lineNum = 1 , $ count = count ($ bodyLines ); $ lineNum <= $ count ; $ lineNum ++) {
146
- if ($ lineNum == $ docBlock ->getStartLine ()) {
147
- $ bodyReturn [] = str_replace (
148
- '? ' ,
149
- $ class ->getName (),
150
- '/* Zend_Code_Generator_FileGenerator-DocBlockMarker */ '
151
- );
152
- $ lineNum = $ docBlock ->getEndLine ();
153
- } else {
154
- $ bodyReturn [] = $ bodyLines [$ lineNum - 1 ]; // adjust for index -> line conversion
155
- }
156
- }
157
- $ body = implode ("\n" , $ bodyReturn );
158
- unset($ bodyLines , $ bodyReturn , $ classStartLine , $ classEndLine );
159
121
}
160
122
161
- $ file ->setBody ($ body );
162
-
163
123
return $ file ;
164
124
}
165
125
@@ -244,19 +204,6 @@ public function getRequiredFiles()
244
204
return $ this ->requiredFiles ;
245
205
}
246
206
247
- /**
248
- * @param array $classes
249
- * @return FileGenerator
250
- */
251
- public function setClasses (array $ classes )
252
- {
253
- foreach ($ classes as $ class ) {
254
- $ this ->setClass ($ class );
255
- }
256
-
257
- return $ this ;
258
- }
259
-
260
207
/**
261
208
* @return string
262
209
*/
@@ -338,6 +285,19 @@ public function setUse($use, $as = null)
338
285
return $ this ;
339
286
}
340
287
288
+ /**
289
+ * @param array $classes
290
+ * @return FileGenerator
291
+ */
292
+ public function setClasses (array $ classes )
293
+ {
294
+ foreach ($ classes as $ class ) {
295
+ $ this ->setClass ($ class );
296
+ }
297
+
298
+ return $ this ;
299
+ }
300
+
341
301
/**
342
302
* @param string $name
343
303
* @return ClassGenerator
@@ -453,14 +413,18 @@ public function generate()
453
413
454
414
$ output = '' ;
455
415
456
- // start with the body (if there), or open tag
416
+ // @note body gets populated when FileGenerator created
417
+ // from a file. @see fromReflection and may also be set
418
+ // via FileGenerator::setBody
457
419
$ body = $ this ->getBody ();
420
+
421
+ // start with the body (if there), or open tag
458
422
if (preg_match ('#(?:\s*)<\?php# ' , $ body ) == false ) {
459
423
$ output = '<?php ' . self ::LINE_FEED ;
460
424
}
461
425
462
426
// if there are markers, put the body into the output
463
- if (preg_match ('#/\* Zend_Code_Generator_FileGenerator -(.*?)Marker:# ' , $ body )) {
427
+ if (preg_match ('#/\* Zend_Code_Generator_Php_File -(.*?)Marker:#m ' , $ body )) {
464
428
$ tokens = token_get_all ($ body );
465
429
foreach ($ tokens as $ token ) {
466
430
if (is_array ($ token ) && in_array ($ token [0 ], array (T_OPEN_TAG , T_COMMENT , T_DOC_COMMENT , T_WHITESPACE ))
@@ -475,8 +439,8 @@ public function generate()
475
439
if (null !== ($ docBlock = $ this ->getDocBlock ())) {
476
440
$ docBlock ->setIndentation ('' );
477
441
478
- if (preg_match ('#/* Zend_Code_Generator_FileGenerator-DocBlockMarker */# ' , $ output )) {
479
- $ output = preg_replace ('#/* Zend_CodeGenerator_Php_File -DocBlockMarker */# ' , $ docBlock ->generate (),
442
+ if (preg_match ('#/\ * Zend_Code_Generator_FileGenerator-DocBlockMarker \ */#m ' , $ output )) {
443
+ $ output = preg_replace ('#/\* Zend_Code_Generator_FileGenerator -DocBlockMarker \ */#m ' , $ docBlock ->generate (),
480
444
$ output , 1 );
481
445
} else {
482
446
$ output .= $ docBlock ->generate () . self ::LINE_FEED ;
@@ -489,7 +453,13 @@ public function generate()
489
453
// namespace, if any
490
454
$ namespace = $ this ->getNamespace ();
491
455
if ($ namespace ) {
492
- $ output .= sprintf ('namespace %s;%s ' , $ namespace , str_repeat (self ::LINE_FEED , 2 ));
456
+ $ namespace = sprintf ('namespace %s;%s ' , $ namespace , str_repeat (self ::LINE_FEED , 2 ));
457
+ if (preg_match ('#/\* Zend_Code_Generator_FileGenerator-NamespaceMarker \*/#m ' , $ output )) {
458
+ $ output = preg_replace ('#/\* Zend_Code_Generator_FileGenerator-NamespaceMarker \*/#m ' , $ namespace ,
459
+ $ output , 1 );
460
+ } else {
461
+ $ output .= $ namespace ;
462
+ }
493
463
}
494
464
495
465
// process required files
@@ -503,28 +473,53 @@ public function generate()
503
473
$ output .= self ::LINE_FEED ;
504
474
}
505
475
476
+ $ classes = $ this ->getClasses ();
477
+ $ classUses = array ();
478
+ //build uses array
479
+ foreach ($ classes as $ class ) {
480
+ //check for duplicate use statements
481
+ $ uses = $ class ->getUses ();
482
+ if (!empty ($ uses ) && is_array ($ uses )) {
483
+ $ classUses = array_merge ($ classUses , $ uses );
484
+ }
485
+ }
486
+
506
487
// process import statements
507
488
$ uses = $ this ->getUses ();
508
489
if (!empty ($ uses )) {
490
+ $ useOutput = '' ;
491
+
509
492
foreach ($ uses as $ use ) {
510
493
list ($ import , $ alias ) = $ use ;
511
494
if (null === $ alias ) {
512
- $ output . = sprintf ('use %s;%s ' , $ import, self :: LINE_FEED );
495
+ $ tempOutput = sprintf ('%s ' , $ import );
513
496
} else {
514
- $ output .= sprintf ('use %s as %s;%s ' , $ import , $ alias , self ::LINE_FEED );
497
+ $ tempOutput = sprintf ('%s as %s ' , $ import , $ alias );
498
+ }
499
+
500
+ //don't duplicate use statements
501
+ if (!in_array ($ tempOutput , $ classUses )) {
502
+ $ useOutput .= "use " . $ tempOutput ."; " ;
503
+ $ useOutput .= self ::LINE_FEED ;
515
504
}
516
505
}
517
- $ output .= self ::LINE_FEED ;
506
+ $ useOutput .= self ::LINE_FEED ;
507
+
508
+ if (preg_match ('#/\* Zend_Code_Generator_FileGenerator-UseMarker \*/#m ' , $ output )) {
509
+ $ output = preg_replace ('#/\* Zend_Code_Generator_FileGenerator-UseMarker \*/#m ' , $ useOutput ,
510
+ $ output , 1 );
511
+ } else {
512
+ $ output .= $ useOutput ;
513
+ }
514
+
518
515
}
519
516
520
517
// process classes
521
- $ classes = $ this ->getClasses ();
522
518
if (!empty ($ classes )) {
523
519
foreach ($ classes as $ class ) {
524
- $ regex = str_replace ('? ' , $ class ->getName (),
525
- '/* Zend_Code_Generator_FileGenerator-ClassMarker: {?} */ ' );
526
- $ regex = preg_quote ($ regex , '# ' );
527
- if (preg_match ('# ' . $ regex . '# ' , $ output )) {
520
+ $ regex = str_replace ('& ' , $ class ->getName (),
521
+ '/\* Zend_Code_Generator_Php_File-ClassMarker: \{[A-Za-z0-9 \\\]+?&\} \*/ ' );
522
+ if (preg_match ('# ' . $ regex . '#m ' , $ output )) {
528
523
$ output = preg_replace ('# ' . $ regex . '# ' , $ class ->generate (), $ output , 1 );
529
524
} else {
530
525
if ($ namespace ) {
0 commit comments