001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Sam
028: */
029:
030: package com.caucho.quercus.lib.spl;
031:
032: import java.util.ArrayList;
033: import java.util.LinkedHashMap;
034: import java.util.Map;
035:
036: import com.caucho.quercus.annotation.Optional;
037: import com.caucho.quercus.env.ArrayValue;
038: import com.caucho.quercus.env.ArrayValueImpl;
039: import com.caucho.quercus.env.BooleanValue;
040: import com.caucho.quercus.env.Env;
041: import com.caucho.quercus.env.QuercusClass;
042: import com.caucho.quercus.env.Value;
043: import com.caucho.quercus.module.AbstractQuercusModule;
044: import com.caucho.quercus.program.AbstractFunction;
045: import com.caucho.util.CharBuffer;
046:
047: /*
048: * XXX: Not finished.
049: */
050: public class SplModule extends AbstractQuercusModule {
051: private static String DEFAULT_EXTENSIONS = ".php,.inc";
052:
053: public String[] getLoadedExtensions() {
054: return new String[] { "SPL" };
055: }
056:
057: public static boolean spl_autoload_register(Env env, @Optional
058: String fun) {
059: if (fun == null || fun.length() == 0)
060: fun = "spl_autoload";
061:
062: env.addAutoloadFunction(fun);
063:
064: return true;
065: }
066:
067: public static boolean spl_autoload_unregister(Env env, String fun) {
068: env.removeAutoloadFunction(fun);
069:
070: return true;
071: }
072:
073: public static Value spl_autoload_functions(Env env) {
074: LinkedHashMap<String, AbstractFunction> funMap = env
075: .getAutoloadFunctions();
076:
077: if (funMap == null)
078: return BooleanValue.FALSE;
079:
080: ArrayValue array = new ArrayValueImpl();
081:
082: for (Map.Entry<String, AbstractFunction> entry : funMap
083: .entrySet()) {
084: array.put(entry.getKey());
085: }
086:
087: return array;
088: }
089:
090: public static String spl_autoload_extensions(Env env, @Optional
091: String extensions) {
092: String oldExtensions = getAutoloadExtensions(env);
093:
094: if (extensions != null)
095: env.setSpecialValue("caucho.spl_autoload", extensions);
096:
097: return oldExtensions;
098: }
099:
100: private static String getAutoloadExtensions(Env env) {
101: Object obj = env.getSpecialValue("caucho.spl_autoload");
102:
103: if (obj == null)
104: return DEFAULT_EXTENSIONS;
105: else
106: return (String) obj;
107: }
108:
109: public static void spl_autoload(Env env, String className,
110: @Optional
111: String extensions) {
112: if (env.findClass(className, false) != null)
113: return;
114:
115: ArrayList<String> extensionList = new ArrayList<String>();
116:
117: if (extensions == null) {
118: extensions = getAutoloadExtensions(env);
119: }
120:
121: if (extensions == DEFAULT_EXTENSIONS) {
122: extensionList.add(".php");
123: extensionList.add(".inc");
124: } else {
125: int len = extensions.length();
126:
127: CharBuffer cb = CharBuffer.allocate();
128: for (int i = 0; i < len; i++) {
129: char ch = extensions.charAt(i);
130:
131: if (ch == ',')
132: extensionList.add(cb.toString());
133: cb.clear();
134: }
135:
136: if (cb.length() > 0)
137: extensionList.add(cb.toString());
138:
139: cb.free();
140: }
141:
142: String filePrefix = className.toLowerCase();
143:
144: for (String ext : extensionList) {
145: String filename = filePrefix + ext;
146:
147: env.include(filename);
148:
149: QuercusClass cls = env.findClass(className, false);
150:
151: if (cls != null)
152: return;
153: }
154: }
155: }
|