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

Breadcrumb

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

function SelfUpdateCommand::setLocalPhar

Checks if the downloaded/rollback phar is valid then moves it

Parameters

string $localFilename The composer.phar location:

string $newFilename The downloaded or backup phar:

string $backupTarget The filename to use for the backup:

Return value

bool Whether the phar is valid and has been moved

Throws

FilesystemException If the file cannot be moved

2 calls to SelfUpdateCommand::setLocalPhar()
SelfUpdateCommand::execute in vendor/composer/composer/src/Composer/Command/SelfUpdateCommand.php
SelfUpdateCommand::rollback in vendor/composer/composer/src/Composer/Command/SelfUpdateCommand.php

File

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

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 setLocalPhar(string $localFilename, string $newFilename, ?string $backupTarget = null) : bool {
    $io = $this->getIO();
    $perms = @fileperms($localFilename);
    if ($perms !== false) {
        @chmod($newFilename, $perms);
    }
    // check phar validity
    if (!$this->validatePhar($newFilename, $error)) {
        $io->writeError('<error>The ' . ($backupTarget !== null ? 'update' : 'backup') . ' file is corrupted (' . $error . ')</error>');
        if ($backupTarget !== null) {
            $io->writeError('<error>Please re-run the self-update command to try again.</error>');
        }
        return false;
    }
    // copy current file into backups dir
    if ($backupTarget !== null) {
        @copy($localFilename, $backupTarget);
    }
    try {
        if (Platform::isWindows()) {
            // use copy to apply permissions from the destination directory
            // as rename uses source permissions and may block other users
            copy($newFilename, $localFilename);
            @unlink($newFilename);
        }
        else {
            rename($newFilename, $localFilename);
        }
        return true;
    } catch (\Exception $e) {
        // see if we can run this operation as an Admin on Windows
        if (!is_writable(dirname($localFilename)) && $io->isInteractive() && $this->isWindowsNonAdminUser()) {
            return $this->tryAsWindowsAdmin($localFilename, $newFilename);
        }
        @unlink($newFilename);
        $action = 'Composer ' . ($backupTarget !== null ? 'update' : 'rollback');
        throw new FilesystemException($action . ' failed: "' . $localFilename . '" could not be written.' . PHP_EOL . $e->getMessage());
    }
}
RSS feed
Powered by Drupal