Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions src/Symfony/Component/Cache/Adapter/PhpArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,12 @@ public function warmUp(array $values)
*/
public function getItem($key)
{
if (null === $this->values) {
$this->initialize();
}

if (!is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
}

if (null === $this->values) {
$this->initialize();
}
if (!isset($this->values[$key])) {
return $this->fallbackPool->getItem($key);
}
Expand Down Expand Up @@ -203,15 +201,14 @@ public function getItem($key)
*/
public function getItems(array $keys = array())
{
if (null === $this->values) {
$this->initialize();
}

foreach ($keys as $key) {
if (!is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
}
}
if (null === $this->values) {
$this->initialize();
}

return $this->generateItems($keys);
}
Expand All @@ -221,13 +218,12 @@ public function getItems(array $keys = array())
*/
public function hasItem($key)
{
if (null === $this->values) {
$this->initialize();
}

if (!is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
}
if (null === $this->values) {
$this->initialize();
}

return isset($this->values[$key]) || $this->fallbackPool->hasItem($key);
}
Expand All @@ -249,13 +245,12 @@ public function clear()
*/
public function deleteItem($key)
{
if (null === $this->values) {
$this->initialize();
}

if (!is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key)));
}
if (null === $this->values) {
$this->initialize();
}

return !isset($this->values[$key]) && $this->fallbackPool->deleteItem($key);
}
Expand All @@ -265,10 +260,6 @@ public function deleteItem($key)
*/
public function deleteItems(array $keys)
{
if (null === $this->values) {
$this->initialize();
}

$deleted = true;
$fallbackKeys = array();

Expand All @@ -283,6 +274,9 @@ public function deleteItems(array $keys)
$fallbackKeys[] = $key;
}
}
if (null === $this->values) {
$this->initialize();
}

if ($fallbackKeys) {
$deleted = $this->fallbackPool->deleteItems($fallbackKeys) && $deleted;
Expand Down Expand Up @@ -328,7 +322,7 @@ public function commit()
*/
private function initialize()
{
$this->values = @(include $this->file) ?: array();
$this->values = file_exists($this->file) ? (include $this->file ?: array()) : array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be is_file instead? file_exists() returns true for directories.

Copy link
Member Author

@nicolas-grekas nicolas-grekas Jan 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_file is slower, and this is a mandatory path. there is no reason this is not a file but in severe mistakes, where the php warning will be enough a report

}

/**
Expand Down