|
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; |
|
12
|
|
|
use yii\base\Application; |
|
13
|
|
|
use yii\base\BaseObject; |
|
14
|
|
|
|
|
15
|
|
|
class Detector extends BaseObject |
|
16
|
|
|
{ |
|
17
|
|
|
public ?Application $app = null; |
|
18
|
|
|
public array $ids = [ |
|
19
|
|
|
'basic-console' => 'basic', |
|
20
|
|
|
'app-console' => 'advanced', |
|
21
|
|
|
]; |
|
22
|
|
|
public array $configs = [ |
|
23
|
|
|
'basic' => [ |
|
24
|
|
|
'@app/config/console.php', |
|
25
|
|
|
'@app/config/web.php', |
|
26
|
|
|
], |
|
27
|
|
|
'advanced' => [ |
|
28
|
|
|
'@common/config/main.php', |
|
29
|
|
|
'@common/config/main-local.php', |
|
30
|
|
|
'@console/config/main.php', |
|
31
|
|
|
'@console/config/main-local.php', |
|
32
|
|
|
'@backend/config/main.php', |
|
33
|
|
|
'@backend/config/main-local.php', |
|
34
|
|
|
'@frontend/config/main.php', |
|
35
|
|
|
'@frontend/config/main-local.php', |
|
36
|
|
|
], |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
public function detect(): bool|string |
|
40
|
|
|
{ |
|
41
|
|
|
$application = $this->getApplication(); |
|
42
|
|
|
|
|
43
|
|
|
if (isset($this->ids[$application->id])) { |
|
44
|
|
|
return $this->ids[$application->id]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getConfig(): array |
|
51
|
|
|
{ |
|
52
|
|
|
if ($type = $this->detect()) { |
|
53
|
|
|
return $this->configs[$type] ?? []; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return []; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function getApplication(): Application |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$this->app instanceof Application) { |
|
62
|
|
|
$this->app = Yii::$app; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->app; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|