Projet

Général

Profil

Paste
Télécharger (3,37 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / includes / FeedsHTTPCacheItem.inc @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains FeedsHTTPCacheItem class.
6
 */
7

    
8
/**
9
 * Class of a cached item.
10
 */
11
class FeedsHTTPCacheItem {
12

    
13
  /**
14
   * The cache bin to save the data to.
15
   *
16
   * @var string
17
   */
18
  const CACHE_BIN = 'cache_feeds_http';
19

    
20
  /**
21
   * The cache ID.
22
   *
23
   * @var string
24
   */
25
  protected $cid;
26

    
27
  /**
28
   * Headers of the response.
29
   *
30
   * @var array
31
   */
32
  protected $headers = array();
33

    
34
  /**
35
   * Path to the cache file.
36
   *
37
   * @var string
38
   */
39
  protected $file_path;
40

    
41
  /**
42
   * FeedsHTTPCacheItem object constructor.
43
   *
44
   * @param string $cid
45
   *   The cache ID.
46
   * @param object $response
47
   *   The HTTP response object.
48
   */
49
  public function __construct($cid, $response) {
50
    $this->setCid($cid);
51

    
52
    // Copy over other metadata from result, but not the raw data.
53
    // Value is assigned by reference to save memory.
54
    foreach ($response as $key => &$value) {
55
      switch ($key) {
56
        case 'headers':
57
          $this->headers = (array) $value;
58
          break;
59

    
60
        case 'file_path':
61
          $this->setFilePath($value);
62
          break;
63

    
64
        case 'data':
65
          // Data should not be cached in the database, so save data to a file
66
          // instead. The whole response object is passed to save memory usage.
67
          // The data could potentially be huge.
68
          $this->saveResponseData($response);
69
          break;
70

    
71
        default:
72
          $this->$key = $response->$key;
73
      }
74
    }
75
  }
76

    
77
  /**
78
   * Sets cache ID.
79
   *
80
   * @param string $cid
81
   *   The cache ID.
82
   */
83
  public function setCid($cid) {
84
    $this->cid = $cid;
85
  }
86

    
87
  /**
88
   * Sets file path.
89
   *
90
   * @param string $file_path
91
   *   The file path to set.
92
   */
93
  public function setFilePath($file_path) {
94
    $this->file_path = $file_path;
95
  }
96

    
97
  /**
98
   * Magic isset.
99
   */
100
  public function __isset($member) {
101
    if ($member == 'data') {
102
      // Data should always be cached to a file, so in that case we check if the
103
      // file exist where the data should be in.
104
      return (isset($this->file_path) && file_exists($this->file_path));
105
    }
106

    
107
    return isset($this->$member);
108
  }
109

    
110
  /**
111
   * Magic getter.
112
   */
113
  public function __get($member) {
114
    if ($member == 'data') {
115
      // Data is cached in a file, so when that member is requested, return the
116
      // file contents.
117
      return $this->getFileContents();
118
    }
119

    
120
    return $this->$member;
121
  }
122

    
123
  /**
124
   * Returns cache object.
125
   *
126
   * @return FeedsHTTPCache
127
   *   An instance of FeedsHTTPCache.
128
   */
129
  public function getCacheObject() {
130
    return FeedsHTTPCache::getInstance(self::CACHE_BIN);
131
  }
132

    
133
  /**
134
   * Gets the cached file from the file system.
135
   *
136
   * @return string|null
137
   *   The file's contents, if the file exists.
138
   *   NULL otherwise.
139
   */
140
  public function getFileContents() {
141
    if (file_exists($this->file_path)) {
142
      return file_get_contents($this->file_path);
143
    }
144
  }
145

    
146
  /**
147
   * Writes data to a file.
148
   *
149
   * @param object $response
150
   *   The HTTP Response object that contains the data.
151
   */
152
  public function saveResponseData($response) {
153
    if (isset($response->data)) {
154
      // Get cache object, this object knows where to save the file.
155
      $filename = $this->getCacheObject()->saveFile($this->cid, $response);
156
      $this->setFilePath($filename);
157
    }
158
  }
159

    
160
  /**
161
   * Saves item to cache.
162
   */
163
  public function cacheSet() {
164
    cache_set($this->cid, $this, self::CACHE_BIN);
165
  }
166

    
167
}