Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Cache / CommandCache.php
1 <?php
2
3 namespace Drush\Cache;
4
5 use Consolidation\AnnotatedCommand\Cache\SimpleCacheInterface;
6
7 /**
8  * Command cache implementation.
9  *
10  * This wrapper implements a cache usable with the annotated-command
11  * library's command cache. It uses a Drush JSONCache for its back-end.
12  */
13 class CommandCache implements SimpleCacheInterface
14 {
15
16     protected $cacheBackend;
17
18     public function __construct(CacheInterface $cacheBackend)
19     {
20         $this->cacheBackend = $cacheBackend;
21     }
22
23     /**
24      * Test for an entry from the cache
25      * @param string $key
26      * @return boolean
27      */
28     public function has($key)
29     {
30         $cacheItem = $this->cacheBackend->get($key);
31         return $this->valid($cacheItem);
32     }
33     /**
34      * Get an entry from the cache
35      * @param string $key
36      * @return array
37      */
38     public function get($key)
39     {
40         $cacheItem = $this->cacheBackend->get($key);
41         if (!$this->valid($cacheItem)) {
42             return [];
43         }
44         // TODO: FileCache::get() should just return the
45         // data element, not the entire cacheItem. Then we
46         // could make it implement SimpleCacheInterface & do
47         // away with this adapter class.
48         return $cacheItem->data;
49     }
50     /**
51      * Store an entry in the cache
52      * @param string $key
53      * @param array $data
54      */
55     public function set($key, $data)
56     {
57         $this->cacheBackend->set($key, $data);
58     }
59
60     protected function valid($cacheItem)
61     {
62         return is_object($cacheItem) && isset($cacheItem->data);
63     }
64 }