Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
/*
3
 * Copyright 2016 Google Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
18
namespace Google\Auth\Cache;
19
 
20
use Psr\Cache\CacheItemInterface;
21
use Psr\Cache\CacheItemPoolInterface;
22
 
23
/**
24
 * Simple in-memory cache implementation.
25
 */
26
final class MemoryCacheItemPool implements CacheItemPoolInterface
27
{
28
    /**
29
     * @var CacheItemInterface[]
30
     */
31
    private $items;
32
 
33
    /**
34
     * @var CacheItemInterface[]
35
     */
36
    private $deferredItems;
37
 
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getItem($key)
42
    {
43
        return current($this->getItems([$key]));
44
    }
45
 
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getItems(array $keys = [])
50
    {
51
        $items = [];
52
 
53
        foreach ($keys as $key) {
54
            $this->isValidKey($key);
55
            $items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new Item($key);
56
        }
57
 
58
        return $items;
59
    }
60
 
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function hasItem($key)
65
    {
66
        $this->isValidKey($key);
67
 
68
        return isset($this->items[$key]) && $this->items[$key]->isHit();
69
    }
70
 
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function clear()
75
    {
76
        $this->items = [];
77
        $this->deferred = [];
78
 
79
        return true;
80
    }
81
 
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function deleteItem($key)
86
    {
87
        return $this->deleteItems([$key]);
88
    }
89
 
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function deleteItems(array $keys)
94
    {
95
        array_walk($keys, [$this, 'isValidKey']);
96
 
97
        foreach ($keys as $key) {
98
            unset($this->items[$key]);
99
        }
100
 
101
        return true;
102
    }
103
 
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function save(CacheItemInterface $item)
108
    {
109
        $this->items[$item->getKey()] = $item;
110
 
111
        return true;
112
    }
113
 
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function saveDeferred(CacheItemInterface $item)
118
    {
119
        $this->deferredItems[$item->getKey()] = $item;
120
 
121
        return true;
122
    }
123
 
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function commit()
128
    {
129
        foreach ($this->deferredItems as $item) {
130
            $this->save($item);
131
        }
132
 
133
        $this->deferredItems = [];
134
 
135
        return true;
136
    }
137
 
138
    /**
139
     * Determines if the provided key is valid.
140
     *
141
     * @param string $key
142
     * @return bool
143
     * @throws InvalidArgumentException
144
     */
145
    private function isValidKey($key)
146
    {
147
        $invalidCharacters = '{}()/\\\\@:';
148
 
149
        if (!is_string($key) || preg_match("#[$invalidCharacters]#", $key)) {
150
            throw new InvalidArgumentException('The provided key is not valid: ' . var_export($key, true));
151
        }
152
 
153
        return true;
154
    }
155
}