Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / rest / src / PathProcessor / PathProcessorEntityResourceBC.php
1 <?php
2
3 namespace Drupal\rest\PathProcessor;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
7 use Symfony\Component\HttpFoundation\Request;
8
9 /**
10  * Path processor to maintain BC for entity REST resource URLs from Drupal 8.0.
11  */
12 class PathProcessorEntityResourceBC implements InboundPathProcessorInterface {
13
14   /**
15    * The entity type manager.
16    *
17    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
18    */
19   protected $entityTypeManager;
20
21   /**
22    * Creates a new PathProcessorEntityResourceBC instance.
23    *
24    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
25    *   The entity type manager.
26    */
27   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
28     $this->entityTypeManager = $entity_type_manager;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function processInbound($path, Request $request) {
35     if ($request->getMethod() === 'POST' && strpos($path, '/entity/') === 0) {
36       $parts = explode('/', $path);
37       $entity_type_id = array_pop($parts);
38
39       // Until Drupal 8.3, no entity types specified a link template for the
40       // 'create' link relation type. As of Drupal 8.3, all core content entity
41       // types provide this link relation type. This inbound path processor
42       // provides automatic backwards compatibility: it allows both the old
43       // default from \Drupal\rest\Plugin\rest\resource\EntityResource, i.e.
44       // "/entity/{entity_type}" and the link template specified in a particular
45       // entity type. The former is rewritten to the latter
46       // specific one if it exists.
47       $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
48       if ($entity_type->hasLinkTemplate('create')) {
49         return $entity_type->getLinkTemplate('create');
50       }
51     }
52     return $path;
53   }
54
55 }