17
17
use RuntimeException ;
18
18
use Symfony \Bundle \FrameworkBundle \Routing \Router ;
19
19
use Exception ;
20
+ use Symfony \Component \HttpFoundation \Request ;
20
21
21
22
/**
22
23
* Class JsonApiSerializer
@@ -31,6 +32,7 @@ class JsonApiSerializer extends \NilPortugues\Api\JsonApi\JsonApiSerializer
31
32
public function __construct (JsonApiTransformer $ transformer , Router $ router )
32
33
{
33
34
$ this ->mapUrls ($ transformer , $ router );
35
+
34
36
parent ::__construct ($ transformer );
35
37
}
36
38
@@ -40,6 +42,9 @@ public function __construct(JsonApiTransformer $transformer, Router $router)
40
42
*/
41
43
private function mapUrls (JsonApiTransformer $ transformer , Router $ router )
42
44
{
45
+ $ request = Request::createFromGlobals ();
46
+ $ baseUrl = $ request ->getSchemeAndHttpHost ();
47
+
43
48
$ reflectionClass = new ReflectionClass ($ transformer );
44
49
$ reflectionProperty = $ reflectionClass ->getProperty ('mappings ' );
45
50
$ reflectionProperty ->setAccessible (true );
@@ -48,14 +53,14 @@ private function mapUrls(JsonApiTransformer $transformer, Router $router)
48
53
foreach ($ mappings as &$ mapping ) {
49
54
$ mappingClass = new ReflectionClass ($ mapping );
50
55
51
- $ this ->setUrlWithReflection ($ router , $ mapping , $ mappingClass , 'resourceUrlPattern ' );
52
- $ this ->setUrlWithReflection ($ router , $ mapping , $ mappingClass , 'selfUrl ' );
56
+ $ this ->setUrlWithReflection ($ router , $ mapping , $ mappingClass , 'resourceUrlPattern ' , $ baseUrl );
57
+ $ this ->setUrlWithReflection ($ router , $ mapping , $ mappingClass , 'selfUrl ' , $ baseUrl );
53
58
54
59
$ mappingProperty = $ mappingClass ->getProperty ('otherUrls ' );
55
60
$ mappingProperty ->setAccessible (true );
56
61
$ otherUrls = $ mappingProperty ->getValue ($ mapping );
57
62
foreach ($ otherUrls as &$ url ) {
58
- $ url = $ this ->getUrlPattern ($ router , $ url );
63
+ $ url = $ this ->getUrlPattern ($ router , $ url, $ baseUrl );
59
64
}
60
65
$ mappingProperty ->setValue ($ mapping , $ otherUrls );
61
66
@@ -64,7 +69,7 @@ private function mapUrls(JsonApiTransformer $transformer, Router $router)
64
69
$ relationshipSelfUrl = $ mappingProperty ->getValue ($ mapping );
65
70
foreach ($ relationshipSelfUrl as &$ urlMember ) {
66
71
foreach ($ urlMember as &$ url ) {
67
- $ url = $ this ->getUrlPattern ($ router , $ url );
72
+ $ url = $ this ->getUrlPattern ($ router , $ url, $ baseUrl );
68
73
}
69
74
}
70
75
$ mappingProperty ->setValue ($ mapping , $ relationshipSelfUrl );
@@ -79,12 +84,12 @@ private function mapUrls(JsonApiTransformer $transformer, Router $router)
79
84
* @param ReflectionClass $mappingClass
80
85
* @param string $property
81
86
*/
82
- private function setUrlWithReflection (Router $ router , Mapping $ mapping , ReflectionClass $ mappingClass , $ property )
87
+ private function setUrlWithReflection (Router $ router , Mapping $ mapping , ReflectionClass $ mappingClass , $ property, $ baseUrl )
83
88
{
84
89
$ mappingProperty = $ mappingClass ->getProperty ($ property );
85
90
$ mappingProperty ->setAccessible (true );
86
91
$ value = $ mappingProperty ->getValue ($ mapping );
87
- $ value = $ this ->getUrlPattern ($ router , $ value );
92
+ $ value = $ this ->getUrlPattern ($ router , $ value, $ baseUrl );
88
93
$ mappingProperty ->setValue ($ mapping , $ value );
89
94
}
90
95
@@ -96,7 +101,7 @@ private function setUrlWithReflection(Router $router, Mapping $mapping, Reflecti
96
101
*
97
102
* @throws RuntimeException
98
103
*/
99
- private function getUrlPattern (Router $ router , $ routeNameFromMappingFile )
104
+ private function getUrlPattern (Router $ router , $ routeNameFromMappingFile, $ baseUrl )
100
105
{
101
106
if (!empty ($ routeNameFromMappingFile )) {
102
107
try {
@@ -117,7 +122,7 @@ private function getUrlPattern(Router $router, $routeNameFromMappingFile)
117
122
$ pattern = \array_combine ($ matches [1 ], $ matches [0 ]);
118
123
}
119
124
120
- return \urldecode ($ router ->generate ($ routeNameFromMappingFile , $ pattern , true ));
125
+ return $ baseUrl . \urldecode ($ router ->generate ($ routeNameFromMappingFile , $ pattern , true ));
121
126
}
122
127
123
128
return (string ) $ routeNameFromMappingFile ;
0 commit comments