-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackendCommandsTrait.php
179 lines (162 loc) · 5.12 KB
/
BackendCommandsTrait.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
<?php
namespace Drush\Commands\BurdaStyleGroup;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\SiteAlias\SiteAliasInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Drupal\Core\Site\Settings;
use Drush\Drush;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Path;
/**
* Trait for backend drush commands.
*/
trait BackendCommandsTrait
{
use SiteAliasManagerAwareTrait;
/**
* Maps site alias to site directory.
*
* @var string[]
*/
private $siteDomainDirectoryMapping = [
'@elle.dev' => 'elle.de',
'@esquire.dev' => 'esquire.de',
'@focusplus.dev' => 'focusplus.de',
'@freundin.dev' => 'freundin.de',
'@harpersbazaar.dev' => 'harpersbazaar.de',
'@instyle.dev' => 'instyle.de',
];
/**
* @var string
*/
private $projectDirectory;
/**
* Drush command wrapper.
*
* Runs the command and prints the output.
*
* @param \Consolidation\SiteAlias\SiteAliasInterface $siteAlias
* @param $command
* @param array $args
* @param array $options
* @param array $optionsDoubleDash
*/
protected function drush(SiteAliasInterface $siteAlias, $command, $args = [], $options = [], $optionsDoubleDash = [])
{
/**
* @var \Consolidation\SiteProcess\SiteProcess $process
*/
$process = $this->processManager()->drush($siteAlias, $command, $args, $options, $optionsDoubleDash);
$isTtySupported = (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes);
if ($isTtySupported) {
// This allows us to ask user for confirmation, if --yes option was not given.
$process->setTty(true);
}
$process->mustRun($process->showRealtime());
}
/**
* Process wrapper.
*
* Runs the process and prints the output.
*
* @param $command
* @param null $cwd
* @param array|null $env
* @param null $input
* @param int $timeout
*/
protected function process($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
{
$process = $this->processManager()->process($command, $cwd, $env, $input, $timeout);
$process->mustRun($process->showRealtime());
}
/**
* The base project directory, where most commands will be executed from.
* Defaults to composer root of project-
*
* @return string
*/
protected function projectDirectory(): string
{
return $this->projectDirectory;
}
/**
* The drupal root directory for a given site.
*
* @return string
*/
protected function drupalRootDirectory(): string
{
return $this->projectDirectory().'/docroot';
}
/**
* The site directory for a given site.
* @see SiteInstallCommands::getSitesSubdirFromUri().
*
* @return string
*/
protected function siteDirectory(): string
{
$uri = preg_replace('#^https?://#', '', $this->selfRecord()->get('uri'));
$sitesFile = $this->drupalRootDirectory().'/sites/sites.php';
if (file_exists($sitesFile)) {
include $sitesFile;
/** @var array $sites */
if (isset($sites) && array_key_exists($uri, $sites)) {
return Path::join($this->drupalRootDirectory(), 'sites', $sites[$uri]);
}
}
// Fall back to default directory if it exists.
if (file_exists(Path::join($this->drupalRootDirectory(), 'sites', 'default'))) {
return 'default';
}
return false;
}
/**
* The directory, where site specific configuration is placed.
*
* @return string
*/
protected function siteConfigSyncDirectory(): string
{
return $this->drupalRootDirectory().'/'.Settings::get('config_sync_directory', false);
}
/**
* The directory, where shared configuration is placed.
*
* @return string
*/
protected function configSharedDirectory(): string
{
return $this->siteConfigSyncDirectory().'/../../shared';
}
/**
* The directory, where site specific overridden configuration is placed.
*
* @return string
*/
protected function siteConfigOverrideDirectory(): string
{
return $this->siteConfigSyncDirectory().'/../override';
}
/**
* Get the '@self' alias record.
*
* @return \Consolidation\SiteAlias\SiteAlias|\Consolidation\SiteAlias\SiteAliasInterface
*/
protected function selfRecord(): SiteAliasInterface
{
return $this->siteAliasManager()->getSelf();
}
/**
* The supported site alias records.
*
* @return string[]
*/
protected function supportedAliases()
{
return array_keys($this->siteDomainDirectoryMapping);
}
}