Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / memcache / src / DrupalMemcacheFactory.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\memcache\DrupalMemcacheFactory.
6  */
7
8 namespace Drupal\memcache;
9
10 use Psr\Log\LogLevel;
11
12 /**
13  * Factory class for creation of Memcache objects.
14  */
15 class DrupalMemcacheFactory {
16
17   /**
18    * The settings object.
19    *
20    * @var \Drupal\memcache\DrupalMemcacheConfig
21    */
22   protected $settings;
23
24   /**
25    * @var string
26    */
27   protected $extension;
28
29   /**
30    * @var bool
31    */
32   protected $memcachePersistent;
33
34   /**
35    * @var \Drupal\memcache\DrupalMemcacheInterface[]
36    */
37   protected $memcacheCache = array();
38
39   /**
40    * @var array
41    */
42   protected $memcacheServers = array();
43
44   /**
45    * @var array
46    */
47   protected $memcacheBins = array();
48
49   /**
50    * @var array
51    */
52   protected $failedConnectionCache = array();
53
54   /**
55    * Constructs a DrupalMemcacheFactory object.
56    *
57    * @param \Drupal\memcache\DrupalMemcacheConfig $settings
58    */
59   public function __construct(DrupalMemcacheConfig $settings) {
60     $this->settings = $settings;
61
62     $this->initialize();
63   }
64
65   /**
66    * Returns a Memcache object based on settings and the bin requested.
67    *
68    * @param string $bin
69    *   The bin which is to be used.
70    *
71    * @param bool $flush
72    *   Rebuild the bin/server/cache mapping.
73    *
74    * @return \Drupal\memcache\DrupalMemcacheInterface
75    *   A Memcache object.
76    */
77   public function get($bin = NULL, $flush = FALSE) {
78     if ($flush) {
79       $this->flush();
80     }
81
82     if (empty($this->memcacheCache) || empty($this->memcacheCache[$bin])) {
83       // If there is no cluster for this bin in $memcache_bins, cluster is
84       // 'default'.
85       $cluster = empty($this->memcacheBins[$bin]) ? 'default' : $this->memcacheBins[$bin];
86
87       // If this bin isn't in our $memcacheBins configuration array, and the
88       // 'default' cluster is already initialized, map the bin to 'default'
89       // because we always map the 'default' bin to the 'default' cluster.
90       if (empty($this->memcacheBins[$bin]) && !empty($this->memcacheCache['default'])) {
91         $this->memcacheCache[$bin] = &$this->memcacheCache['default'];
92       }
93       else {
94         // Create a new Memcache object. Each cluster gets its own Memcache
95         // object.
96         // @todo Can't add a custom memcache class here yet.
97         if ($this->extension == 'Memcached') {
98           $memcache = new DrupalMemcached($this->settings);
99         }
100         elseif ($this->extension == 'Memcache') {
101           $memcache = new DrupalMemcache($this->settings);
102         }
103
104         // A variable to track whether we've connected to the first server.
105         $init = FALSE;
106
107         // Link all the servers to this cluster.
108         foreach ($this->memcacheServers as $s => $c) {
109           if ($c == $cluster && !isset($this->failedConnectionCache[$s])) {
110             if ($memcache->addServer($s, $this->memcachePersistent) && !$init) {
111               $init = TRUE;
112             }
113
114             if (!$init) {
115               $this->failedConnectionCache[$s] = FALSE;
116             }
117           }
118         }
119
120         if ($init) {
121           // Map the current bin with the new Memcache object.
122           $this->memcacheCache[$bin] = $memcache;
123
124           // Now that all the servers have been mapped to this cluster, look for
125           // other bins that belong to the cluster and map them too.
126           foreach ($this->memcacheBins as $b => $c) {
127             if (($c == $cluster) && ($b != $bin)) {
128               // Map this bin and cluster by reference.
129               $this->memcacheCache[$b] = &$this->memcacheCache[$bin];
130             }
131           }
132         }
133         else {
134           throw new MemcacheException('Memcache instance could not be initialized. Check memcache is running and reachable');
135         }
136       }
137     }
138
139     return empty($this->memcacheCache[$bin]) ? FALSE : $this->memcacheCache[$bin];
140   }
141
142   /**
143    * Initializes memcache settings.
144    */
145   protected function initialize() {
146     // If an extension is specified in settings.php, use that when available.
147     $preferred = $this->settings->get('extension', NULL);
148     if (isset($preferred) && class_exists($preferred)) {
149       $this->extension = $preferred;
150     }
151     // If no extension is set, default to Memcache. The Memcached extension has
152     // some features that the older extension lacks but also an unfixed bug that
153     // affects cache clears.
154     // @see http://pecl.php.net/bugs/bug.php?id=16829
155     elseif (class_exists('Memcache')) {
156       $this->extension = 'Memcache';
157     }
158     elseif (class_exists('Memcached')) {
159       $this->extension = 'Memcached';
160     }
161     else {
162       throw new MemcacheException('No Memcache extension found');
163     }
164
165     // Values from settings.php
166     $this->memcacheServers = $this->settings->get('servers', ['127.0.0.1:11211' => 'default']);
167     $this->memcacheBins = $this->settings->get('bins', ['default' => 'default']);
168
169     // Indicate whether to connect to memcache using a persistent connection.
170     // Note: this only affects the Memcache PECL extension, and does not affect
171     // the Memcached PECL extension.  For a detailed explanation see:
172     // http://drupal.org/node/822316#comment-4427676
173     $this->memcachePersistent = $this->settings->get('persistent', FALSE);
174   }
175
176   /**
177    * Flushes the memcache bin/server/cache mappings and closes connections.
178    */
179   protected function flush() {
180     foreach ($this->memcacheCache as $cluster) {
181       $cluster->close();
182     }
183
184     $this->memcacheCache = array();
185   }
186
187 }