1313
1414namespace Silverback \ApiComponentsBundle \ApiPlatform \Api ;
1515
16+ use ApiPlatform \Metadata \Exception \OperationNotFoundException ;
17+ use ApiPlatform \Metadata \Exception \ResourceClassNotFoundException ;
18+ use ApiPlatform \Metadata \Get ;
1619use ApiPlatform \Metadata \IriConverterInterface ;
1720use ApiPlatform \Metadata \Operation ;
21+ use ApiPlatform \Metadata \Operations ;
22+ use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
1823use ApiPlatform \Metadata \UrlGeneratorInterface ;
1924use Silverback \ApiComponentsBundle \Entity \Core \Route ;
2025
2328 */
2429class IriConverter implements IriConverterInterface
2530{
26- public function __construct (private IriConverterInterface $ decorated )
31+ public function __construct (private IriConverterInterface $ decorated, private ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory )
2732 {
2833 }
2934
@@ -33,8 +38,39 @@ public function getResourceFromIri(string $iri, array $context = [], ?Operation
3338 }
3439
3540 // We want relations when they are found, to use the IRI with the path
41+
42+ /**
43+ * @throws ResourceClassNotFoundException
44+ * @throws \Exception
45+ */
3646 public function getIriFromResource ($ resource , int $ referenceType = UrlGeneratorInterface::ABS_PATH , ?Operation $ operation = null , array $ context = []): ?string
3747 {
48+ if ($ operation ?->getName() === 'me ' ) {
49+ // we do not want to return the /me IRI if a Get endpoint is configured. The IRI should be canonical to the user's ID etc.
50+
51+ // get the API metadata of the class
52+ // find the Get operation - ApiPlatform\Metadata\Get
53+ // use this uriTemplate instead, overwrite $operation and the operation in context
54+ $ resourceIterator = $ this ->resourceMetadataCollectionFactory ->create ($ operation ->getClass ())->getIterator ();
55+ while ($ resourceIterator ->valid ()) {
56+ $ current = $ resourceIterator ->current ();
57+ /**
58+ * @var Operations $resourceOperations
59+ */
60+ $ resourceOperations = $ current ->getOperations ();
61+ $ operationIterator = $ resourceOperations ->getIterator ();
62+ while ($ operationIterator ->valid ()) {
63+ $ checkOperation = $ operationIterator ->current ();
64+ if ($ checkOperation instanceof Get) {
65+ $ operation = $ checkOperation ;
66+ $ context ['operation ' ] = $ checkOperation ;
67+ break 2 ;
68+ }
69+ $ operationIterator ->next ();
70+ }
71+ $ resourceIterator ->next ();
72+ }
73+ }
3874 $ originalIri = $ this ->decorated ->getIriFromResource ($ resource , $ referenceType , $ operation , $ context );
3975
4076 if (!$ resource instanceof Route || !($ path = $ resource ->getPath ())) {
0 commit comments