13
13
* @copyright Copyright (c) 2008 Yii Software LLC
14
14
* @license http://www.yiiframework.com/license/
15
15
*/
16
-
17
- if (!extension_loaded ('mcrypt ' )) {
18
- die ('The mcrypt PHP extension is required by Yii2. ' );
16
+ if (!extension_loaded ('openssl ' )) {
17
+ die ('The OpenSSL PHP extension is required by Yii2. ' );
19
18
}
20
-
21
19
$ params = getParams ();
22
20
$ root = str_replace ('\\' , '/ ' , __DIR__ );
23
21
$ envs = require ("$ root/environments/index.php " );
24
22
$ envNames = array_keys ($ envs );
25
-
26
23
echo "Yii Application Initialization Tool v1.0 \n\n" ;
27
-
28
24
$ envName = null ;
29
25
if (empty ($ params ['env ' ]) || $ params ['env ' ] === '1 ' ) {
30
26
echo "Which environment do you want the application to be initialized in? \n\n" ;
@@ -33,27 +29,22 @@ if (empty($params['env']) || $params['env'] === '1') {
33
29
}
34
30
echo "\n Your choice [0- " . (count ($ envs ) - 1 ) . ', or "q" to quit] ' ;
35
31
$ answer = trim (fgets (STDIN ));
36
-
37
32
if (!ctype_digit ($ answer ) || !in_array ($ answer , range (0 , count ($ envs ) - 1 ))) {
38
33
echo "\n Quit initialization. \n" ;
39
34
exit (0 );
40
35
}
41
-
42
36
if (isset ($ envNames [$ answer ])) {
43
37
$ envName = $ envNames [$ answer ];
44
38
}
45
39
} else {
46
40
$ envName = $ params ['env ' ];
47
41
}
48
-
49
42
if (!in_array ($ envName , $ envNames )) {
50
43
$ envsList = implode (', ' , $ envNames );
51
44
echo "\n $ envName is not a valid environment. Try one of the following: $ envsList. \n" ;
52
45
exit (2 );
53
46
}
54
-
55
47
$ env = $ envs [$ envName ];
56
-
57
48
if (empty ($ params ['env ' ])) {
58
49
echo "\n Initialize the application under ' {$ envNames [$ answer ]}' environment? [yes|no] " ;
59
50
$ answer = trim (fgets (STDIN ));
@@ -62,25 +53,26 @@ if (empty($params['env'])) {
62
53
exit (0 );
63
54
}
64
55
}
65
-
66
56
echo "\n Start initialization ... \n\n" ;
67
57
$ files = getFileList ("$ root/environments/ {$ env ['path ' ]}" );
58
+ if (isset ($ env ['skipFiles ' ])) {
59
+ $ skipFiles = $ env ['skipFiles ' ];
60
+ array_walk ($ skipFiles , function (&$ value ) use ($ env , $ root ) { $ value = "$ root/ $ value " ; });
61
+ $ files = array_diff ($ files , array_intersect_key ($ env ['skipFiles ' ], array_filter ($ skipFiles , 'file_exists ' )));
62
+ }
68
63
$ all = false ;
69
64
foreach ($ files as $ file ) {
70
65
if (!copyFile ($ root , "environments/ {$ env ['path ' ]}/ $ file " , $ file , $ all , $ params )) {
71
66
break ;
72
67
}
73
68
}
74
-
75
- $ callbacks = ['setCookieValidationKey ' , 'setWritable ' , 'setExecutable ' ];
69
+ $ callbacks = ['setCookieValidationKey ' , 'setWritable ' , 'setExecutable ' , 'createSymlink ' ];
76
70
foreach ($ callbacks as $ callback ) {
77
71
if (!empty ($ env [$ callback ])) {
78
72
$ callback ($ root , $ env [$ callback ]);
79
73
}
80
74
}
81
-
82
75
echo "\n ... initialization completed. \n\n" ;
83
-
84
76
function getFileList ($ root , $ basePath = '' )
85
77
{
86
78
$ files = [];
@@ -100,7 +92,6 @@ function getFileList($root, $basePath = '')
100
92
closedir ($ handle );
101
93
return $ files ;
102
94
}
103
-
104
95
function copyFile ($ root , $ source , $ target , &$ all , $ params )
105
96
{
106
97
if (!is_file ($ root . '/ ' . $ source )) {
@@ -117,8 +108,6 @@ function copyFile($root, $source, $target, &$all, $params)
117
108
} else {
118
109
echo " exist $ target \n" ;
119
110
echo " ...overwrite? [Yes|No|All|Quit] " ;
120
-
121
-
122
111
$ answer = !empty ($ params ['overwrite ' ]) ? $ params ['overwrite ' ] : trim (fgets (STDIN ));
123
112
if (!strncasecmp ($ answer , 'q ' , 1 )) {
124
113
return false ;
@@ -144,15 +133,13 @@ function copyFile($root, $source, $target, &$all, $params)
144
133
file_put_contents ($ root . '/ ' . $ target , file_get_contents ($ root . '/ ' . $ source ));
145
134
return true ;
146
135
}
147
-
148
136
function getParams ()
149
137
{
150
138
$ rawParams = [];
151
139
if (isset ($ _SERVER ['argv ' ])) {
152
140
$ rawParams = $ _SERVER ['argv ' ];
153
141
array_shift ($ rawParams );
154
142
}
155
-
156
143
$ params = [];
157
144
foreach ($ rawParams as $ param ) {
158
145
if (preg_match ('/^--(\w+)(=(.*))?$/ ' , $ param , $ matches )) {
@@ -164,32 +151,37 @@ function getParams()
164
151
}
165
152
return $ params ;
166
153
}
167
-
168
154
function setWritable ($ root , $ paths )
169
155
{
170
156
foreach ($ paths as $ writable ) {
171
157
echo " chmod 0777 $ writable \n" ;
172
158
@chmod ("$ root/ $ writable " , 0777 );
173
159
}
174
160
}
175
-
176
161
function setExecutable ($ root , $ paths )
177
162
{
178
163
foreach ($ paths as $ executable ) {
179
164
echo " chmod 0755 $ executable \n" ;
180
165
@chmod ("$ root/ $ executable " , 0755 );
181
166
}
182
167
}
183
-
184
168
function setCookieValidationKey ($ root , $ paths )
185
169
{
186
170
foreach ($ paths as $ file ) {
187
171
echo " generate cookie validation key in $ file \n" ;
188
172
$ file = $ root . '/ ' . $ file ;
189
173
$ length = 32 ;
190
- $ bytes = mcrypt_create_iv ($ length, MCRYPT_DEV_URANDOM );
174
+ $ bytes = openssl_random_pseudo_bytes ($ length );
191
175
$ key = strtr (substr (base64_encode ($ bytes ), 0 , $ length ), '+/= ' , '_-. ' );
192
176
$ content = preg_replace ('/(("| \')cookieValidationKey("| \')\s*=>\s*)(""| \'\')/ ' , "\\1' $ key' " , file_get_contents ($ file ));
193
177
file_put_contents ($ file , $ content );
194
178
}
195
179
}
180
+ function createSymlink ($ root , $ links ) {
181
+ foreach ($ links as $ link => $ target ) {
182
+ echo " symlink " . $ root . "/ " . $ target . " " . $ root . "/ " . $ link . "\n" ;
183
+ //first removing folders to avoid errors if the folder already exists
184
+ @rmdir ($ root . "/ " . $ link );
185
+ @symlink ($ root . "/ " . $ target , $ root . "/ " . $ link );
186
+ }
187
+ }
0 commit comments