This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
platformsh_generate_drush_yml.php
80 lines (62 loc) · 1.79 KB
/
platformsh_generate_drush_yml.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
<?php
/**
* @file
* A script that creates the .drush/drush.yml file.
*/
// This file should only be executed as a PHP-CLI script.
if (PHP_SAPI !== 'cli') {
exit;
}
require_once(__DIR__ . '/../vendor/autoload.php');
/**
* Returns a site URL to use with Drush, if possible.
*
* @return string|NULL
*/
function _platformsh_drush_site_url() {
$platformsh = new \Platformsh\ConfigReader\Config();
if (!$platformsh->inRuntime()) {
return;
}
$routes = $platformsh->getUpstreamRoutes($platformsh->applicationName);
// Sort URLs, with the primary route first, then by HTTPS before HTTP, then by length.
usort($routes, function (array $a, array $b) {
// false sorts before true, normally, so negate the comparison.
return
[!$a['primary'], strpos($a['url'], 'https://') !== 0, strlen($a['url'])]
<=>
[!$b['primary'], strpos($b['url'], 'https://') !== 0, strlen($b['url'])];
});
// Return the url of the first one.
return reset($routes)['url'] ?: NULL;
}
$appRoot = dirname(__DIR__);
$filename = $appRoot . '/.drush/drush.yml';
$siteUrl = _platformsh_drush_site_url();
if (empty($siteUrl)) {
echo "Failed to find a site URL\n";
if (file_exists($filename)) {
echo "The file exists but may be invalid: $filename\n";
}
exit(1);
}
$siteUrlYamlEscaped = json_encode($siteUrl, JSON_UNESCAPED_SLASHES);
$scriptPath = __FILE__;
$success = file_put_contents($filename, <<<EOF
# Drush configuration file.
# This was automatically generated by the script:
# $scriptPath
options:
# Set the default site URL.
uri: $siteUrlYamlEscaped
EOF
);
if (!$success) {
echo "Failed to write file: $filename\n";
exit(1);
}
if (!chmod($filename, 0600)) {
echo "Failed to modify file permissions: $filename\n";
exit(1);
}
echo "Created Drush configuration file: $filename\n";