|
9 | 9 |
|
10 | 10 | namespace Zend\Code\Generator;
|
11 | 11 |
|
| 12 | +use Zend\Code\DeclareStatement; |
| 13 | +use Zend\Code\Exception\InvalidArgumentException; |
12 | 14 | use Zend\Code\Reflection\Exception as ReflectionException;
|
13 | 15 | use Zend\Code\Reflection\FileReflection;
|
14 | 16 |
|
@@ -72,6 +74,11 @@ class FileGenerator extends AbstractGenerator
|
72 | 74 | */
|
73 | 75 | protected $body;
|
74 | 76 |
|
| 77 | + /** |
| 78 | + * @var DeclareStatement[] |
| 79 | + */ |
| 80 | + protected $declares = []; |
| 81 | + |
75 | 82 | /**
|
76 | 83 | * Passes $options to {@link setOptions()}.
|
77 | 84 | *
|
@@ -166,6 +173,11 @@ public static function fromArray(array $values)
|
166 | 173 | case 'requiredfiles':
|
167 | 174 | $fileGenerator->setRequiredFiles($value);
|
168 | 175 | break;
|
| 176 | + case 'declares': |
| 177 | + $fileGenerator->setDeclares(array_map(static function ($directive, $value) { |
| 178 | + return DeclareStatement::fromArray([$directive => $value]); |
| 179 | + }, array_keys($value), $value)); |
| 180 | + break; |
169 | 181 | default:
|
170 | 182 | if (property_exists($fileGenerator, $name)) {
|
171 | 183 | $fileGenerator->{$name} = $value;
|
@@ -408,6 +420,25 @@ public function getBody()
|
408 | 420 | return $this->body;
|
409 | 421 | }
|
410 | 422 |
|
| 423 | + public function setDeclares(array $declares) |
| 424 | + { |
| 425 | + foreach ($declares as $declare) { |
| 426 | + if (! $declare instanceof DeclareStatement) { |
| 427 | + throw new InvalidArgumentException(sprintf( |
| 428 | + '%s is expecting an array of %s objects', |
| 429 | + __METHOD__, |
| 430 | + DeclareStatement::class |
| 431 | + )); |
| 432 | + } |
| 433 | + |
| 434 | + if (! array_key_exists($declare->getDirective(), $this->declares)) { |
| 435 | + $this->declares[$declare->getDirective()] = $declare; |
| 436 | + } |
| 437 | + } |
| 438 | + |
| 439 | + return $this; |
| 440 | + } |
| 441 | + |
411 | 442 | /**
|
412 | 443 | * @return bool
|
413 | 444 | */
|
@@ -491,6 +522,28 @@ public function generate()
|
491 | 522 | }
|
492 | 523 | }
|
493 | 524 |
|
| 525 | + // declares, if any |
| 526 | + if ($this->declares) { |
| 527 | + $declareStatements = ''; |
| 528 | + |
| 529 | + foreach ($this->declares as $declare) { |
| 530 | + $declareStatements .= $declare->getStatement() . self::LINE_FEED; |
| 531 | + } |
| 532 | + |
| 533 | + if (preg_match('#/\* Zend_Code_Generator_FileGenerator-DeclaresMarker \*/#m', $output)) { |
| 534 | + $output = preg_replace( |
| 535 | + '#/\* Zend_Code_Generator_FileGenerator-DeclaresMarker \*/#m', |
| 536 | + $declareStatements, |
| 537 | + $output, |
| 538 | + 1 |
| 539 | + ); |
| 540 | + } else { |
| 541 | + $output .= $declareStatements; |
| 542 | + } |
| 543 | + |
| 544 | + $output .= self::LINE_FEED; |
| 545 | + } |
| 546 | + |
494 | 547 | // process required files
|
495 | 548 | // @todo marker replacement for required files
|
496 | 549 | $requiredFiles = $this->getRequiredFiles();
|
|
0 commit comments