variablemodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 798 行 · 第 1/2 页

JAVA
798
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.quercus.lib;import com.caucho.quercus.QuercusModuleException;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.annotation.ReadOnly;import com.caucho.quercus.annotation.Reference;import com.caucho.quercus.annotation.ReturnNullAsFalse;import com.caucho.quercus.annotation.UsesSymbolTable;import com.caucho.quercus.env.*;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.util.L10N;import com.caucho.util.LruCache;import com.caucho.vfs.StringWriter;import com.caucho.vfs.WriteStream;import java.io.IOException;import java.lang.ref.*;import java.lang.reflect.Method;import java.util.HashMap;import java.util.IdentityHashMap;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;/** * Information about PHP variables. */public class VariableModule extends AbstractQuercusModule {  private static final Logger log    = Logger.getLogger(VariableModule.class.getName());  private static final L10N L = new L10N(VariableModule.class);  private static final    LruCache<UnserializeKey,UnserializeCacheEntry> _unserializeCache    = new LruCache<UnserializeKey,UnserializeCacheEntry>(256);  /**   * Returns a constant   *   * @param env the quercus calling environment   * @param name the constant name   */  public static Value constant(Env env, String name)  {    return env.getConstant(name);  }  /**   * Prints a debug version of the variable   *   * @param env the quercus calling environment   * @param v the variable to print   * @return the escaped stringPhp   */  public static Value debug_zval_dump(Env env, @ReadOnly Value v)  {    try {      debug_impl(env, v, 0);      return NullValue.NULL;    } catch (IOException e) {      throw new QuercusModuleException(e);    }  }  /**   * Defines a constant   *   * @param env the quercus calling environment   * @param name the constant name   * @param value the constant value   */  public static Value define(Env env,			     StringValue name,			     Value value,			     @Optional boolean isCaseInsensitive)  {    return env.addConstant(name.toString(), value, isCaseInsensitive);  }  /**   * Returns true if the constant is defined.   *   * @param env the quercus calling environment   * @param name the constant name   */  public static boolean defined(Env env, StringValue name)  {    return env.isDefined(name.toString());  }  /**   * Converts to a double   *   * @param v the variable to convert   * @return the double value   */  public static Value doubleval(@ReadOnly Value v)  {    return floatval(v);  }  /**   * Returns true for an empty variable.   *   * @param v the value to test   *   * @return true if the value is empty   */  public static boolean empty(@ReadOnly Value v)  {    return v.isEmpty();  }  /**   * Converts to a double   *   * @param v the variable to convert   * @return the double value   */  public static Value floatval(@ReadOnly Value v)  {    return new DoubleValue(v.toDouble());  }  /**   * Returns the defined variables in the current scope.   */  @UsesSymbolTable  public static Value get_defined_vars(Env env)  {    ArrayValue result = new ArrayValueImpl();    HashMap<String,Var> map = env.getEnv();    for (Map.Entry<String,Var> entry : map.entrySet()) {      result.append(env.createString(entry.getKey()),		    entry.getValue().toValue());    }    HashMap<String,Var> globalMap = env.getGlobalEnv();    if (map != globalMap) {      for (Map.Entry<String,Var> entry : globalMap.entrySet()) {	result.append(env.createString(entry.getKey()),		      entry.getValue().toValue());      }    }    return result;  }  /*   * Returns the type of this resource.   */  @ReturnNullAsFalse  public static String get_resource_type(Env env, Value v)  {    return v.getResourceType();  }  /**   * Returns the type string for the variable   */  public static String gettype(@ReadOnly Value v)  {    return v.getType();  }  /**   * Imports request variables   *   * @param types the variables to import   * @param prefix the prefix   */  public static boolean import_request_variables(Env env,						 String types,						 @Optional String prefix)  {    if ("".equals(prefix))      env.notice(L.l("import_request_variables should use a prefix argument"));    for (int i = 0; i < types.length(); i++) {      char ch = types.charAt(i);      Value value = null;      if (ch == 'c' || ch == 'C')	value = env.getGlobalValue("_COOKIE");      else if (ch == 'g' || ch == 'G')	value = env.getGlobalValue("_GET");      else if (ch == 'p' || ch == 'P')	value = env.getGlobalValue("_POST");      if (! (value instanceof ArrayValue))	continue;      ArrayValue array = (ArrayValue) value;      for (Map.Entry<Value,Value> entry : array.entrySet()) {	String key = entry.getKey().toString();	env.setGlobalValue(prefix + key,			 array.getRef(entry.getKey()));      }    }    return true;  }  /**   * Converts to a long   *   * @param v the variable to convert   * @return the double value   */  public static long intval(@ReadOnly Value v)  {    return v.toLong();  }  /**   * Returns true for an array.   *   * @param v the value to test   *   * @return true for an array   */  public static boolean is_array(@ReadOnly Value v)  {    return v.isArray();  }  // XXX: is_binary  /**   * Returns true for a boolean   *   * @param v the value to test   *   * @return true for a boolean   */  public static Value is_bool(@ReadOnly Value v)  {    return (v.toValue() instanceof BooleanValue	    ? BooleanValue.TRUE	    : BooleanValue.FALSE);  }  // XXX: is_buffer  /**   * Returns the type string for the variable   */  public static boolean is_callable(Env env,				    Value v,				    @Optional boolean isSyntaxOnly,				    @Optional @Reference Value nameRef)  {    if (v instanceof StringValue) {      if (nameRef != null)	nameRef.set(v);      if (isSyntaxOnly)	return true;      else	return env.findFunction(v.toString()) != null;    }    else if (v instanceof ArrayValue) {      Value obj = v.get(LongValue.ZERO);      Value name = v.get(LongValue.ONE);      if (! (name instanceof StringValue))	return false;      if (nameRef != null)	nameRef.set(name);      if (obj instanceof StringValue) {	if (isSyntaxOnly)	  return true;	QuercusClass cl = env.findClass(obj.toString());	if (cl == null)	  return false;	return (cl.findFunction(name.toString()) != null);      }      else if (obj.isObject()) {	if (isSyntaxOnly)	  return true;	return obj.findFunction(name.toString()) != null;      }      else	return false;    }    else      return false;  }  /**   * Returns true for a double   *   * @param v the value to test   *   * @return true for a double   */  public static boolean is_double(@ReadOnly Value v)  {    return is_float(v);  }  /**   * Returns true for a double   *   * @param v the value to test   *   * @return true for a double   */  public static boolean is_float(@ReadOnly Value v)  {    return (v.toValue() instanceof DoubleValue);  }  /**   * Returns true for an integer   *   * @param v the value to test   *   * @return true for a double   */  public static Value is_int(@ReadOnly Value v)  {    return (v.toValue() instanceof LongValue	    ? BooleanValue.TRUE	    : BooleanValue.FALSE);  }  /**   * Returns true for an integer   *   * @param v the value to test   *   * @return true for a double   */  public static Value is_integer(@ReadOnly Value v)  {    return is_int(v);  }  /**   * Returns true for an integer   *   * @param v the value to test   *   * @return true for a double   */  public static Value is_long(@ReadOnly Value v)  {    return is_int(v);  }  /**   * Returns true for null   *   * @param v the value to test   *   * @return true for null   */  public static boolean is_null(@ReadOnly Value v)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?