TypeLinkManager.php 2.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\rest\LinkManager\TypeLinkManager.
 */

namespace Drupal\rest\LinkManager;

10
use Drupal\Core\Cache\Cache;
11
12
use Drupal\Core\Cache\CacheBackendInterface;

13
14
class TypeLinkManager implements TypeLinkManagerInterface {

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /**
   * Injected cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface;
   */
  protected $cache;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The injected cache backend for caching type URIs.
   */
  public function __construct(CacheBackendInterface $cache) {
    $this->cache = $cache;
  }

32
33
34
35
36
37
38
39
40
41
42
43
44
  /**
   * Get a type link for a bundle.
   *
   * @param string $entity_type
   *   The bundle's entity type.
   * @param string $bundle
   *   The name of the bundle.
   *
   * @return array
   *   The URI that identifies this bundle.
   */
  public function getTypeUri($entity_type, $bundle) {
    // @todo Make the base path configurable.
45
    return _url("rest/type/$entity_type/$bundle", array('absolute' => TRUE));
46
47
  }

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  /**
   * Implements \Drupal\rest\LinkManager\TypeLinkManagerInterface::getTypeInternalIds().
   */
  public function getTypeInternalIds($type_uri) {
    $types = $this->getTypes();
    if (isset($types[$type_uri])) {
      return $types[$type_uri];
    }
    return FALSE;
  }

  /**
   * Get the array of type links.
   *
   * @return array
   *   An array of typed data ids (entity_type and bundle) keyed by
   *   corresponding type URI.
   */
66
  protected function getTypes() {
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    $cid = 'rest:links:types';
    $cache = $this->cache->get($cid);
    if (!$cache) {
      $this->writeCache();
      $cache = $this->cache->get($cid);
    }
    return $cache->data;
  }

  /**
   * Writes the cache of type links.
   */
  protected function writeCache() {
    $data = array();

    // Type URIs correspond to bundles. Iterate through the bundles to get the
    // URI and data for them.
84
85
    $entity_types = \Drupal::entityManager()->getDefinitions();
    foreach (entity_get_bundles() as $entity_type_id => $bundles) {
86
87
      // Only content entities are supported currently.
      // @todo Consider supporting config entities.
88
      if ($entity_types[$entity_type_id]->isSubclassOf('\Drupal\Core\Config\Entity\ConfigEntityInterface')) {
89
90
91
92
        continue;
      }
      foreach ($bundles as $bundle => $bundle_info) {
        // Get a type URI for the bundle.
93
        $bundle_uri = $this->getTypeUri($entity_type_id, $bundle);
94
        $data[$bundle_uri] = array(
95
          'entity_type' => $entity_type_id,
96
97
98
99
100
101
          'bundle' => $bundle,
        );
      }
    }
    // These URIs only change when entity info changes, so cache it permanently
    // and only clear it when entity_info is cleared.
102
    $this->cache->set('rest:links:types', $data, Cache::PERMANENT, array('entity_types'));
103
  }
104
}