66 */
77namespace Ibexa \Bundle \Rest \RequestParser ;
88
9- use Ibexa \Contracts \Rest \Exceptions \ InvalidArgumentException ;
9+ use Ibexa \Contracts \Rest \UriParser \ UriParserInterface ;
1010use Ibexa \Rest \RequestParser ;
1111use Symfony \Component \HttpFoundation \Request ;
12- use Symfony \Component \Routing \Exception \ResourceNotFoundException ;
1312use Symfony \Component \Routing \RouterInterface ;
1413
1514/**
15+ * @deprecated use \Ibexa\Contracts\Rest\UriParser\UriParserInterface instead
16+ * @see \Ibexa\Contracts\Rest\UriParser\UriParserInterface
17+ *
1618 * Router based request parser.
1719 */
1820class Router implements RequestParser
1921{
20- /**
21- * @var \Symfony\Cmf\Component\Routing\ChainRouter
22- */
23- private $ router ;
22+ private RouterInterface $ router ;
23+
24+ private UriParserInterface $ uriParser ;
2425
25- public function __construct (RouterInterface $ router )
26+ public function __construct (RouterInterface $ router, UriParserInterface $ uriParser )
2627 {
2728 $ this ->router = $ router ;
29+ $ this ->uriParser = $ uriParser ;
2830 }
2931
3032 /**
31- * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException If no match was found
33+ * @return array<mixed> matched route configuration and parameters
34+ *
35+ * @throws \Ibexa\Contracts\Rest\Exceptions\InvalidArgumentException If no match was found
3236 */
33- public function parse ($ url )
37+ public function parse ($ url ): array
3438 {
35- // we create a request with a new context in order to match $url to a route and get its properties
36- $ request = Request::create ($ url , 'GET ' );
37- $ originalContext = $ this ->router ->getContext ();
38- $ context = clone $ originalContext ;
39- $ context ->fromRequest ($ request );
40- $ this ->router ->setContext ($ context );
41-
42- try {
43- $ matchResult = $ this ->router ->matchRequest ($ request );
44- } catch (ResourceNotFoundException $ e ) {
45- // Note: this probably won't occur in real life because of the legacy matcher
46- $ this ->router ->setContext ($ originalContext );
47- throw new InvalidArgumentException ("No route matched ' $ url' " );
48- }
49-
50- if (!$ this ->matchesRestRequest ($ matchResult )) {
51- $ this ->router ->setContext ($ originalContext );
52- throw new InvalidArgumentException ("No route matched ' $ url' " );
53- }
54-
55- $ this ->router ->setContext ($ originalContext );
56-
57- return $ matchResult ;
39+ return $ this ->uriParser ->matchUri ($ url );
5840 }
5941
6042 public function generate ($ type , array $ values = [])
@@ -63,35 +45,11 @@ public function generate($type, array $values = [])
6345 }
6446
6547 /**
66- * @throws \Ibexa\Core\Base \Exceptions\InvalidArgumentException If $attribute wasn't found in the match
48+ * @throws \Ibexa\Contracts\ Core\Repository \Exceptions\InvalidArgumentException If $attribute wasn't found in the match
6749 */
6850 public function parseHref ($ href , $ attribute )
6951 {
70- $ parsingResult = $ this ->parse ($ href );
71-
72- if (!isset ($ parsingResult [$ attribute ])) {
73- throw new InvalidArgumentException ("No attribute ' $ attribute' in route matched from $ href " );
74- }
75-
76- return $ parsingResult [$ attribute ];
77- }
78-
79- /**
80- * Checks if a router match response matches a REST resource.
81- *
82- * @param array $match Match array returned by Router::match() / Router::matchRequest()
83- *
84- * @throws \Ibexa\Contracts\Rest\Exceptions\InvalidArgumentException if the \$match isn't valid
85- *
86- * @return bool
87- */
88- private function matchesRestRequest (array $ match )
89- {
90- if (!isset ($ match ['_route ' ])) {
91- throw new InvalidArgumentException ('Invalid $match parameter, no _route key ' );
92- }
93-
94- return strpos ($ match ['_route ' ], 'ibexa.rest. ' ) === 0 ;
52+ return $ this ->uriParser ->getAttributeFromUri ($ href , $ attribute );
9553 }
9654}
9755
0 commit comments