iiifx-production /
yii2-autocomplete-helper
| 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 Exception; |
||
| 12 | use Yii; |
||
| 13 | use yii\base\InvalidCallException; |
||
| 14 | use yii\base\InvalidConfigException; |
||
| 15 | use yii\helpers\Console; |
||
| 16 | use yii\helpers\FileHelper; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Automatically generate IDE auto-completion file |
||
| 20 | * |
||
| 21 | * @package iiifx\Yii2\Autocomplete |
||
| 22 | */ |
||
| 23 | class Controller extends \yii\console\Controller |
||
| 24 | { |
||
| 25 | public string $component = 'autocomplete'; |
||
| 26 | public ?string $config = null; |
||
| 27 | protected ?Detector $detector = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @inheritdoc |
||
| 31 | */ |
||
| 32 | public function options($actionID = null): array |
||
| 33 | { |
||
| 34 | return [ |
||
| 35 | 'component', |
||
| 36 | 'config', |
||
| 37 | ]; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @inheritdoc |
||
| 42 | */ |
||
| 43 | public function optionAliases(): array |
||
| 44 | { |
||
| 45 | return []; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function showDescription(): void |
||
| 49 | { |
||
| 50 | $this->stdout("Yii2 IDE auto-completion helper\n"); |
||
| 51 | $this->stdout("Vitaliy IIIFX Khomenko, 2021\n\n"); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Generate IDE auto-completion file |
||
| 56 | */ |
||
| 57 | public function actionIndex(): void |
||
| 58 | { |
||
| 59 | $this->showDescription(); |
||
| 60 | |||
| 61 | try { |
||
| 62 | $component = $this->getComponent(); |
||
| 63 | $configList = $this->getConfig($component); |
||
| 64 | |||
| 65 | $config = new Config([ |
||
| 66 | 'files' => $configList, |
||
| 67 | ]); |
||
| 68 | |||
| 69 | $builder = new Builder([ |
||
| 70 | 'components' => $config->getComponents(), |
||
| 71 | 'template' => require __DIR__ . '/template.php', |
||
| 72 | 'webAppClass' => $component->webAppClass, |
||
| 73 | 'consoleAppClass' => $component->consoleAppClass, |
||
| 74 | ]); |
||
| 75 | |||
| 76 | if (null === $component->result) { |
||
| 77 | $component->result = ($this->getDetector()->detect() === 'basic') |
||
| 78 | ? '@app/_ide_components.php' |
||
| 79 | : '@console/../_ide_components.php'; |
||
| 80 | } |
||
| 81 | |||
| 82 | $result = Yii::getAlias($component->result); |
||
| 83 | $result = FileHelper::normalizePath($result); |
||
| 84 | |||
| 85 | if ($builder->build($result)) { |
||
| 86 | $this->stdout("Success: {$result}\n", Console::FG_GREEN); |
||
| 87 | } else { |
||
| 88 | $this->stdout("Fail!\n", Console::FG_RED); |
||
| 89 | } |
||
| 90 | } catch (Exception $exception) { |
||
| 91 | $this->stdout($exception->getMessage() . "\n\n", Console::FG_RED); |
||
| 92 | $this->stdout("Please read the package documentation: https://github.com/iiifx-production/yii2-autocomplete-helper\n"); |
||
| 93 | $this->stdout("or create new issue: https://github.com/iiifx-production/yii2-autocomplete-helper/issues/new\n"); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return Component |
||
| 99 | * |
||
| 100 | * @throws InvalidConfigException |
||
| 101 | */ |
||
| 102 | protected function getComponent(): Component |
||
| 103 | { |
||
| 104 | if (isset(Yii::$app->{$this->component}) && Yii::$app->{$this->component} instanceof Component) { |
||
| 105 | return Yii::$app->{$this->component}; |
||
| 106 | } |
||
| 107 | |||
| 108 | throw new InvalidConfigException(sprintf('Component "%s" not found in Yii::$app', $this->component)); |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function getDetector(): Detector |
||
| 112 | { |
||
| 113 | if ($this->detector === null) { |
||
| 114 | $this->detector = new Detector([ |
||
| 115 | 'app' => Yii::$app, |
||
| 116 | ]); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $this->detector; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param Component $component |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | * |
||
| 127 | * @throws InvalidCallException |
||
| 128 | */ |
||
| 129 | protected function getConfig(Component $component): array |
||
| 130 | { |
||
| 131 | if (null === $component->config) { |
||
| 132 | if ($this->getDetector()->detect() === false) { |
||
| 133 | throw new InvalidCallException('Unable to determine application type'); |
||
| 134 | } |
||
| 135 | |||
| 136 | $configList = $this->getDetector()->getConfig(); |
||
| 137 | } else { |
||
| 138 | if (null === $this->config) { |
||
| 139 | if (isset($component->config[0])) { |
||
| 140 | $configList = $component->config; |
||
| 141 | } else { |
||
| 142 | throw new InvalidCallException('Default config list not found in component config data'); |
||
| 143 | } |
||
| 144 | } else { |
||
| 145 | if (isset($component->config[$this->config])) { |
||
| 146 | $configList = $component->config[$this->config]; |
||
| 147 | } else { |
||
| 148 | throw new InvalidCallException(sprintf('Scope "%s" not found in component config data', $this->config)); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return $configList; |
||
| 154 | } |
||
| 155 | } |
||
| 156 |