forked from bazilio91/yii2-stubs-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StubsController.php
154 lines (129 loc) · 3.83 KB
/
StubsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace bazilio\stubsgenerator;
use yii\console\Controller;
use yii\console\Exception;
use yii\di\Instance;
class StubsController extends Controller
{
public $outputFile = null;
protected function getTemplate()
{
return <<<TPL
<?php
/**
* Yii app stub file. Autogenerated by yii2-stubs-generator (stubs console command).
* Used for enhanced IDE code autocompletion.
* Updated on {time}
*/
class Yii extends \yii\BaseYii
{
/**
* @var BaseApplication|WebApplication|ConsoleApplication the application instance
*/
public static \$app;
}
/**{stubs}
**/
abstract class BaseApplication extends yii\base\Application
{
}
/**{stubs}
**/
class WebApplication extends yii\web\Application
{
}
/**{stubs}
**/
class ConsoleApplication extends yii\console\Application
{
}
TPL;
}
protected function getUserTemplate()
{
return <<<TPL
/**
* @property {user_identities} \$identity
*/
class User extends \yii\web\User {
}
TPL;
}
public function actionIndex()
{
$path = $this->outputFile ? $this->outputFile :
\Yii::$app->getVendorPath() . DIRECTORY_SEPARATOR . 'Yii.php';
$components = [];
$userIdentities = [];
foreach (array_slice(\Yii::$app->getRequest()->getParams(), 1) as $configPath) {
if (!file_exists($configPath)) {
throw new Exception('Config file doesn\'t exists: ' . $configPath);
}
$config = include($configPath);
if (empty($config['components'])) {
continue;
}
foreach ($config['components'] as $name => $component) {
if (is_string($component)) {
$component = ['class' => $component];
}
if ($name === 'user' && isset($component['identityClass'])) {
$userIdentities[] = $component['identityClass'];
}
if (isset($component['class'])) {
$class = $component['class'];
} elseif (isset($component['__class'])) {
$class = $component['class'];
}
if (isset($class)) {
if ($class instanceof Instance)
{
$components[$name][] = get_class($class->get());
} else {
$components[$name][] = $class;
}
}
}
}
$stubs = '';
$userStubs = '';
if (sizeof($userIdentities)) {
$components['user'][] = 'User';
$userIdentities = implode('|', array_unique($userIdentities));
$userStubs = str_replace(
'{user_identities}',
$userIdentities,
$this->getUserTemplate()
);
}
foreach ($components as $name => $classes) {
$classes = implode('|', array_unique($classes));
$stubs .= "\n * @property {$classes} \$$name";
if (in_array($name, [
'db',
'log',
'cache',
'formatter',
'request',
'response',
'errorHandler',
'view',
'urlManager',
'i18n',
'authManager',
'assetManager',
'security',
'session',
'user',
])) {
$stubs .= "\n * @method {$classes} get" . ucfirst($name) . "()";
}
}
$content = str_replace('{stubs}', $stubs, $this->getTemplate());
$content = str_replace('{time}', date(DATE_ISO8601), $content);
$content .= $userStubs;
if ($content != @file_get_contents($path)) {
file_put_contents($path, $content);
}
}
}