root / phpframework / trunk / myFusebox.php

Revision 751, 17.1 kB (checked in by starkraving2002, 3 months ago)

reverted to previous version: rev 730 shouldn't have been made

  • Property svn:executable set to *
Line 
1<?php
2/*
3Fusebox Software License
4Version 1.0
5
6Copyright (c) 2003, 2004, 2005, 2006 The Fusebox Corporation. All rights reserved.
7
8Redistribution && use in source && binary forms, with or without modification, are permitted
9provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this list of conditions
12   && the following disclaimer.
13
142. Redistributions in binary form or otherwise encrypted form must reproduce the above copyright
15   notice, this list of conditions && the following disclaimer in the documentation and/or other
16   materials provided with the distribution.
17
183. The end-user documentation included with the redistribution, if any, must include the following
19   acknowledgment:
20
21   "This product includes software developed by the Fusebox Corporation (http://www.fusebox.org/)."
22
23   Alternately, this acknowledgment may appear in the software itself, if && wherever such
24   third-party acknowledgments normally appear.
25
264. The names "Fusebox" && "Fusebox Corporation" must not be used to endorse or promote products
27   derived from this software without prior written (non-electronic) permission. For written
28   permission, please contact fusebox@fusebox.org.
29
305. Products derived from this software may not be called "Fusebox", nor may "Fusebox" appear in
31   their name, without prior written (non-electronic) permission of the Fusebox Corporation. For
32   written permission, please contact fusebox@fusebox.org.
33
34If one or more of the above conditions are violated, then this license is immediately revoked &&
35can be re-instated only upon prior written authorization of the Fusebox Corporation.
36
37THIS SOFTWARE IS PROVIDED "AS IS" && ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY && FITNESS FOR A PARTICULAR PURPOSE ARE
39DISCLAIMED. IN NO EVENT SHALL THE FUSEBOX CORPORATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY
40DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42BUSINESS INTERRUPTION) HOWEVER CAUSED && ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46-------------------------------------------------------------------------------
47
48This software consists of voluntary contributions made by many individuals on behalf of the
49Fusebox Corporation. For more information on Fusebox, please see <http://www.fusebox.org/>.
50
51*/
52class MyFusebox { /*I provide the per-request myFusebox data structure && some convenience methods.*/
53    var $version;
54    var $thisCircuit;
55    var $thisFuseaction;
56    var $thisPlugin;
57    var $thisPhase;
58    var $plugins;
59    var $parameters;
60    var $stack;
61   
62    var $created;
63    var $log;
64    var $occurence;
65    var $frame;
66   
67   
68    function MyFusebox ( $appKey, $attributes ) {
69        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
70       
71        $this->thisCircuit = "";
72        $this->thisFuseaction "";
73        $this->thisPlugin = "";
74        $this->thisPhase = "";
75       
76        $this->version = array();
77        $this->version['runtime']     = "unknown";
78        $this->version['loader']      = "unknown";
79        $this->version['transformer'] = "unknown";
80        $this->version['parser']      = "unknown";
81       
82        $this->version['runtime']     = "5.0.0.GR.0";
83       
84        // the basic default is development-full-load mode:
85        $this->parameters = array();
86        $this->parameters['load'] = true;
87        $this->parameters['parse'] = true;
88        $this->parameters['execute'] = true;
89        // FB5: new execution parameters:
90        $this->parameters['clean'] = false;         // don't delete parsed files by default
91        $this->parameters['parseall'] = false;    // don't compile all fuseactions by default
92         
93        $this->parameters['userProvidedLoadParameter'] = false;
94        $this->parameters['userProvidedCleanParameter'] = false;
95        $this->parameters['userProvidedParseParameter'] = false;
96        $this->parameters['userProvidedParseAllParameter'] = false;
97        $this->parameters['userProvidedExecuteParameter'] = false;
98       
99        $this->plugins = array();
100       
101        // stack frame for do/include parameters:
102        $this->stack = array();
103   
104       
105        $this->appKey = $appKey;
106        $theFusebox = array();
107       
108        list($usec, $sec) = explode(" ", microtime());
109        $this->created = ((float)$usec + (float)$sec);
110        //$this->created = substr($created,-strpos($created,' '));
111        $this->log = array();
112        $this->occurrence = array();
113
114        // we can't guarantee the fusebox exists in application scope yet...
115        if ( isset($GLOBALS['application']) && is_array($GLOBALS['application']) && array_key_exists($this->appKey,$GLOBALS['application']) ) {
116            $theFusebox =& $GLOBALS['application'][$this->appKey];
117        }
118        // default myFusebox.parameters depending on "mode" of the application set in fusebox.xml
119        if ( is_object($theFusebox) && array_key_exists("mode",get_object_vars($theFusebox)) ) {
120            switch ( $theFusebox->mode ) {
121            // FB41 backward compatibility - now deprecated
122            case "development" :
123                if ( array_key_exists('strictMode',get_object_vars($theFusebox)) && $theFusebox->strictMode ) {
124                    // since we don't load fusebox.xml if we throw an exception, we must fixup the value for the next run
125                    $theFusebox->mode = "development-full-load";
126                    __cfthrow(array( 'type'=>"fusebox.badGrammar.deprecated",
127                            'message'=>"Deprecated feature",
128                            'detail'=>"'development' is a deprecated execution mode - use 'development-full-load' instead."));
129                }
130                $this->parameters['load'] = true;
131                $this->parameters['parse'] = true;
132                $this->parameters['execute'] = true;
133                break;
134            // FB5: replacement for old development mode
135            case "development-full-load" :
136                $this->parameters['load'] = true;
137                $this->parameters['parse'] = true;
138                $this->parameters['execute'] = true;
139                break;
140            // FB5: new option - does not load fusebox.xml && therefore does not (re-)load fuseboxApplication object
141            case "development-circuit-load" :
142                $this->parameters['load'] = false;
143                $this->parameters['parse'] = true;
144                $this->parameters['execute'] = true;
145                break;
146            case "production" :
147                $this->parameters['load'] = false;
148                $this->parameters['parse'] = false;
149                $this->parameters['execute'] = true;
150                break;
151            default:
152                // since we don't load fusebox.xml if we throw an exception, we must fixup the value for the next run
153                $theFusebox->mode = "development-full-load";
154                __cfthrow(array( 'type'=>"fusebox.badGrammar.invalidParameterValue",
155                        'message'=>"Parameter has invalid value",
156                        'detail'=>"The parameter 'mode' must be one of 'development-full-load', 'development-circuit-load' or 'production' in the fusebox.xml file."));
157                break;
158            }
159        }
160        // did the user pass in any special "fuseboxDOT" parameters for this request?
161        // If so, process them
162        // note: only if attributes.fusebox.password matches the application password
163        if ( !isset($attributes["fusebox.password"]) ) { $attributes["fusebox.password"] = ""; }
164        if ( is_object($theFusebox) && array_key_exists('password',get_object_vars($theFusebox)) && $theFusebox->password == $attributes['fusebox.password'] ) {
165            // FB5: does a load and wipes the parsed files out
166            if ( array_key_exists('fusebox.loadclean',$attributes) && in_array($attributes['fusebox.loadclean'],array('true','false')) ) {
167                $this->parameters['load'] = ( $attributes['fusebox.loadclean'] == 'true' );
168                $this->parameters['clean'] = ( $attributes['fusebox.loadclean'] == 'true' );
169                $this->parameters['userProvidedLoadParameter'] = true;
170                $this->parameters['userProvidedCleanParameter'] = true;
171            }
172            if ( array_key_exists('fusebox.load',$attributes) && in_array($attributes['fusebox.load'],array('true','false')) ) {
173                $this->parameters['load'] = ( $attributes['fusebox.load'] == 'true' );
174                $this->parameters['userProvidedLoadParameter'] = true;
175            }
176            if ( array_key_exists('fusebox.parseall',$attributes) && in_array($attributes['fusebox.parseall'],array('true','false')) ) {
177                $this->parameters['parse'] = ( $attributes['fusebox.parseall'] == 'true' );
178                $this->parameters['parseall'] = ( $attributes['fusebox.parseall'] == 'true' );
179                if ( $this->parameters['parseall'] ) {
180                    $this->parameters['load'] = true;
181                }
182                $this->parameters['userProvidedLoadParameter'] = true;
183                $this->parameters['userProvidedParseParameter'] = true;
184                $this->parameters['userProvidedParseAllParameter'] = true;
185            }
186            if ( array_key_exists('fusebox.parse',$attributes) && in_array($attributes['fusebox.parse'],array('true','false')) ) {
187                $this->parameters['parse'] = ( $attributes['fusebox.parse'] == 'true' );
188                $this->parameters['userProvidedParseParameter'] = true;
189            }
190            if ( array_key_exists('fusebox.execute',$attributes) && in_array($attributes['fusebox.execute'],array('true','false')) ) {
191                $this->parameters['execute'] = ( $attributes['fusebox.execute'] == 'true' );
192                $this->parameters['userProvidedExecuteParameter'] = true;
193            }
194        }
195       
196        /*
197            force a load if the runtime and core versions differ: this allows a new
198            version to be dropped in and the framework will automatically reload!
199            note: that we must *force* a load, by pretending this is user-provided!
200        */
201        if ( is_object($theFusebox) && ( in_array('getversion',get_class_methods($theFusebox)) || in_array('getVersion',get_class_methods($theFusebox)) ) ) {
202            if ( $this->version['runtime'] != $theFusebox->getVersion() ) {
203                $this->parameters['userProvidedLoadParameter'] = true;
204                $this->parameters['load'] = true;
205            }
206        } else {
207            // hmm, doesn't look like the core is present (or it's not FB5 Alpha 2 or higher)
208            $this->parameters['userProvidedLoadParameter'] = true;
209            $this->parameters['load'] = true;
210        }
211
212        // if the fusebox doesn't already exist we definitely want to reload
213        if ( is_object($theFusebox) && array_key_exists("isFullyLoaded",get_object_vars($theFusebox)) && $theFusebox->isFullyLoaded ) {
214            // if fully loaded, leave the load parameter alone
215        } else {
216            $this->parameters['load'] = true;
217        }
218       
219        $this->theFusebox =& $theFusebox;
220        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
221        return $this;
222    }
223   
224    function &getApplication () { //I am a convenience method to return the fuseboxApplication object without needing to know reference application scope or the FUSEBOX_APPLICATION_KEY variable.
225        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
226   
227        /*
228            this is a bit of a hack since we're accessing application scope directly
229            but it's probably cleaner than exposing a method to allow fuseboxApplication
230            to inject itself back into myFusebox during compileRequest()...
231        */
232        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
233        return $GLOBALS['application'][$this->appKey];
234   
235    }
236   
237    function &getCurrentCircuit() { //I am a convenience method to return the current Fusebox circuit object.
238        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
239   
240        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
241        return $GLOBALS['application'][$this->appKey]->circuits[$this->thisCircuit];
242   
243    }
244   
245    function &getCurrentFuseaction() { //I am a convenience method to return the current fuseboxAction (fuseaction) object.
246        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
247   
248        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
249        return $GLOBALS['application'][$this->appKey]->circuits[$this->thisCircuit]->fuseactions[$this->thisFuseaction];
250   
251    }
252   
253    function enterStackFrame() { //I create a new stack frame (for scoped parameters to do/include).
254        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
255       
256        $frame = array();
257       
258        $frame['__fuseboxStack'] = $this->stack;
259        $this->stack = $frame;
260       
261        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
262    }
263   
264    function leaveStackFrame() { //I pop the last stack frame (for scoped parameters to do/include).
265        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
266       
267        $this->stack = $this->stack['__fuseboxStack'];
268       
269        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
270    }
271   
272    function trace ( $type, $message, $theTime = 0 ) { //I add a line to the execution trace log.
273        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
274       
275        list($usec, $sec) = explode(" ", microtime());
276        $theTime = ( $theTime > 0 ) ? $theTime : ((float)$usec + (float)$sec);
277        $this->addTrace(($theTime - $this->created) * 1000,$type,$message);
278       
279        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
280    }
281
282    function addTrace( //I add a detailed line to the execution trace log.
283            $time, //I am the time taken to get to this point in the request.
284            $type, //I am the type of trace.
285            $message, //I am the trace message.
286            $occurrence = 0 //I am a placeholder for part of the struct that is added to the log.
287        ) {
288        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
289        global $log;
290        if ( array_key_exists($message,$this->occurrence) ) {
291            $this->occurrence[$message]++;
292        } else {
293            $this->occurrence[$message] = 1;
294        }
295        $occurrence = $this->occurrence[$message];
296        $log[] = array('time'=>$time,'type'=>$type,'message'=>$message,'occurrence'=>$occurrence);
297        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
298    }
299   
300    function renderTrace() { //I render the trace log as HTML.
301        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
302       
303        global $log;
304        $result = "";
305        $i = 0;
306       
307        $result = '
308            <br />
309            <div style="clear:both;padding-top:10px;border-bottom:1px Solid #CCC;font-family:verdana;font-size:16px;font-weight:bold">Fusebox debugging:</div>
310            <br />
311            <table cellpadding="2" cellspacing="0" width="100%" style="border:1px Solid #CCC;font-family:verdana;font-size:11pt;">
312                <tr style="background:#EAEAEA">
313                    <td style="border-bottom:1px Solid #CCC;font-family:verdana;font-size:11pt;"><strong>Time</strong></td>
314                    <td style="border-bottom:1px Solid #CCC;font-family:verdana;font-size:11pt;"><strong>Category</strong></td>
315                    <td style="border-bottom:1px Solid #CCC;font-family:verdana;font-size:11pt;"><strong>Message</strong></td>
316                    <td style="border-bottom:1px Solid #CCC;font-family:verdana;font-size:11pt;"><strong>Count</strong></td>
317                </tr>
318                '; for ( $i = 0 ; $i < count($log) ; $i++ ) {
319                    $result .= ( $i % 2 > 0 ) ? '<tr style="background:#F9F9F9">' : '<tr style="background:#FFFFFF">'; $result .= '
320                        <td valign="top" style="font-size:10pt;border-bottom:1px Solid #CCC;font-family:verdana;">'.round($log[$i]['time']).'ms</td>
321                        <td valign="top" style="font-size:10pt;border-bottom:1px Solid #CCC;font-family:verdana;">'.$log[$i]['type'].'</td>
322                        <td valign="top" style="font-size:10pt;border-bottom:1px Solid #CCC;font-family:verdana;">'.$log[$i]['message'].'</td>
323                        <td valign="top" align="center" style="font-size:10pt;border-bottom:1px Solid #CCC;font-family:verdana;">'.$log[$i]['occurrence'].'</td>
324                    </tr>';
325                } $result .= '
326            </table>';
327       
328        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
329        return $result;
330       
331    }
332
333}
334?>
Note: See TracBrowser for help on using the browser.