1313
1414namespace Silverback \ApiComponentsBundle \ApiPlatform \Api ;
1515
16- use ApiPlatform \Metadata \Exception \OperationNotFoundException ;
17- use ApiPlatform \Metadata \Exception \ResourceClassNotFoundException ;
1816use ApiPlatform \Metadata \Get ;
1917use ApiPlatform \Metadata \IriConverterInterface ;
2018use ApiPlatform \Metadata \Operation ;
@@ -37,40 +35,43 @@ public function getResourceFromIri(string $iri, array $context = [], ?Operation
3735 return $ this ->decorated ->getResourceFromIri ($ iri , $ context , $ operation );
3836 }
3937
40- // We want relations when they are found, to use the IRI with the path
41-
42- /**
43- * @throws ResourceClassNotFoundException
44- * @throws \Exception
45- */
46- public function getIriFromResource ($ resource , int $ referenceType = UrlGeneratorInterface::ABS_PATH , ?Operation $ operation = null , array $ context = []): ?string
38+ private function getUserGetOperation (?Operation $ operation = null ): Operation
4739 {
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.
40+ // 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.
5041
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 ();
42+ // get the API metadata of the class
43+ // find the Get operation - ApiPlatform\Metadata\Get
44+ // use this uriTemplate instead, overwrite $operation and the operation in context
45+ $ resourceIterator = $ this ->resourceMetadataCollectionFactory ->create ($ operation ->getClass ())->getIterator ();
46+ while ($ resourceIterator ->valid ()) {
47+ $ current = $ resourceIterator ->current ();
48+ /**
49+ * @var Operations $resourceOperations
50+ */
51+ $ resourceOperations = $ current ->getOperations ();
52+ $ operationIterator = $ resourceOperations ->getIterator ();
53+ while ($ operationIterator ->valid ()) {
54+ $ checkOperation = $ operationIterator ->current ();
55+ if ($ checkOperation instanceof Get) {
56+ return $ checkOperation ;
7057 }
71- $ resourceIterator ->next ();
58+ $ operationIterator ->next ();
7259 }
60+ $ resourceIterator ->next ();
7361 }
62+
63+ return $ operation ;
64+ }
65+
66+ // We want relations when they are found, to use the IRI with the path
67+ public function getIriFromResource ($ resource , int $ referenceType = UrlGeneratorInterface::ABS_PATH , ?Operation $ operation = null , array $ context = []): ?string
68+ {
69+ if ('me ' === $ operation ?->getName()) {
70+ $ checkOperation = $ this ->getUserGetOperation ($ operation );
71+ $ operation = $ checkOperation ;
72+ $ context ['operation ' ] = $ checkOperation ;
73+ }
74+
7475 $ originalIri = $ this ->decorated ->getIriFromResource ($ resource , $ referenceType , $ operation , $ context );
7576
7677 if (!$ resource instanceof Route || !($ path = $ resource ->getPath ())) {
0 commit comments