Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / rest / src / Plugin / Deriver / EntityDeriver.php
1 <?php
2
3 namespace Drupal\rest\Plugin\Deriver;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * Provides a resource plugin definition for every entity type.
11  *
12  * @see \Drupal\rest\Plugin\rest\resource\EntityResource
13  */
14 class EntityDeriver implements ContainerDeriverInterface {
15
16   /**
17    * List of derivative definitions.
18    *
19    * @var array
20    */
21   protected $derivatives;
22
23   /**
24    * The entity manager.
25    *
26    * @var \Drupal\Core\Entity\EntityManagerInterface
27    */
28   protected $entityManager;
29
30   /**
31    * Constructs an EntityDeriver object.
32    *
33    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
34    *   The entity manager.
35    */
36   public function __construct(EntityManagerInterface $entity_manager) {
37     $this->entityManager = $entity_manager;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function create(ContainerInterface $container, $base_plugin_id) {
44     return new static(
45       $container->get('entity.manager')
46     );
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
53     if (!isset($this->derivatives)) {
54       $this->getDerivativeDefinitions($base_plugin_definition);
55     }
56     if (isset($this->derivatives[$derivative_id])) {
57       return $this->derivatives[$derivative_id];
58     }
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getDerivativeDefinitions($base_plugin_definition) {
65     if (!isset($this->derivatives)) {
66       // Add in the default plugin configuration and the resource type.
67       foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
68         $this->derivatives[$entity_type_id] = [
69           'id' => 'entity:' . $entity_type_id,
70           'entity_type' => $entity_type_id,
71           'serialization_class' => $entity_type->getClass(),
72           'label' => $entity_type->getLabel(),
73         ];
74
75         $default_uris = [
76           'canonical' => "/entity/$entity_type_id/" . '{' . $entity_type_id . '}',
77           'create' => "/entity/$entity_type_id",
78         ];
79
80         foreach ($default_uris as $link_relation => $default_uri) {
81           // Check if there are link templates defined for the entity type and
82           // use the path from the route instead of the default.
83           if ($link_template = $entity_type->getLinkTemplate($link_relation)) {
84             $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $link_template;
85           }
86           else {
87             $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $default_uri;
88           }
89         }
90
91         $this->derivatives[$entity_type_id] += $base_plugin_definition;
92       }
93     }
94     return $this->derivatives;
95   }
96
97 }