-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9889 from dkarlovi/feat/cache.gz
feat: cache.gz
- Loading branch information
Showing
8 changed files
with
219 additions
and
859 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Psalm\Internal; | ||
|
||
use Psalm\Config; | ||
use Psalm\Internal\Provider\Providers; | ||
|
||
use function file_exists; | ||
use function file_put_contents; | ||
use function gzdeflate; | ||
use function gzinflate; | ||
use function igbinary_serialize; | ||
use function igbinary_unserialize; | ||
use function serialize; | ||
use function unlink; | ||
use function unserialize; | ||
|
||
use const LOCK_EX; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class Cache | ||
{ | ||
private Config $config; | ||
|
||
public bool $use_igbinary; | ||
|
||
public function __construct(Config $config) | ||
{ | ||
$this->config = $config; | ||
$this->use_igbinary = $config->use_igbinary; | ||
} | ||
|
||
/** | ||
* @return array|object|string|null | ||
*/ | ||
public function getItem(string $path) | ||
{ | ||
if (!file_exists($path)) { | ||
return null; | ||
} | ||
|
||
$cache = Providers::safeFileGetContents($path); | ||
if ($this->config->use_gzip) { | ||
$inflated = @gzinflate($cache); | ||
if ($inflated !== false) { | ||
$cache = $inflated; | ||
} | ||
} | ||
|
||
if ($this->config->use_igbinary) { | ||
/** @var object|false $unserialized */ | ||
$unserialized = igbinary_unserialize($cache); | ||
} else { | ||
/** @var object|false $unserialized */ | ||
$unserialized = @unserialize($cache); | ||
} | ||
|
||
return $unserialized !== false ? $unserialized : null; | ||
} | ||
|
||
public function deleteItem(string $path): void | ||
{ | ||
if (file_exists($path)) { | ||
unlink($path); | ||
} | ||
} | ||
|
||
/** | ||
* @param array|object|string $item | ||
*/ | ||
public function saveItem(string $path, $item): void | ||
{ | ||
if ($this->config->use_igbinary) { | ||
$serialized = igbinary_serialize($item); | ||
} else { | ||
$serialized = serialize($item); | ||
} | ||
|
||
if ($this->config->use_gzip) { | ||
$serialized = gzdeflate($serialized); | ||
} | ||
|
||
file_put_contents($path, $serialized, LOCK_EX); | ||
} | ||
|
||
public function getCacheDirectory(): ?string | ||
{ | ||
return $this->config->getCacheDirectory(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.