1 | <?php declare(strict_types=1); |
||
2 | /** |
||
3 | * @author Vitaliy IIIFX Khomenko (c) 2021 |
||
4 | * @license MIT |
||
5 | * |
||
6 | * @link https://github.com/iiifx-production/yii2-autocomplete-helper |
||
7 | */ |
||
8 | |||
9 | namespace iiifx\Yii2\Autocomplete; |
||
10 | |||
11 | use yii\base\BaseObject; |
||
12 | |||
13 | class Builder extends BaseObject |
||
14 | { |
||
15 | public ?string $template = null; |
||
16 | public array $components = []; |
||
17 | public ?string $webAppClass = null; |
||
18 | public ?string $consoleAppClass = null; |
||
19 | |||
20 | public function build(string|false $file = null): bool|string |
||
21 | { |
||
22 | $prepared = preg_replace_callback('/%.*%/U', function ($m) { |
||
23 | if ($m[0] === '%phpdoc%') { |
||
24 | $string = '/**'; |
||
25 | |||
26 | foreach ($this->components as $name => $classes) { |
||
27 | $string .= PHP_EOL . ' * @property ' . implode('|', $classes) . ' $' . $name; |
||
28 | } |
||
29 | $string .= PHP_EOL . ' */'; |
||
30 | |||
31 | return $string; |
||
32 | } |
||
33 | |||
34 | if ($m[0] === '%webapp%') { |
||
35 | return $this->webAppClass ?? '\yii\web\Application'; |
||
36 | } |
||
37 | |||
38 | if ($m[0] === '%consoleapp%') { |
||
39 | return $this->consoleAppClass ?? '\yii\console\Application'; |
||
40 | } |
||
41 | |||
42 | return $m[0]; |
||
43 | }, $this->template); |
||
44 | |||
45 | if ($file === null) { |
||
46 | return $prepared; |
||
47 | } |
||
48 | |||
49 | return (bool)file_put_contents($file, $prepared); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
50 | } |
||
51 | } |
||
52 |