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

Breadcrumb

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

function Cache::write

Return value

bool

File

vendor/composer/composer/src/Composer/Cache.php, line 138

Class

Cache
Reads/writes to a filesystem cache

Namespace

Composer

Code

public function write(string $file, string $contents) {
    $wasEnabled = $this->enabled === true;
    if ($this->isEnabled() && !$this->readOnly) {
        $file = Preg::replace('{[^' . $this->allowlist . ']}i', '-', $file);
        $this->io
            ->writeError('Writing ' . $this->root . $file . ' into cache', true, IOInterface::DEBUG);
        $tempFileName = $this->root . $file . bin2hex(random_bytes(5)) . '.tmp';
        try {
            return file_put_contents($tempFileName, $contents) !== false && rename($tempFileName, $this->root . $file);
        } catch (\ErrorException $e) {
            // If the write failed despite isEnabled checks passing earlier, rerun the isEnabled checks to
            // see if they are still current and recreate the cache dir if needed. Refs https://github.com/composer/composer/issues/11076
            if ($wasEnabled) {
                clearstatcache();
                $this->enabled = null;
                return $this->write($file, $contents);
            }
            $this->io
                ->writeError('<warning>Failed to write into cache: ' . $e->getMessage() . '</warning>', true, IOInterface::DEBUG);
            if (Preg::isMatch('{^file_put_contents\\(\\): Only ([0-9]+) of ([0-9]+) bytes written}', $e->getMessage(), $m)) {
                // Remove partial file.
                unlink($tempFileName);
                $message = sprintf('<warning>Writing %1$s into cache failed after %2$u of %3$u bytes written, only %4$s bytes of free space available</warning>', $tempFileName, $m[1], $m[2], function_exists('disk_free_space') ? @disk_free_space(dirname($tempFileName)) : 'unknown');
                $this->io
                    ->writeError($message);
                return false;
            }
            throw $e;
        }
    }
    return false;
}

API Navigation

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