root / phpframework / trunk / fuseboxPlugin.php

Revision 635, 9.8 kB (checked in by starkraving2002, 1 year ago)
  • 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 and use in source and 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   and the following disclaimer.
13
142. Redistributions in binary form or otherwise encrypted form must reproduce the above copyright
15   notice, this list of conditions and 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 and wherever such
24   third-party acknowledgments normally appear.
25
264. The names "Fusebox" and "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 and
35can be re-instated only upon prior written authorization of the Fusebox Corporation.
36
37THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 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 AND 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 FuseboxPlugin { //I represent a plugin declaration.
53
54    var $name;
55    var $fuseboxApplication;
56    var $phase;
57    var $path;
58    var $template;
59    var $rootPath;
60    var $parameters;
61    var $paramVerbs;
62   
63    function FuseboxPlugin /*I am the constructor.*/ (
64            $phase, //I am the phase with which this plugin is associated.
65            $pluginXML, //I am the XML representation of this plugin's declaration.
66            &$fbApp //I am the fusebox application object.
67        ) {
68        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
69   
70        $i = 0;
71        $n = count($pluginXML['xmlChildren']);
72        $attr = 0;
73        $nAttrs = 2;
74        $verbChildren = array();
75        $factory =& $fbApp->getFuseactionFactory();
76        $ext = "." . $fbApp->scriptFileDelimiter;
77       
78        if ( !array_key_exists("name",$pluginXML['xmlAttributes']) ) {
79            __cfthrow(array( 'type'=>"fusebox.badGrammar.requiredAttributeMissing",
80                'message'=>"Required attribute is missing",
81                'detail'=>"The attribute 'name' is required, for a '$phase' plugin declaration in fusebox.xml."
82            ));
83        }
84       
85        $this->name = $pluginXML['xmlAttributes']['name'];
86        $this->fuseboxApplication =& $fbApp;
87
88        if ( !array_key_exists("template",$pluginXML['xmlAttributes']) ) {
89            __cfthrow(array( 'type'=>"fusebox.badGrammar.requiredAttributeMissing",
90                'message'=>"Required attribute is missing",
91                'detail'=>"The attribute 'template' is required, for the '".$this->getName()."' plugin declaration in fusebox.xml."
92            ));
93        }
94
95        $this->phase = $phase;
96        if ( $pluginXML['xmlName'] == "plugin" ) {
97            $this->path = $fbApp->getPluginsPath();
98            if ( array_key_exists("path",$pluginXML['xmlAttributes']) ) {
99                $this->path .= str_replace("\\","/",$pluginXML['xmlAttributes']['path']);
100                $nAttrs = 3;
101            }
102            if ( substr($this->path,-1) != "/" ) {
103                $this->path .= "/";
104            }
105            $this->template = $pluginXML['xmlAttributes']['template'];
106            if ( strlen($this->template) > 4 || substr($this->template,-4) != $ext ) {
107                $this->template .= $ext;
108            }
109            $this->rootpath =
110                    $fbApp->relativePath($fbApp->getApplicationRoot() .
111                                                    $this->path,$fbApp->getApplicationRoot());
112            /* remove pairs of directory/../ to form canonical path: */
113            while ( strpos($this->rootpath,'/../') !== false ) {
114                $this->rootpath = ereg_replace("[^\\.:/]*/\\.\\./","",$this->rootpath);
115            }
116            if ( $fbApp->strictMode && count($pluginXML['xmlAttributes']) != $nAttrs ) {
117                __cfthrow(array( 'type'=>"fusebox.badGrammar.unexpectedAttributes",
118                    'message'=>"Unexpected attributes",
119                    'detail'=>"Unexpected attributes were found in the '".$this->getName()."' plugin declaration in fusebox.xml."
120                ));
121            }
122            $this->parameters = $pluginXML['xmlChildren'];
123            $this->paramVerbs = array();
124            for ( $i = ; $i < $n ; $i++ ) {
125                if ( !array_key_exists("name",$this->parameters[$i]['xmlAttributes']) ) {
126                    __cfthrow(array( 'type'=>"fusebox.badGrammar.requiredAttributeMissing",
127                        'message'=>"Required attribute is missing",
128                        'detail'=>"The attribute 'name' is required, for a 'parameter' to the '".$this->getName()."' plugin declaration in fusebox.xml."
129                    ));
130                }
131                if ( !array_key_exists("value",$this->parameters[$i]['xmlAttributes']) ) {
132                    __cfthrow(array( 'type'=>"fusebox.badGrammar.requiredAttributeMissing",
133                        'message'=>"Required attribute is missing",
134                        'detail'=>"The attribute 'value' is required, for a 'parameter' to the '".$this->getName()."' plugin declaration in fusebox.xml."
135                    ));
136                }
137                if ( $fbApp->strictMode && count($this->parameters[$i].xmlAttributes) != 2 ) {
138                    __cfthrow(array( 'type'=>"fusebox.badGrammar.unexpectedAttributes",
139                        'message'=>"Unexpected attributes",
140                        'detail'=>"Unexpected attributes were found in the '".$this->parameters[$i]['xmlAttributes']['name']."' parameter of the '$this->getName()' plugin declaration in fusebox.xml."
141                    ));
142                }
143                $attr = array();
144                $attr['name'] = 'myFusebox->plugins["'.$this->getName().'"]["parameters"]["'.$this->parameters[$i]['xmlAttributes']['name'].'"]';
145                $attr['value'] = $this->parameters[$i]['xmlAttributes']['value'];
146                $this->paramVerbs[$i] = $factory->create("set",$this,$attr,$verbChildren);
147            }
148        } else {
149            __cfthrow(array( 'type'=>"fusebox.badGrammar.illegalDeclaration",
150                'message'=>"Illegal declaration",
151                'detail'=>"The XML entity '{$pluginXML['xmlName']}' was found where a plugin declaration was expected in fusebox.xml."
152            ));
153        }
154   
155        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
156        return $this;
157       
158    }
159   
160    function compile /*I compile this plugin object.*/ (
161            &$writer //I am the parsed file writer object. I am required but it's faster to specify that I am not required.
162        ) {
163       
164        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
165        $i = 0;
166        $n = count($this->paramVerbs);
167        $file = "";
168        $p = "";
169       
170        if ( $_REQUEST['__fusebox']['SuppressPlugins'] ) {
171            return;
172        }
173        switch ( $this->phase ) {
174            case 'processError' :
175            case 'fuseactionException' :
176                $fplFilename = $this->fuseboxApplication->getApplicationRoot().$this->path.$this->template;
177                $fpl = fopen($fplFilename,'r');
178                $file = fread($fpl,filesize($fplFilename));
179                fclose($fpl);
180                $writer->rawPrintln($file);
181                break;
182            default :
183                for ( $i = ; $i < $n ; $i++ ) {
184                    $this->paramVerbs[$i]->compile($writer);
185                }
186                $p = $writer->setPhase($this->phase);
187                $writer->println('$myFusebox->thisPlugin = "'.$this->getName().'";');
188                $writer->_print('include(');
189                $writer->_print('$application[$FUSEBOX_APPLICATION_KEY]->WebRootPathToappRoot."'.$this->path.$this->template);
190                $writer->println('");');
191                $writer->setPhase($p);
192                break;
193        }
194        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
195
196    }
197   
198    function getName() { //I return the name of the plugin.
199       
200        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
201        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
202        return $this->name;
203       
204    }
205
206    function &getCircuit() { //I return the enclosing application object. This is an edge case to allow code that works with fuseactions to work with plugins too
207   
208        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '<ul><li>Starting $'.__CLASS__.'->'.__FUNCTION__.'()';
209        if ( isset($GLOBALS['attributes']['fusebox.debug']) && $GLOBALS['attributes']['fusebox.debug'] == 'true' ) echo '</li><li>Ending $'.__CLASS__.'->'.__FUNCTION__.'()</li></ul>';
210        return $this->fuseboxApplication;
211   
212    }
213   
214}
215?>
Note: See TracBrowser for help on using the browser.