Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Cache / ChainedFastBackendFactory.php
1 <?php
2
3 namespace Drupal\Core\Cache;
4
5 use Drupal\Core\Site\Settings;
6 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7
8 /**
9  * Defines the chained fast cache backend factory.
10  *
11  * @see \Drupal\Core\Cache\ChainedFastBackend
12  */
13 class ChainedFastBackendFactory implements CacheFactoryInterface {
14
15   use ContainerAwareTrait;
16
17   /**
18    * The service name of the consistent backend factory.
19    *
20    * @var string
21    */
22   protected $consistentServiceName;
23
24   /**
25    * The service name of the fast backend factory.
26    *
27    * @var string
28    */
29   protected $fastServiceName;
30
31   /**
32    * Constructs ChainedFastBackendFactory object.
33    *
34    * @param \Drupal\Core\Site\Settings|null $settings
35    *   (optional) The settings object.
36    * @param string|null $consistent_service_name
37    *   (optional) The service name of the consistent backend factory. Defaults
38    *   to:
39    *   - $settings->get('cache')['default'] (if specified)
40    *   - 'cache.backend.database' (if the above isn't specified)
41    * @param string|null $fast_service_name
42    *   (optional) The service name of the fast backend factory. Defaults to:
43    *   - 'cache.backend.apcu' (if the PHP process has APCu enabled)
44    *   - NULL (if the PHP process doesn't have APCu enabled)
45    */
46   public function __construct(Settings $settings = NULL, $consistent_service_name = NULL, $fast_service_name = NULL) {
47     // Default the consistent backend to the site's default backend.
48     if (!isset($consistent_service_name)) {
49       $cache_settings = isset($settings) ? $settings->get('cache') : [];
50       $consistent_service_name = isset($cache_settings['default']) ? $cache_settings['default'] : 'cache.backend.database';
51     }
52
53     // Default the fast backend to APCu if it's available.
54     if (!isset($fast_service_name) && function_exists('apcu_fetch')) {
55       $fast_service_name = 'cache.backend.apcu';
56     }
57
58     $this->consistentServiceName = $consistent_service_name;
59
60     // Do not use the fast chained backend during installation. In those cases,
61     // we expect many cache invalidations and writes, the fast chained cache
62     // backend performs badly in such a scenario.
63     if (!drupal_installation_attempted()) {
64       $this->fastServiceName = $fast_service_name;
65     }
66   }
67
68   /**
69    * Instantiates a chained, fast cache backend class for a given cache bin.
70    *
71    * @param string $bin
72    *   The cache bin for which a cache backend object should be returned.
73    *
74    * @return \Drupal\Core\Cache\CacheBackendInterface
75    *   The cache backend object associated with the specified bin.
76    */
77   public function get($bin) {
78     // Use the chained backend only if there is a fast backend available;
79     // otherwise, just return the consistent backend directly.
80     if (isset($this->fastServiceName)) {
81       return new ChainedFastBackend(
82         $this->container->get($this->consistentServiceName)->get($bin),
83         $this->container->get($this->fastServiceName)->get($bin),
84         $bin
85       );
86     }
87     else {
88       return $this->container->get($this->consistentServiceName)->get($bin);
89     }
90   }
91
92 }