use of php.runtime.memory.ArrayMemory in project jphp by jphp-compiler.
the class PHttpClient method applyConnection.
@Signature
protected void applyConnection(PHttpResponse response, URLConnection _connection, Environment env) throws IOException {
if (_connection instanceof HttpURLConnection) {
HttpURLConnection connection = (HttpURLConnection) _connection;
response.setStatusCode(connection.getResponseCode());
response.setStatusMessage(connection.getResponseMessage());
try {
response.setBodyStream(new MiscStream(env, connection.getInputStream()));
} catch (IOException e) {
response.setBodyStream(new MiscStream(env, connection.getErrorStream()));
}
// headers.
ArrayMemory headers = new ArrayMemory();
Map<String, List<String>> headerFields = connection.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {
String name = entry.getKey();
List<String> value = entry.getValue();
if (value.size() == 1) {
headers.putAsKeyString(name, StringMemory.valueOf(value.get(0)));
} else {
headers.putAsKeyString(name, ArrayMemory.ofStringCollection(value));
}
}
response.setHeaders(headers);
ArrayMemory cookies = new ArrayMemory();
String cookieHeader = connection.getHeaderField("Set-Cookie");
if (cookieHeader != null && !cookieHeader.isEmpty()) {
List<HttpCookie> httpCookies = HttpCookie.parse(cookieHeader);
for (HttpCookie cookie : httpCookies) {
ArrayMemory memory = HttpClientExtension.cookieToArray(cookie);
cookies.putAsKeyString(cookie.getName(), memory);
}
}
response.setRawCookies(cookies);
} else {
throw new IllegalStateException("Argument 2 must be instance of HttpURLConnection");
}
}
use of php.runtime.memory.ArrayMemory in project jphp by jphp-compiler.
the class PSqlConnection method getSchemas.
@Signature
public Memory getSchemas(Environment env) throws SQLException {
ResultSet schemas = metaData.getSchemas();
ArrayMemory r = new ArrayMemory();
while (schemas.next()) {
r.add(new PSqlResult(env, schemas).toArray(env));
}
return r.toConstant();
}
use of php.runtime.memory.ArrayMemory in project jphp by jphp-compiler.
the class MemoryDeserializer method convert.
protected Memory convert(JsonElement json, int depth) {
if (depth > maxDepth)
throw new MaxDepthException();
if (json.isJsonNull())
return Memory.NULL;
if (json.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
if (jsonPrimitive.isString())
return StringMemory.valueOf(jsonPrimitive.getAsString());
else if (jsonPrimitive.isBoolean())
return jsonPrimitive.getAsBoolean() ? Memory.TRUE : Memory.FALSE;
else if (jsonPrimitive.isNumber()) {
Memory l = StringMemory.toLong(jsonPrimitive.getAsString());
if (l != null)
return l;
else
return new DoubleMemory(json.getAsDouble());
}
return Memory.NULL;
} else if (json.isJsonArray()) {
ArrayMemory array = new ArrayMemory();
for (JsonElement el : json.getAsJsonArray()) array.add(convert(el, depth + 1).toImmutable());
return array.toConstant();
} else if (json.isJsonObject()) {
JsonObject jsonObject = json.getAsJsonObject();
StdClass stdClass = assoc ? null : new StdClass(env.get());
ArrayMemory array = assoc ? new ArrayMemory() : stdClass.getProperties();
for (Map.Entry<String, JsonElement> el : jsonObject.entrySet()) {
String key = el.getKey();
if (!key.startsWith("\0"))
array.put(key, convert(el.getValue(), depth + 1).toImmutable());
}
return assoc ? array : new ObjectMemory(stdClass);
} else
return Memory.NULL;
}
use of php.runtime.memory.ArrayMemory in project jphp by jphp-compiler.
the class ReflectionFunction method getParameters.
@Override
@Signature
public Memory getParameters(Environment env, Memory... args) {
if (cachedParameters != null)
return cachedParameters;
if (functionEntity instanceof CompileFunctionEntity)
exception(env, "Cannot get parameters for internal function %s()", functionEntity.getName());
ParameterEntity[] parameters = closureEntity == null ? functionEntity.getParameters() : closureEntity.parameters;
ClassEntity entity = env.fetchClass("ReflectionParameter");
ArrayMemory result = new ArrayMemory();
int i = 0;
for (ParameterEntity param : parameters) {
ReflectionParameter e = new ReflectionParameter(env, entity);
e.setEntity(param);
e.setFunctionEntity(functionEntity);
e.setPosition(i);
i++;
result.add(new ObjectMemory(e));
}
return cachedParameters = result;
}
use of php.runtime.memory.ArrayMemory in project jphp by jphp-compiler.
the class ReflectionMethod method getParameters.
@Override
@Signature
public Memory getParameters(Environment env, Memory... args) {
if (cachedParameters != null)
return cachedParameters;
ParameterEntity[] parameters = methodEntity.getParameters(Integer.MAX_VALUE);
ClassEntity entity = env.fetchClass("ReflectionParameter");
ArrayMemory result = new ArrayMemory();
int i = 0;
for (ParameterEntity param : parameters) {
ReflectionParameter e = new ReflectionParameter(env, entity);
e.setEntity(param);
e.setFunctionEntity(methodEntity);
e.setPosition(i);
i++;
result.add(new ObjectMemory(e));
}
return cachedParameters = result;
}
Aggregations