Search in sources :

Example 71 with ArrayMemory

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");
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) List(java.util.List) Map(java.util.Map) MiscStream(php.runtime.ext.core.classes.stream.MiscStream)

Example 72 with ArrayMemory

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();
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory)

Example 73 with ArrayMemory

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;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) DoubleMemory(php.runtime.memory.DoubleMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) StringMemory(php.runtime.memory.StringMemory) ObjectMemory(php.runtime.memory.ObjectMemory) DoubleMemory(php.runtime.memory.DoubleMemory) Map(java.util.Map) StdClass(php.runtime.lang.StdClass)

Example 74 with ArrayMemory

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;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory) ParameterEntity(php.runtime.reflection.ParameterEntity) ObjectMemory(php.runtime.memory.ObjectMemory) CompileFunctionEntity(php.runtime.reflection.CompileFunctionEntity)

Example 75 with ArrayMemory

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;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory) ParameterEntity(php.runtime.reflection.ParameterEntity) ObjectMemory(php.runtime.memory.ObjectMemory)

Aggregations

ArrayMemory (php.runtime.memory.ArrayMemory)148 Memory (php.runtime.Memory)60 ForeachIterator (php.runtime.lang.ForeachIterator)41 ObjectMemory (php.runtime.memory.ObjectMemory)35 LongMemory (php.runtime.memory.LongMemory)31 StringMemory (php.runtime.memory.StringMemory)29 ReferenceMemory (php.runtime.memory.ReferenceMemory)25 KeyValueMemory (php.runtime.memory.KeyValueMemory)17 Invoker (php.runtime.invoke.Invoker)9 ClassEntity (php.runtime.reflection.ClassEntity)9 Signature (php.runtime.annotation.Reflection.Signature)6 IObject (php.runtime.lang.IObject)6 Map (java.util.Map)4 Test (org.junit.Test)4 DoubleMemory (php.runtime.memory.DoubleMemory)4 ConcurrentEnvironment (php.runtime.env.ConcurrentEnvironment)3 TraceInfo (php.runtime.env.TraceInfo)3 ModuleEntity (php.runtime.reflection.ModuleEntity)3 ParameterEntity (php.runtime.reflection.ParameterEntity)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2