c3a948ff86eb85d89c0e37626ece38b98cb7df11
[yaffs-website] / web / core / modules / hal / src / LinkManager / TypeLinkManager.php
1 <?php
2
3 namespace Drupal\hal\LinkManager;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Symfony\Component\HttpFoundation\RequestStack;
11
12 class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterface {
13
14   /**
15    * Injected cache backend.
16    *
17    * @var \Drupal\Core\Cache\CacheBackendInterface;
18    */
19   protected $cache;
20
21   /**
22    * Module handler service.
23    *
24    * @var \Drupal\Core\Extension\ModuleHandlerInterface
25    */
26   protected $moduleHandler;
27
28   /**
29    * The bundle info service.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
32    */
33   protected $bundleInfoService;
34
35   /**
36    * Constructor.
37    *
38    * @param \Drupal\Core\Cache\CacheBackendInterface $cache
39    *   The injected cache backend for caching type URIs.
40    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
41    *   The module handler service.
42    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
43    *   The config factory service.
44    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
45    *   The request stack.
46    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info_service
47    *   The bundle info service.
48    */
49   public function __construct(CacheBackendInterface $cache, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, RequestStack $request_stack, EntityTypeBundleInfoInterface $bundle_info_service) {
50     $this->cache = $cache;
51     $this->configFactory = $config_factory;
52     $this->moduleHandler = $module_handler;
53     $this->requestStack = $request_stack;
54     $this->bundleInfoService = $bundle_info_service;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function getTypeUri($entity_type, $bundle, $context = []) {
61     // Per the interface documentation of this method, the returned URI may
62     // optionally also serve as the URL of a documentation page about this
63     // bundle. However, Drupal does not currently implement such a documentation
64     // page. Therefore, we return a URI assembled relative to the site's base
65     // URL, which is sufficient to uniquely identify the site's entity type and
66     // bundle for use in hypermedia formats, but we do not take into account
67     // unclean URLs, language prefixing, or anything else that would be required
68     // for Drupal to be able to respond with content at this URL. If a module is
69     // installed that adds such content, but requires this URL to be different
70     // (e.g., include a language prefix), then the module must also override the
71     // TypeLinkManager class/service to return the desired URL.
72     $uri = $this->getLinkDomain() . "/rest/type/$entity_type/$bundle";
73     $this->moduleHandler->alter('hal_type_uri', $uri, $context);
74     // @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This
75     // hook is invoked to maintain backwards compatibility
76     $this->moduleHandler->alter('rest_type_uri', $uri, $context);
77     return $uri;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function getTypeInternalIds($type_uri, $context = []) {
84     $types = $this->getTypes($context);
85     if (isset($types[$type_uri])) {
86       return $types[$type_uri];
87     }
88     return FALSE;
89   }
90
91   /**
92    * Get the array of type links.
93    *
94    * @param array $context
95    *   Context from the normalizer/serializer operation.
96    *
97    * @return array
98    *   An array of typed data ids (entity_type and bundle) keyed by
99    *   corresponding type URI.
100    */
101   protected function getTypes($context = []) {
102     $cid = 'hal:links:types';
103     $cache = $this->cache->get($cid);
104     if (!$cache) {
105       $data = $this->writeCache($context);
106     }
107     else {
108       $data = $cache->data;
109     }
110     return $data;
111   }
112
113   /**
114    * Writes the cache of type links.
115    *
116    * @param array $context
117    *   Context from the normalizer/serializer operation.
118    *
119    * @return array
120    *   An array of typed data ids (entity_type and bundle) keyed by
121    *   corresponding type URI.
122    */
123   protected function writeCache($context = []) {
124     $data = [];
125
126     // Type URIs correspond to bundles. Iterate through the bundles to get the
127     // URI and data for them.
128     $entity_types = \Drupal::entityManager()->getDefinitions();
129     foreach ($this->bundleInfoService->getAllBundleInfo() as $entity_type_id => $bundles) {
130       // Only content entities are supported currently.
131       // @todo Consider supporting config entities.
132       if ($entity_types[$entity_type_id]->entityClassImplements(ConfigEntityInterface::class)) {
133         continue;
134       }
135       foreach ($bundles as $bundle => $bundle_info) {
136         // Get a type URI for the bundle.
137         $bundle_uri = $this->getTypeUri($entity_type_id, $bundle, $context);
138         $data[$bundle_uri] = [
139           'entity_type' => $entity_type_id,
140           'bundle' => $bundle,
141         ];
142       }
143     }
144     // These URIs only change when entity info changes, so cache it permanently
145     // and only clear it when entity_info is cleared.
146     $this->cache->set('hal:links:types', $data, Cache::PERMANENT, ['entity_types']);
147     return $data;
148   }
149
150 }