Move extracting documents from request objects to a service
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3104972. --> Reported by: [mglaman](https://www.drupal.org/user/2416470) >>> <p>While working on <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/jsonapi_user_resources/issues/3104957" title="Status: Closed (fixed)">#3104957: Fire an event to allow modules to react to user registration</a></span>, I needed the top level JSON:API document to receive some metadata. Alas, I could not. After speaking with @gabesullice, we have an idea.</p> <p>The DocumentExtractor service. This takes a request object and returns a JSON:API document. This extracts logic currently in the DocumentResolver argument resolver and moves it to a service that supports caching based on requests.</p> <p>We currently do something like this in Commerce for resolving the current store per request with \SplObjectStorage.</p> <p><a href="https://git.drupalcode.org/project/commerce/blob/8.x-2.x/modules/store/src/CurrentStore.php#L63">https://git.drupalcode.org/project/commerce/blob/8.x-2.x/modules/store/src/CurrentStore.php#L63</a></p> <pre>&nbsp;&nbsp;&nbsp; $request = $this-&gt;requestStack-&gt;getCurrentRequest();<br>&nbsp;&nbsp;&nbsp; if (!$this-&gt;stores-&gt;contains($request)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;stores[$request] = $this-&gt;chainResolver-&gt;resolve();<br>&nbsp;&nbsp;&nbsp; }</pre><p>The DocumentExtractor can check if the request exists in cache and then return the extracted document, otherwise it can perform actions. This makes it possible for anyone to take a request and receive a JSON:API documentation! (for better or worse!)</p>
issue