Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. SelfUpdateCommand.php

function SelfUpdateCommand::tryAsWindowsAdmin

Invokes a UAC prompt to update composer.phar as an admin

Uses a .vbs script to elevate and run the cmd.exe copy command.

Parameters

string $localFilename The composer.phar location:

string $newFilename The downloaded or backup phar:

Return value

bool Whether composer.phar has been updated

1 call to SelfUpdateCommand::tryAsWindowsAdmin()
SelfUpdateCommand::setLocalPhar in vendor/composer/composer/src/Composer/Command/SelfUpdateCommand.php
Checks if the downloaded/rollback phar is valid then moves it

File

vendor/composer/composer/src/Composer/Command/SelfUpdateCommand.php, line 596

Class

SelfUpdateCommand
@author Igor Wiedler <igor@wiedler.ch> @author Kevin Ran <kran@adobe.com> @author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Command

Code

protected function tryAsWindowsAdmin(string $localFilename, string $newFilename) : bool {
    $io = $this->getIO();
    $io->writeError('<error>Unable to write "' . $localFilename . '". Access is denied.</error>');
    $helpMessage = 'Please run the self-update command as an Administrator.';
    $question = 'Complete this operation with Administrator privileges [<comment>Y,n</comment>]? ';
    if (!$io->askConfirmation($question, true)) {
        $io->writeError('<warning>Operation cancelled. ' . $helpMessage . '</warning>');
        return false;
    }
    $tmpFile = tempnam(sys_get_temp_dir(), '');
    if (false === $tmpFile) {
        $io->writeError('<error>Operation failed.' . $helpMessage . '</error>');
        return false;
    }
    $script = $tmpFile . '.vbs';
    rename($tmpFile, $script);
    $checksum = hash_file('sha256', $newFilename);
    // cmd's internal copy is fussy about backslashes
    $source = str_replace('/', '\\', $newFilename);
    $destination = str_replace('/', '\\', $localFilename);
    $vbs = <<<EOT
Set UAC = CreateObject("Shell.Application")
UAC.ShellExecute "cmd.exe", "/c copy /b /y ""{<span class="php-variable">$source</span>}"" ""{<span class="php-variable">$destination</span>}""", "", "runas", 0
Wscript.Sleep(300)
EOT;
    file_put_contents($script, $vbs);
    exec('"' . $script . '"');
    @unlink($script);
    // see if the file was copied and is still accessible
    if ($result = Filesystem::isReadable($localFilename) && hash_file('sha256', $localFilename) === $checksum) {
        $io->writeError('<info>Operation succeeded.</info>');
        @unlink($newFilename);
    }
    else {
        $io->writeError('<error>Operation failed.' . $helpMessage . '</error>');
    }
    return $result;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal