diff --git a/src/Composer/Cache.php b/src/Composer/Cache.php
index c02a636a2..8ee724357 100644
--- a/src/Composer/Cache.php
+++ b/src/Composer/Cache.php
@@ -30,8 +30,8 @@ class Cache
private $io;
/** @var string */
private $root;
- /** @var bool */
- private $enabled = true;
+ /** @var ?bool */
+ private $enabled = null;
/** @var string */
private $allowlist;
/** @var Filesystem */
@@ -56,16 +56,6 @@ class Cache
if (!self::isUsable($cacheDir)) {
$this->enabled = false;
-
- return;
- }
-
- if (
- (!is_dir($this->root) && !Silencer::call('mkdir', $this->root, 0777, true))
- || !is_writable($this->root)
- ) {
- $this->io->writeError('Cannot create cache directory ' . $this->root . ', or directory is not writable. Proceeding without cache');
- $this->enabled = false;
}
}
@@ -92,6 +82,18 @@ class Cache
public function isEnabled()
{
+ if ($this->enabled === null) {
+ $this->enabled = true;
+
+ if (
+ (!is_dir($this->root) && !Silencer::call('mkdir', $this->root, 0777, true))
+ || !is_writable($this->root)
+ ) {
+ $this->io->writeError('Cannot create cache directory ' . $this->root . ', or directory is not writable. Proceeding without cache');
+ $this->enabled = false;
+ }
+ }
+
return $this->enabled;
}
@@ -102,7 +104,7 @@ class Cache
public function read($file)
{
- if ($this->enabled) {
+ if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) {
$this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG);
@@ -116,7 +118,7 @@ class Cache
public function write($file, $contents)
{
- if ($this->enabled && !$this->readOnly) {
+ if ($this->isEnabled() && !$this->readOnly) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
$this->io->writeError('Writing '.$this->root . $file.' into cache', true, IOInterface::DEBUG);
@@ -155,7 +157,7 @@ class Cache
*/
public function copyFrom($file, $source)
{
- if ($this->enabled && !$this->readOnly) {
+ if ($this->isEnabled() && !$this->readOnly) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
$this->filesystem->ensureDirectoryExists(dirname($this->root . $file));
@@ -176,7 +178,7 @@ class Cache
*/
public function copyTo($file, $target)
{
- if ($this->enabled) {
+ if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) {
try {
@@ -216,7 +218,7 @@ class Cache
public function remove($file)
{
- if ($this->enabled) {
+ if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) {
return $this->filesystem->unlink($this->root . $file);
@@ -228,7 +230,7 @@ class Cache
public function clear()
{
- if ($this->enabled) {
+ if ($this->isEnabled()) {
$this->filesystem->emptyDirectory($this->root);
return true;
@@ -239,7 +241,7 @@ class Cache
public function gc($ttl, $maxSize)
{
- if ($this->enabled) {
+ if ($this->isEnabled()) {
$expire = new \DateTime();
$expire->modify('-'.$ttl.' seconds');
@@ -269,7 +271,7 @@ class Cache
public function sha1($file)
{
- if ($this->enabled) {
+ if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) {
return sha1_file($this->root . $file);
@@ -281,7 +283,7 @@ class Cache
public function sha256($file)
{
- if ($this->enabled) {
+ if ($this->isEnabled()) {
$file = preg_replace('{[^'.$this->allowlist.']}i', '-', $file);
if (file_exists($this->root . $file)) {
return hash_file('sha256', $this->root . $file);