forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnvironment.php
508 lines (461 loc) · 15.2 KB
/
Environment.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
namespace Drush\Config;
use Composer\Autoload\ClassLoader;
use Drush\Drush;
use Drush\Utils\FsUtils;
use Webmozart\PathUtil\Path;
/**
* Store information about the environment
*/
class Environment
{
protected $homeDir;
protected $originalCwd;
protected $etcPrefix;
protected $sharePrefix;
protected $drushBasePath;
protected $vendorDir;
protected $docPrefix;
protected $configFileVariant;
protected $loader;
protected $siteLoader;
/**
* Environment constructor
* @param string $homeDir User home directory.
* @param string $cwd The current working directory at the time Drush was called.
* @param string $autoloadFile Path to the autoload.php file.
*/
public function __construct($homeDir, $cwd, $autoloadFile)
{
$this->homeDir = $homeDir;
$this->originalCwd = Path::canonicalize(FsUtils::realpath($cwd));
$this->etcPrefix = '';
$this->sharePrefix = '';
$this->drushBasePath = Path::canonicalize(dirname(dirname(__DIR__)));
$this->vendorDir = FsUtils::realpath(dirname($autoloadFile));
}
/**
* Load the autoloader for the selected Drupal site
*
* @param string $root
* @return ClassLoader
*/
public function loadSiteAutoloader($root)
{
$autloadFilePath = "$root/autoload.php";
if (!file_exists($autloadFilePath)) {
return $this->loader;
}
if ($this->siteLoader) {
return $this->siteLoader;
}
$this->siteLoader = require $autloadFilePath;
if ($this->siteLoader === false) {
// Nothing more to do. See https://github.com/drush-ops/drush/issues/3741.
return $this->loader;
}
if ($this->siteLoader === true) {
// The autoloader was already required. Assume that Drush and Drupal share an autoloader per
// "Point autoload.php to the proper vendor directory" - https://www.drupal.org/node/2404989
$this->siteLoader = $this->loader;
}
// Ensure that the site's autoloader has highest priority. Usually,
// the first classloader registered gets the first shot at loading classes.
// We want Drupal's classloader to be used first when a class is loaded,
// and have Drush's classloader only be called as a fallback measure.
$this->siteLoader->unregister();
$this->siteLoader->register(true);
return $this->siteLoader;
}
/**
* Return the name of the user running drush.
*
* @return string
*/
protected function getUsername()
{
$name = null;
if (!$name = getenv("username")) { // Windows
if (!$name = getenv("USER")) {
// If USER not defined, use posix
if (function_exists('posix_getpwuid')) {
$processUser = posix_getpwuid(posix_geteuid());
$name = $processUser['name'];
}
}
}
return $name;
}
protected function getTmp()
{
$directories = [];
// Get user specific and operating system temp folders from system environment variables.
// See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true
$tempdir = getenv('TEMP');
if (!empty($tempdir)) {
$directories[] = $tempdir;
}
$tmpdir = getenv('TMP');
if (!empty($tmpdir)) {
$directories[] = $tmpdir;
}
// Operating system specific dirs.
if (self::isWindows()) {
$windir = getenv('WINDIR');
if (isset($windir)) {
// WINDIR itself is not writable, but it always contains a /Temp dir,
// which is the system-wide temporary directory on older versions. Newer
// versions only allow system processes to use it.
$directories[] = Path::join($windir, 'Temp');
}
} else {
$directories[] = Path::canonicalize('/tmp');
}
$directories[] = Path::canonicalize(sys_get_temp_dir());
foreach ($directories as $directory) {
if (is_dir($directory) && is_writable($directory)) {
$temporary_directory = $directory;
break;
}
}
if (empty($temporary_directory)) {
// If no directory has been found, create one in cwd.
$temporary_directory = Path::join(Drush::config()->cwd(), 'tmp');
drush_mkdir($temporary_directory, true);
if (!is_dir($temporary_directory)) {
throw new \Exception(dt("Unable to create a temporary directory."));
}
// Function not available yet - this is not likely to get reached anyway.
// drush_register_file_for_deletion($temporary_directory);
}
return $temporary_directory;
}
/**
* Convert the environment object into an exported configuration
* array.
*
* @see PreflightArgs::applyToConfig(), which also exports information to config.
*
* @return array Nested associative array that is overlayed on configuration.
*/
public function exportConfigData()
{
return [
// Information about the environment presented to Drush
'env' => [
'cwd' => $this->cwd(),
'home' => $this->homeDir(),
'user' => $this->getUsername(),
'is-windows' => $this->isWindows(),
'tmp' => $this->getTmp(),
],
// These values are available as global options, and
// will be passed in to the FormatterOptions et. al.
'options' => [
'width' => $this->calculateColumns(),
],
// Information about the directories where Drush found assets, etc.
'drush' => [
'base-dir' => $this->drushBasePath,
'vendor-dir' => $this->vendorPath(),
'docs-dir' => $this->docsPath(),
'user-dir' => $this->userConfigPath(),
'system-dir' => $this->systemConfigPath(),
'system-command-dir' => $this->systemCommandFilePath(),
],
'runtime' => [
'site-file-previous' => $this->getSiteSetAliasFilePath('drush-drupal-prev-site-'),
'site-file-current' => $this->getSiteSetAliasFilePath(),
],
];
}
/**
* The base directory of the Drush application itself
* (where composer.json et.al. are found)
*
* @return string
*/
public function drushBasePath()
{
return $this->drushBasePath;
}
/**
* Get the site:set alias from the current site:set file path.
*
* @return bool|string
*/
public function getSiteSetAliasName()
{
$site_filename = $this->getSiteSetAliasFilePath();
if (file_exists($site_filename)) {
$site = file_get_contents($site_filename);
if ($site) {
return $site;
}
}
return false;
}
/**
* User's home directory
*
* @return string
*/
public function homeDir()
{
return $this->homeDir;
}
/**
* The user's Drush configuration directory, ~/.drush
*
* @return string
*/
public function userConfigPath()
{
return $this->homeDir() . '/.drush';
}
public function setConfigFileVariant($variant)
{
$this->configFileVariant = $variant;
}
/**
* Get the config file variant -- defined to be
* the Drush major version number. This is for
* loading drush.yml and drush10.yml, etc.
*/
public function getConfigFileVariant()
{
return $this->configFileVariant;
}
/**
* The original working directory
*
* @return string
*/
public function cwd()
{
return $this->originalCwd;
}
/**
* Return the path to Drush's vendor directory
*
* @return string
*/
public function vendorPath()
{
return $this->vendorDir;
}
/**
* The class loader returned when the autoload.php file is included.
*
* @return \Composer\Autoload\ClassLoader
*/
public function loader()
{
return $this->loader;
}
/**
* Set the class loader from the autload.php file, if available.
*
* @param \Composer\Autoload\ClassLoader $loader
*/
public function setLoader(ClassLoader $loader)
{
$this->loader = $loader;
}
/**
* Alter our default locations based on the value of environment variables
*
* @return $this
*/
public function applyEnvironment()
{
// Copy ETC_PREFIX and SHARE_PREFIX from environment variables if available.
// This alters where we check for server-wide config and alias files.
// Used by unit test suite to provide a clean environment.
$this->setEtcPrefix(getenv('ETC_PREFIX'));
$this->setSharePrefix(getenv('SHARE_PREFIX'));
return $this;
}
/**
* Set the directory prefix to locate the directory that Drush will
* use as /etc (e.g. during the functional tests)
*
* @param string $etcPrefix
* @return $this
*/
public function setEtcPrefix($etcPrefix)
{
if (isset($etcPrefix)) {
$this->etcPrefix = $etcPrefix;
}
return $this;
}
/**
* Set the directory prefix to locate the directory that Drush will
* use as /user/share (e.g. during the functional tests)
* @param string $sharePrefix
* @return $this
*/
public function setSharePrefix($sharePrefix)
{
if (isset($sharePrefix)) {
$this->sharePrefix = $sharePrefix;
$this->docPrefix = null;
}
return $this;
}
/**
* Return the directory where Drush's documentation is stored. Usually
* this is within the Drush application, but some Drush RPM distributions
* & c. for Linux platforms slice-and-dice the contents and put the docs
* elsewhere.
*
* @return string
*/
public function docsPath()
{
if (!$this->docPrefix) {
$this->docPrefix = $this->findDocsPath($this->drushBasePath);
}
return $this->docPrefix;
}
/**
* Locate the Drush documentation. This is recalculated whenever the
* share prefix is changed.
*
* @param string $drushBasePath
* @return string
*/
protected function findDocsPath($drushBasePath)
{
$candidates = [
"$drushBasePath/README.md",
static::systemPathPrefix($this->sharePrefix, '/usr') . '/share/docs/drush/README.md',
];
return $this->findFromCandidates($candidates);
}
/**
* Check a list of directories and return the first one that exists.
*
* @param array $candidates
* @return string|boolean
*/
protected function findFromCandidates($candidates)
{
foreach ($candidates as $candidate) {
if (file_exists($candidate)) {
return dirname($candidate);
}
}
return false;
}
/**
* Return the appropriate system path prefix, unless an override is provided.
* @param string $override
* @param string $defaultPrefix
* @return string
*/
protected static function systemPathPrefix($override = '', $defaultPrefix = '')
{
if ($override) {
return $override;
}
return static::isWindows() ? Path::join(getenv('ALLUSERSPROFILE'), 'Drush') : $defaultPrefix;
}
/**
* Return the system configuration path (default: /etc/drush)
*
* @return string
*/
public function systemConfigPath()
{
return static::systemPathPrefix($this->etcPrefix, '') . '/etc/drush';
}
/**
* Return the system shared commandfile path (default: /usr/share/drush/commands)
*
* @return string
*/
public function systemCommandFilePath()
{
return static::systemPathPrefix($this->sharePrefix, '/usr') . '/share/drush/commands';
}
/**
* Determine whether current OS is a Windows variant.
*
* @return boolean
*/
public static function isWindows($os = null)
{
return strtoupper(substr($os ?: PHP_OS, 0, 3)) === 'WIN';
}
/**
* Verify that we are running PHP through the command line interface.
*
* @return boolean
* A boolean value that is true when PHP is being run through the command line,
* and false if being run through cgi or mod_php.
*/
public function verifyCLI()
{
return (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0));
}
/**
* Calculate the terminal width used for wrapping table output.
* Normally this is exported using tput in the drush script.
* If this is not present we do an additional check using stty here.
* On Windows in CMD and PowerShell is this exported using mode con.
*
* @return integer
*/
public function calculateColumns()
{
if ($columns = getenv('COLUMNS')) {
return $columns;
}
// Trying to export the columns using stty.
exec('stty size 2>&1', $columns_output, $columns_status);
$matched = false;
if (!$columns_status && $matched = preg_match('/^\d+\s(\d+)$/', $columns_output[0], $matches, 0)) {
$columns = $matches[1];
}
// If stty fails and Drush is running on Windows are we trying with mode con.
if (($columns_status || !$matched) && static::isWindows()) {
$columns_output = [];
exec('mode con', $columns_output, $columns_status);
if (!$columns_status && is_array($columns_output)) {
$columns = (int)preg_replace('/\D/', '', $columns_output[4], -1, $columns_count);
}
// TODO: else { 'Drush could not detect the console window width. Set a Windows Environment Variable of COLUMNS to the desired width.'
}
// Failling back to default columns value
if (empty($columns)) {
$columns = 80;
}
// TODO: should we deal with reserve-margin here, or adjust it later?
return $columns;
}
/**
* Returns the filename for the file that stores the DRUPAL_SITE variable.
*
* @param string $filename_prefix
* An arbitrary string to prefix the filename with.
*
* @return string|false
* Returns the full path to temp file if possible, or FALSE if not.
*/
protected function getSiteSetAliasFilePath($filename_prefix = 'drush-drupal-site-')
{
$shell_pid = getenv('DRUSH_SHELL_PID');
if (!$shell_pid && function_exists('posix_getppid')) {
$shell_pid = posix_getppid();
}
if (!$shell_pid) {
return false;
}
// The env variables below must match the variables in example.prompt.sh
$tmp = getenv('TMPDIR') ? getenv('TMPDIR') : '/tmp';
$username = $this->getUsername();
return "{$tmp}/drush-env-{$username}/{$filename_prefix}" . $shell_pid;
}
}