| 1 | <?php |
||
| 19 | class EvaluatingGeneratorStrategy implements GeneratorStrategyInterface |
||
| 20 | { |
||
| 21 | /** @var bool flag indicating whether {@see eval} can be used */ |
||
| 22 | private bool $canEval = true; |
||
|
|
|||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor |
||
| 26 | */ |
||
| 27 | public function __construct() |
||
| 28 | 1 | { |
|
| 29 | // @codeCoverageIgnoreStart |
||
| 30 | $this->canEval = ! ini_get('suhosin.executor.disable_eval'); |
||
| 31 | // @codeCoverageIgnoreEnd |
||
| 32 | } |
||
| 33 | 1 | ||
| 34 | /** |
||
| 35 | * Evaluates the generated code before returning it |
||
| 36 | * |
||
| 37 | * {@inheritDoc} |
||
| 38 | */ |
||
| 39 | public function generate(ClassGenerator $classGenerator) : string |
||
| 40 | 1 | { |
|
| 41 | $code = $classGenerator->generate(); |
||
| 42 | 1 | ||
| 43 | // @codeCoverageIgnoreStart |
||
| 44 | if (! $this->canEval) { |
||
| 45 | $fileName = tempnam(sys_get_temp_dir(), 'EvaluatingGeneratorStrategy.php.tmp.'); |
||
| 46 | |||
| 47 | assert(is_string($fileName)); |
||
| 48 | |||
| 49 | file_put_contents($fileName, "<?php\n" . $code); |
||
| 50 | /* @noinspection PhpIncludeInspection */ |
||
| 51 | require $fileName; |
||
| 52 | unlink($fileName); |
||
| 53 | |||
| 54 | return $code; |
||
| 55 | } |
||
| 56 | // @codeCoverageIgnoreEnd |
||
| 57 | |||
| 58 | eval($code); |
||
| 59 | 1 | ||
| 60 | return $code; |
||
| 61 | 1 | } |
|
| 62 | } |
||
| 63 |