@@ -57,49 +57,79 @@ public function resolve(
57
57
throw new GraphQlInputException (__ ('"url" argument should be specified and not empty ' ));
58
58
}
59
59
60
+ $ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
60
61
$ result = null ;
61
62
$ url = $ args ['url ' ];
62
63
if (substr ($ url , 0 , 1 ) === '/ ' && $ url !== '/ ' ) {
63
64
$ url = ltrim ($ url , '/ ' );
64
65
}
65
66
$ customUrl = $ this ->customUrlLocator ->locateUrl ($ url );
66
67
$ url = $ customUrl ?: $ url ;
67
- $ urlRewrite = $ this ->findCanonicalUrl ($ url , (int )$ context ->getExtensionAttributes ()->getStore ()->getId ());
68
- if ($ urlRewrite ) {
69
- if (!$ urlRewrite ->getEntityId ()) {
68
+ $ finalUrlRewrite = $ this ->findFinalUrl ($ url , $ storeId );
69
+ if ($ finalUrlRewrite ) {
70
+ $ relativeUrl = $ finalUrlRewrite ->getRequestPath ();
71
+ $ resultArray = $ this ->rewriteCustomUrls ($ finalUrlRewrite , $ storeId ) ?? [
72
+ 'id ' => $ finalUrlRewrite ->getEntityId (),
73
+ 'canonical_url ' => $ relativeUrl ,
74
+ 'relative_url ' => $ relativeUrl ,
75
+ 'type ' => $ this ->sanitizeType ($ finalUrlRewrite ->getEntityType ())
76
+ ];
77
+
78
+
79
+ if (empty ($ resultArray ['id ' ])) {
70
80
throw new GraphQlNoSuchEntityException (
71
81
__ ('No such entity found with matching URL key: %url ' , ['url ' => $ url ])
72
82
);
73
83
}
74
- $ result = [
75
- 'id ' => $ urlRewrite ->getEntityId (),
76
- 'canonical_url ' => $ urlRewrite ->getTargetPath (),
77
- 'relative_url ' => $ urlRewrite ->getTargetPath (),
78
- 'type ' => $ this ->sanitizeType ($ urlRewrite ->getEntityType ())
79
- ];
84
+
85
+ $ result = $ resultArray ;
80
86
}
81
87
return $ result ;
82
88
}
83
89
84
90
/**
85
- * Find the canonical url passing through all redirects if any
91
+ * Handle custom urls with and without redirects
92
+ *
93
+ * @param UrlRewrite $finalUrlRewrite
94
+ * @param int $storeId
95
+ * @return array|null
96
+ */
97
+ private function rewriteCustomUrls (UrlRewrite $ finalUrlRewrite , int $ storeId ): ?array
98
+ {
99
+ if ($ finalUrlRewrite ->getEntityType () === 'custom ' || !($ finalUrlRewrite ->getEntityId () > 0 )) {
100
+ $ finalCustomUrlRewrite = clone $ finalUrlRewrite ;
101
+ $ finalUrlRewrite = $ this ->findFinalUrl ($ finalCustomUrlRewrite ->getTargetPath (), $ storeId , true );
102
+ $ relativeUrl =
103
+ $ finalCustomUrlRewrite ->getRedirectType () == 0
104
+ ? $ finalCustomUrlRewrite ->getRequestPath () : $ finalUrlRewrite ->getRequestPath ();
105
+ return [
106
+ 'id ' => $ finalUrlRewrite ->getEntityId (),
107
+ 'canonical_url ' => $ relativeUrl ,
108
+ 'relative_url ' => $ relativeUrl ,
109
+ 'type ' => $ this ->sanitizeType ($ finalUrlRewrite ->getEntityType ())
110
+ ];
111
+ }
112
+ return null ;
113
+ }
114
+
115
+ /**
116
+ * Find the final url passing through all redirects if any
86
117
*
87
118
* @param string $requestPath
88
119
* @param int $storeId
120
+ * @param bool $findCustom
89
121
* @return UrlRewrite|null
90
122
*/
91
- private function findCanonicalUrl (string $ requestPath , int $ storeId) : ?UrlRewrite
123
+ private function findFinalUrl (string $ requestPath , int $ storeId, bool $ findCustom = false ) : ?UrlRewrite
92
124
{
93
125
$ urlRewrite = $ this ->findUrlFromRequestPath ($ requestPath , $ storeId );
94
- if ($ urlRewrite && $ urlRewrite ->getRedirectType () > 0 ) {
95
- while ($ urlRewrite && $ urlRewrite ->getRedirectType () > 0 ) {
96
- $ urlRewrite = $ this ->findUrlFromRequestPath ($ urlRewrite ->getTargetPath (), $ storeId );
97
- }
126
+ while ($ urlRewrite && $ urlRewrite ->getRedirectType () > 0 ) {
127
+ $ urlRewrite = $ this ->findUrlFromRequestPath ($ urlRewrite ->getTargetPath (), $ storeId );
98
128
}
99
129
if (!$ urlRewrite ) {
100
130
$ urlRewrite = $ this ->findUrlFromTargetPath ($ requestPath , $ storeId );
101
131
}
102
- if ($ urlRewrite && !$ urlRewrite ->getEntityId () && !$ urlRewrite ->getIsAutogenerated ()) {
132
+ if ($ urlRewrite && ( $ findCustom && !$ urlRewrite ->getEntityId () && !$ urlRewrite ->getIsAutogenerated () )) {
103
133
$ urlRewrite = $ this ->findUrlFromTargetPath ($ urlRewrite ->getTargetPath (), $ storeId );
104
134
}
105
135
@@ -113,7 +143,7 @@ private function findCanonicalUrl(string $requestPath, int $storeId) : ?UrlRewri
113
143
* @param int $storeId
114
144
* @return UrlRewrite|null
115
145
*/
116
- private function findUrlFromRequestPath (string $ requestPath , int $ storeId ) : ?UrlRewrite
146
+ private function findUrlFromRequestPath (string $ requestPath , int $ storeId ): ?UrlRewrite
117
147
{
118
148
return $ this ->urlFinder ->findOneByData (
119
149
[
@@ -130,7 +160,7 @@ private function findUrlFromRequestPath(string $requestPath, int $storeId) : ?Ur
130
160
* @param int $storeId
131
161
* @return UrlRewrite|null
132
162
*/
133
- private function findUrlFromTargetPath (string $ targetPath , int $ storeId ) : ?UrlRewrite
163
+ private function findUrlFromTargetPath (string $ targetPath , int $ storeId ): ?UrlRewrite
134
164
{
135
165
return $ this ->urlFinder ->findOneByData (
136
166
[
0 commit comments