/** * The core tokenizing part - runs the tokenizer on the data, * and stores the results in $this->tokens[] * * @param string Data to tokenize * * @return none | PEAR::Error * @access public|private * @see see also methods..... */ function tokenize($data) { require_once 'Fly/Flexy/Tokenizer.php'; $tokenizer =& Fly_Flexy_Tokenizer::construct($data, $this->options); // initialize state - this trys to make sure that // you dont do to many elses etc. //echo "RUNNING TOKENIZER"; // step one just tokenize it. $i = 1; while ($t = $tokenizer->yylex()) { if ($t == FLY_FLEXY_TOKEN_ERROR) { return Fly_Flexy::raiseError(array("Fly_Flexy_Tree::Syntax error in File: %s (Line %s)\n" . "Tokenizer Error: %s\n" . "Context:\n\n%s\n\n >>>>>> %s\n", $this->options['filename'], $tokenizer->yyline, $tokenizer->error, htmlspecialchars(substr($tokenizer->yy_buffer, 0, $tokenizer->yy_buffer_end)), htmlspecialchars(substr($tokenizer->yy_buffer, $tokenizer->yy_buffer_end, 100))), FLY_FLEXY_ERROR_SYNTAX, FLY_FLEXY_ERROR_DIE); } if ($t == FLY_FLEXY_TOKEN_NONE) { continue; } if ($t->token == 'Php') { continue; } $i++; $this->tokens[$i] = $tokenizer->value; $this->tokens[$i]->id = $i; //print_r($_FLY_FLEXY_TOKEN['tokens'][$i]); } //echo "BUILT TOKENS"; }
/** * Output Open Tag and any children and not Child tag (designed for use with <form + hidden elements> * * @access public * @param object $overlay = merge data from object. * @return string * @abstract */ function toHtmlnoClose($overlay = false) { $ret = $this; if ($ret->override !== false) { return $ret->override; } if ($overlay !== false) { $ret = Fly_Flexy::mergeElement($this, $overlay); } return "<{$ret->tag}" . $ret->attributesToHTML() . '>' . $ret->childrenToHTML(); }
/** * Fly_Flexy_Token_Tag toString * * @param object Fly_Flexy_Token_Tag * * @return string string to build a template * @access public * @see toString* */ function toStringTag($element) { $original = $element->getAttribute('ALT'); // techncially only input type=(submit|button|input) alt=.. applies, but we may // as well translate any occurance... if (($element->tag == 'IMG' || $element->tag == 'INPUT') && is_string($original) && strlen($original)) { $this->addStringToGettext($original); $quote = $element->ucAttributes['ALT'][0]; $element->ucAttributes['ALT'] = $quote . $this->translateString($original) . $quote; } $original = $element->getAttribute('TITLE'); if (is_string($original) && strlen($original)) { $this->addStringToGettext($original); $quote = $element->ucAttributes['TITLE'][0]; $element->ucAttributes['TITLE'] = $quote . $this->translateString($original) . $quote; } if (strpos($element->tag, ':') === false) { $namespace = 'Tag'; } else { $bits = explode(':', $element->tag); $namespace = $bits[0]; } if ($namespace[0] == '/') { $namespace = substr($namespace, 1); } if (empty($this->tagHandlers[$namespace])) { require_once 'Fly/Flexy/Compiler/Flexy/Tag.php'; $handler = Fly_Flexy_Compiler_Flexy_Tag::factory($namespace, $this); $this->tagHandlers[$namespace] =& $handler; if (!$this->tagHandlers[$namespace]) { return Fly_Flexy::raiseError('Fly_Flexy::failed to create Namespace Handler ' . $namespace . ' in file ' . $GLOBALS['_FLY_FLEXY']['filename'], FLY_FLEXY_ERROR_SYNTAX, FLY_FLEXY_ERROR_RETURN); } } return $this->tagHandlers[$namespace]->toString($element); }
/** * output the default template with the editing facilities. * * @return none * @access public */ function outputDefaultTemplate() { $o = array('compileDir' => ini_get('session.save_path') . '/Fly_Flexy_Translate', 'templateDir' => dirname(__FILE__) . '/templates'); $x = new Fly_Flexy($o); $x->compile('translator.html'); $x->outputObject($this); }
/** * Load the plugins, and lookup which one provides the required method * * * @param string Name * * @return string|PEAR_Error the class that provides it. * @access private */ function _loadPlugins($name) { // name can be: // ahref = maps to {class_prefix}_ahref::ahref $this->plugins = array(); if (empty($this->plugins)) { foreach ($this->flexy->options['plugins'] as $cname => $file) { if (!is_int($cname)) { include_once $file; $this->plugins[$cname] = new $cname(); $this->plugins[$cname]->flexy =& $this->flexy; continue; } $cname = $file; require_once 'Fly/Flexy/Plugin/' . $cname . '.php'; $class = "Fly_Flexy_Plugin_{$cname}"; $this->plugins[$class] = new $class(); $this->plugins[$class]->flexy =& $this->flexy; } } foreach ($this->plugins as $class => $o) { //echo "checking :". get_class($o). ":: $name\n"; if (is_callable(array($o, $name), true)) { return $class; } } return Fly_Flexy::raiseError("could not find plugin with method: '{$name}'"); }
/** * output a template (optionally with flexy object & element.) * * @param string name of flexy template. * @param object object as per Fly_Flexy:outputObject * @param array elements array as per Fly_Flexy:outputObject * * @return none * @access public */ function display($templatename, $object = null, $elements = array()) { // some standard stuff available to a smarty template.. $this->vars['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME']; $o = PEAR::getStaticProperty('Fly_Flexy', 'options'); require_once 'Fly/Flexy.php'; $t = new Fly_Flexy(); $t->compile($templatename); $object = $object !== null ? $object : new StdClass(); foreach ($this->vars as $k => $v) { $object->{$k} = $v; } $t->outputObject($object, $elements); }
/** * * Assign a token by reference. This allows you change variable * values within the template and have those changes reflected back * at the calling logic script. Works as with form 2 of assign(). * * @access public * * @param string $name The template token-name for the reference. * * @param mixed &$ref The variable passed by-reference. * * @return bool|PEAR_Error Boolean true on success, or a PEAR_Error * on failure. * * @throws SAVANT_ERROR_ASSIGN_REF Unknown reason for error. * * @see assign() * * @see assignObject() * */ function assignRef($name, &$ref) { // look for the proper case: name and variable if (is_string($name) && isset($ref)) { if (isset($this->variables[$name])) { unset($this->variables[$name]); } // // assign the token as a reference $this->references[$name] =& $ref; // done! return true; } // final error catch return Fly_Flexy::raiseError("invalid type sent to assignRef, " . print_r($name, true), FLY_FLEXY_ASSIGN_ERROR_INVALIDARGS); }
/** * setErrors - sets the suffix of an element to a value.. * * HTML_Element_Factory::setErrors($elements,array('name','not long enough')); * * @depreciated - this is really outside the scope of Factory - it should be * seperated into a rendering toolkit of some kind. * @param array of HTML_Element's * @param array key(tag name) => error * @param string sprintf error format.. * * @return array Array of HTML_Elements * @access public */ function &setErrors(&$ret, $set, $format = '<span class="error">%s</span>') { if (empty($ret) || !is_array($ret)) { $ret = array(); } // check what you send this.. !!! if (!is_array($set)) { return Fly_Flexy::raiseError('invalid arguments "$set" (should be an array) sent to Fly_Flexy_Factory::setErrors', 0, FLY_FLEXY_ERROR_DIE); } foreach ($set as $k => $v) { if (!$v) { continue; } if (!isset($ret[$k])) { $ret[$k] = new Fly_Flexy_Element(); } $ret[$k]->suffix .= sprintf($format, $v); } return $ret; }
/** * Handler for User defined functions in templates.. * <flexy:function name="xxxxx">.... </flexy:block> // equivilant to function xxxxx() { * <flexy:function call="{xxxxx}">.... </flexy:block> // equivilant to function {$xxxxx}() { * <flexy:function call="xxxxx">.... </flexy:block> // equivilant to function {$xxxxx}() { * * This will not handle nested blocks initially!! (and may cause even more problems with * if /foreach stuff..!! * * @param object token to convert into a element. * @access public */ function functionToString($element) { if ($arg = $element->getAttribute('NAME')) { // this is a really kludgy way of doing this!!! // hopefully the new Template Package will have a sweeter method.. $GLOBALS['_FLY_FLEXY']['prefixOutput'] .= $this->compiler->appendPHP("\nfunction _fly_flexy_compiler_flexy_flexy_{$arg}(\$t,\$_fly_flexy_this) {\n") . preg_replace('/\\$this-/', '$_fly_flexy_this-', $element->compileChildren($this->compiler)) . $this->compiler->appendPHP("\n}\n"); return ''; } if (!isset($element->ucAttributes['CALL'])) { return Fly_Flexy::raiseError(' tag flexy:function needs an argument call or name' . " Error on Line {$element->line} <{$element->tag}>", null, FLY_FLEXY_ERROR_DIE); } // call is a stirng : nice and simple.. if (is_string($element->ucAttributes['CALL'])) { $arg = $element->getAttribute('CALL'); return $this->compiler->appendPHP("if (function_exists('_fly_flexy_compiler_flexy_flexy_{$arg}')) " . " _fly_flexy_compiler_flexy_flexy_{$arg}(\$t,\$this);"); } // we make a big assumption here.. - it should really be error checked.. // that the {xxx} element is item 1 in the list... $e = $element->ucAttributes['CALL'][1]; $add = $e->toVar($e->value); if ($add instanceof PEAR_Error) { return $add; } return $this->compiler->appendPHP("if (function_exists('_fly_flexy_compiler_flexy_flexy_'.{$add})) " . "call_user_func_array('_fly_flexy_compiler_flexy_flexy_'.{$add},array(\$t,\$this));"); }
/** * do the stack lookup on the variable * this relates to flexy * t relates to the object being parsed. * * @return string PHP variable * @access public */ function findVar($string) { global $_FLY_FLEXY_TOKEN; if (!$string || $string == 't') { return '$t'; } if ($string == 'this') { return '$this'; } // accept global access on some string if (@$GLOBALS['_FLY_FLEXY']['currentOptions']['globals'] && preg_match('/^(_POST|_GET|_REQUEST|_SESSION|_COOKIE|GLOBALS)\\[/', $string)) { return '$' . $string; } if (!@$GLOBALS['_FLY_FLEXY']['currentOptions']['privates'] && $string[0] == '_') { return Fly_Flexy::raiseError('Fly_Flexy::Attempt to access private variable:' . " on line {$this->line} of {$GLOBALS['_FLY_FLEXY']['filename']}" . ", Use options[privates] to allow this.", FLY_FLEXY_ERROR_SYNTAX, FLY_FLEXY_ERROR_RETURN); } $lookup = $string; if ($p = strpos($string, '[')) { $lookup = substr($string, 0, $p); } for ($s = $_FLY_FLEXY_TOKEN['state']; $s > 0; $s--) { if (in_array($lookup, $_FLY_FLEXY_TOKEN['statevars'][$s])) { return '$' . $string; } } return '$t->' . $string; }
/** * calls Fly_Flexy::raiseError() with the current file, line and tag * @param string Message to display * @param type (see Fly_Flexy::raiseError()) * @param boolean isFatal. * * @access private */ function _raiseErrorWithPositionAndTag($message, $type = null, $fatal = FLY_FLEXY_ERROR_RETURN) { global $_FLY_FLEXY; $message = "Error:{$_FLY_FLEXY['filename']} on Line {$this->element->line}" . " in Tag <{$this->element->tag}>:<BR>\n" . $message; return Fly_Flexy::raiseError($message, $type, $fatal); }
/** * Lazy loading of PEAR, and the error handler.. * This should load Fly_Flexy_Error really.. * * @param string message * @param int error type. * @param int an equivalant to pear error return|die etc. * * @return object pear error. * @access public */ function raiseError($message, $type = null, $fatal = FLY_FLEXY_ERROR_RETURN) { Fly_Flexy::debug("<B>Fly_Flexy::raiseError</B>{$message}"); require_once 'PEAR.php'; if ($this instanceof Fly_Flexy && $fatal == FLY_FLEXY_ERROR_DIE) { // rewrite DIE! return PEAR::raiseError($message, $type, $this->options['fatalError']); } if (isset($GLOBALS['_FLY_FLEXY']['fatalError']) && $fatal == FLY_FLEXY_ERROR_DIE) { return PEAR::raiseError($message, $type, $GLOBALS['_FLY_FLEXY']['fatalError']); } return PEAR::raiseError($message, $type, $fatal); }