Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit e4fcb41

Browse files
committed
Improving detection of DVM state.
1 parent d4feab1 commit e4fcb41

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

src/Robo/Inspector/Inspector.php

+50-12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class Inspector implements BuilderAwareInterface, ConfigAwareInterface, LoggerAw
4343
*/
4444
protected $isMySqlAvailable = NULL;
4545

46+
/**
47+
* @var array
48+
*/
49+
protected $drupalVmStatus = [];
50+
4651
/**
4752
* The constructor.
4853
*
@@ -56,6 +61,7 @@ public function __construct(Executor $executor) {
5661
public function clearState() {
5762
$this->isDrupalInstalled = NULL;
5863
$this->isMySqlAvailable = NULL;
64+
$this->drupalVmStatus = [];
5965
}
6066

6167
/**
@@ -234,11 +240,10 @@ public function isDrupalVmConfigPresent() {
234240
* TRUE if Drupal VM is initialized for the local machine.
235241
*/
236242
public function isDrupalVmLocallyInitialized() {
237-
// We assume that if the local drush alias is ${project.machine_name.local},
238-
// rather than self, then Drupal VM is being used locally.
239-
$drush_local_alias = $this->getConfigValue('drush.aliases.local');
240-
$expected_vm_alias = $this->getConfigValue('project.machine_name') . '.local';
241-
$initialized = ($drush_local_alias == $expected_vm_alias) && file_exists($this->getConfigValue('repo.root') . '/box/config.yml');
243+
$status = $this->getDrupalVmStatus();
244+
$machine_name = $this->getConfigValue('project.machine_name');
245+
$initialized = !empty($status[$machine_name])
246+
&& file_exists($this->getConfigValue('repo.root') . '/box/config.yml');
242247
$statement = $initialized ? "is" : "is not";
243248
$this->logger->debug("Drupal VM $statement initialized.");
244249

@@ -256,14 +261,11 @@ public function isDrupalVmBooted() {
256261
return FALSE;
257262
}
258263

259-
$result = $this->executor->execute("vagrant status")
260-
->printOutput(FALSE)
261-
->printMetadata(FALSE)
262-
->interactive(FALSE)
263-
->run();
264-
$output = $result->getMessage();
264+
$status = $this->getDrupalVmStatus();
265+
$machine_name = $this->getConfigValue('project.machine_name');
266+
$booted = !empty($status[$machine_name]['state'])
267+
&& $status[$machine_name]['state'] == 'running';
265268

266-
$booted = strstr($output, "running");
267269
$statement = $booted ? "is" : "is not";
268270
$this->logger->debug("Drupal VM $statement booted.");
269271

@@ -504,4 +506,40 @@ public function isSimpleSamlPhpInstalled() {
504506
return $this->getConfig()->has('simplesamlphp') && $this->getConfigValue('simplesamlphp');
505507
}
506508

509+
/**
510+
* Gets the value of $this->drupalVmStatus. Sets it if empty.
511+
*
512+
* @return array
513+
* An array of status data.
514+
*/
515+
protected function getDrupalVmStatus() {
516+
if (empty($this->drupalVmStatus)) {
517+
$this->setDrupalVmStatus();
518+
}
519+
return $this->drupalVmStatus;
520+
}
521+
522+
/**
523+
* Sets $this->drupalVmStatus by executing `vagrant status`.
524+
*/
525+
protected function setDrupalVmStatus() {
526+
$result = $this->executor->execute("vagrant status --machine-readable")
527+
->printOutput(FALSE)
528+
->printMetadata(FALSE)
529+
->interactive(FALSE)
530+
->run();
531+
$output = $result->getOutputData();
532+
if (!$result->wasSuccessful() || !$output) {
533+
return FALSE;
534+
}
535+
$lines = explode("\n", $output);
536+
foreach ($lines as $line) {
537+
if (count($line) < 4) {
538+
continue;
539+
}
540+
list($timestamp, $target, $type, $data) = explode(',', $line);
541+
$this->drupalVmStatus[$target][$type] = $data;
542+
}
543+
}
544+
507545
}

0 commit comments

Comments
 (0)