@@ -71,25 +71,30 @@ public function __construct($container, $config)
71
71
public function execute (Request $ httprequest )
72
72
{
73
73
$ json = $ httprequest ->getContent ();
74
- $ request = json_decode ($ json );
74
+ $ request = json_decode ($ json , true );
75
+
75
76
if ($ request === null ) {
76
77
return $ this ->getErrorResponse (self ::PARSE_ERROR , null );
77
- } elseif (!(isset ($ request-> jsonrpc ) && isset ($ request-> method ) && $ request-> jsonrpc == '2.0 ' )) {
78
- return $ this ->getErrorResponse (self ::INVALID_REQUEST , $ request-> id );
78
+ } elseif (!(isset ($ request[ ' jsonrpc ' ] ) && isset ($ request[ ' method ' ] ) && $ request[ ' jsonrpc ' ] == '2.0 ' )) {
79
+ return $ this ->getErrorResponse (self ::INVALID_REQUEST , $ request[ ' id ' ] );
79
80
}
80
- if (!in_array ($ request ->method , array_keys ($ this ->config ['functions ' ]))) {
81
- return $ this ->getErrorResponse (self ::METHOD_NOT_FOUND , $ request ->id );
81
+
82
+ if (!in_array ($ request ['method ' ], array_keys ($ this ->config ['functions ' ]))) {
83
+ return $ this ->getErrorResponse (self ::METHOD_NOT_FOUND , $ request ['id ' ]);
82
84
}
83
- $ service = $ this ->container ->get ($ this ->config ['functions ' ][$ request ->method ]['service ' ]);
84
- $ method = $ this ->config ['functions ' ][$ request ->method ]['method ' ];
85
- $ params = (isset ($ request ->params ) ? $ request ->params : array ());
85
+
86
+ $ service = $ this ->container ->get ($ this ->config ['functions ' ][$ request ['method ' ]]['service ' ]);
87
+ $ method = $ this ->config ['functions ' ][$ request ['method ' ]]['method ' ];
88
+ $ params = (isset ($ request ['params ' ]) ? $ request ['params ' ] : array ());
89
+
86
90
if (is_callable (array ($ service , $ method ))) {
87
91
$ r = new \ReflectionMethod ($ service , $ method );
92
+
88
93
if (is_array ($ params )) {
89
94
if (!(count ($ params ) >= $ r ->getNumberOfRequiredParameters ()
90
95
&& count ($ params ) <= $ r ->getNumberOfParameters ())
91
96
) {
92
- return $ this ->getErrorResponse (self ::INVALID_PARAMS , $ request-> id ,
97
+ return $ this ->getErrorResponse (self ::INVALID_PARAMS , $ request[ ' id ' ] ,
93
98
sprintf ('Number of given parameters (%d) does not match the number of expected parameters (%d required, %d total) ' ,
94
99
count ($ params ), $ r ->getNumberOfRequiredParameters (), $ r ->getNumberOfParameters ()));
95
100
}
@@ -100,7 +105,7 @@ public function execute(Request $httprequest)
100
105
/* @var \ReflectionParameter $rp */
101
106
$ name = $ rp ->name ;
102
107
if (!isset ($ params ->$ name ) && !$ rp ->isOptional ()) {
103
- return $ this ->getErrorResponse (self ::INVALID_PARAMS , $ request-> id ,
108
+ return $ this ->getErrorResponse (self ::INVALID_PARAMS , $ request[ ' id ' ] ,
104
109
sprintf ('Parameter %s is missing ' , $ name ));
105
110
}
106
111
if (isset ($ params ->$ name )) {
@@ -111,23 +116,26 @@ public function execute(Request $httprequest)
111
116
}
112
117
$ params = $ newparams ;
113
118
}
119
+
114
120
try {
115
121
$ result = call_user_func_array (array ($ service , $ method ), $ params );
116
122
} catch (\Exception $ e ) {
117
- return $ this ->getErrorResponse (self ::INTERNAL_ERROR , $ request-> id , $ e ->getMessage ());
123
+ return $ this ->getErrorResponse (self ::INTERNAL_ERROR , $ request[ ' id ' ] , $ e ->getMessage ());
118
124
}
125
+
119
126
$ response = array ('jsonrpc ' => '2.0 ' );
120
127
$ response ['result ' ] = $ result ;
121
- $ response ['id ' ] = (isset ($ request-> id ) ? $ request-> id : null );
128
+ $ response ['id ' ] = (isset ($ request[ ' id ' ] ) ? $ request[ ' id ' ] : null );
122
129
123
130
if ($ this ->container ->has ('jms_serializer ' )) {
124
131
$ response = $ this ->container ->get ('jms_serializer ' )->serialize ($ response , 'json ' , $ this ->serializationContext );
125
132
} else {
126
133
$ response = json_encode ($ response );
127
134
}
135
+
128
136
return new Response ($ response , 200 , array ('Content-Type ' => 'application/json ' ));
129
137
} else {
130
- return $ this ->getErrorResponse (self ::METHOD_NOT_FOUND , $ request-> id );
138
+ return $ this ->getErrorResponse (self ::METHOD_NOT_FOUND , $ request[ ' id ' ] );
131
139
}
132
140
}
133
141
@@ -151,15 +159,21 @@ protected function getError($code)
151
159
$ message = 'Internal error ' ;
152
160
break ;
153
161
}
162
+
154
163
return array ('code ' => $ code , 'message ' => $ message );
155
164
}
156
165
157
166
protected function getErrorResponse ($ code , $ id , $ data = null )
158
167
{
159
168
$ response = array ('jsonrpc ' => '2.0 ' );
160
169
$ response ['error ' ] = $ this ->getError ($ code );
161
- if ($ data != null ) $ response ['error ' ]['data ' ] = $ data ;
170
+
171
+ if ($ data != null ) {
172
+ $ response ['error ' ]['data ' ] = $ data ;
173
+ }
174
+
162
175
$ response ['id ' ] = $ id ;
176
+
163
177
return new Response (json_encode ($ response ), 200 , array ('Content-Type ' => 'application/json ' ));
164
178
}
165
179
0 commit comments