CacheFactory.php 1.65 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Cache\CacheFactory.
6 7 8 9 10 11 12
 */

namespace Drupal\Core\Cache;

/**
 * Defines the cache backend factory.
 */
13 14 15 16
use Drupal\Component\Utility\Settings;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\DependencyInjection\ContainerBuilder;

17
class CacheFactory extends ContainerAware implements CacheFactoryInterface {
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

  /**
   * The settings array.
   *
   * @var \Drupal\Component\Utility\Settings
   */
  protected $settings;

  /**
   * Constructs CacheFactory object.
   *
   * @param \Drupal\Component\Utility\Settings $settings
   *   The settings array.
   */
  function __construct(Settings $settings) {
    $this->settings = $settings;
  }
35 36 37 38 39 40 41 42 43 44 45 46 47

  /**
   * Instantiates a cache backend class for a given cache bin.
   *
   * By default, this returns an instance of the
   * Drupal\Core\Cache\DatabaseBackend class.
   *
   * Classes implementing Drupal\Core\Cache\CacheBackendInterface can register
   * themselves both as a default implementation and for specific bins.
   *
   * @param string $bin
   *   The cache bin for which a cache backend object should be returned.
   *
48
   * @return \Drupal\Core\Cache\CacheBackendInterface
49 50
   *   The cache backend object associated with the specified bin.
   */
51 52 53 54 55 56 57 58 59 60 61 62
  public function get($bin) {
    $cache_settings = $this->settings->get('cache');
    if (isset($cache_settings[$bin])) {
      $service_name = $cache_settings[$bin];
    }
    elseif (isset($cache_settings['default'])) {
      $service_name = $cache_settings['default'];
    }
    else {
      $service_name = 'cache.backend.database';
    }
    return $this->container->get($service_name)->get($bin);
63 64 65
  }

}