Documentation is available at Crafty_Block.class.php
- <?PHP
- /**
- * Implementation file of the Crafty_Block class.
- *
- * <pre>
- * PROJECT : Crafty
- * Template Engine.
- * AUTHOR : Crafty Team <crafty@zulan.net>
- * COPYRIGHT : (c) Thomas Ilsche, 2004
- *
- * FILE : [ROOT]\Crafty_Block.class.php
- * DESCRIPTION: Implementation file of the Crafty_Block class.
- * </pre>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library 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. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * @package Crafty_Compiler
- * @author Crafty Team <crafty@zulan.net>
- * @copyright Copyright (c) Thomas Ilsche 2005
- * @version 1.0.4.2005.02.18
- * @link http://crafty.zulan.net
- * @license http://www.gnu.org/licenses/lgpl.html
- * GNU Lesser General Public License Version 2.1
- ***/
- /**
- * Crafty_Block class implementation
- *
- * Any crafty block will get it's own Crafty_Block instance with individual
- * {@link $content}.<br/>
- * Each time the block is used static it will be cloned at compile time
- * from {@link Crafty_Compiler::$blocks} and compiled itsself.
- *
- * @package Crafty_Compiler
- * @author Crafty Team <crafty@zulan.net>
- * @copyright Copyright (c) Thomas Ilsche 2005
- * @version 1.0.4.2005.02.18
- * @link http://crafty.zulan.net
- * @license http://www.gnu.org/licenses/lgpl.html
- * GNU Lesser General Public License Version 2.1
- ***/
- class Crafty_Block
- {
- /**
- * Stores all data the block needs to compile
- *
- * This is a list of object handles of Crafty_Core_* objects and
- * corresponding datasets or other Crafty_Block instances.<br/>
- * Since this will be compiled recursively by {@link compileElement()}
- * it can be a complex "tree" of arrays.
- *
- * @var array
- ***/
- public $content = array();
- /**
- * Object handle of the {@link Crafty_Compiler} for this block.
- *
- * @var Crafty_Compiler
- ***/
- public $compiler;
- /**
- * Stores the the name of the block or it's linearized adresses
- *
- * The original parsed blocks will have it's name in here and the cloned ones
- * for static compilation will have it's linearized adresses. For variable
- * blocks that are compiled into separate files the blocks of
- * {@link Crafty_Compiler::$blocks} will get a variable name/adress using $p.
- *
- * @var string
- ***/
- public $name;
- /**
- * Filename where the block is defined
- *
- * @var string
- ***/
- public $filename = '';
- /**
- * Line number of the block declaration in the definition file
- *
- * @var int
- ***/
- public $line_decl = -1;
- /**
- * Line number of the content begining in the definition file
- *
- * @var int
- ***/
- public $line_cont = -1;
- /**
- * The raw content of the block used for error tracing
- *
- * @var string
- ***/
- public $raw_content = '';
- /**
- * The path to the template file that generated this block
- *
- * Set by the constructor using {@link Crafty_Compiler::template_path}
- * that is reset by recursion
- *
- * @var string
- ***/
- public $template_path;
- /**
- * Defines if a blocks content is stored in an external file
- *
- * @var bool
- ***/
- public $is_external = FALSE;
- /**
- * Initializes a Crafty_Block instance
- *
- * The constructor intializes a Crafty_Block instance and sets the
- * {@link $name} aswell as the {@link $compiler}.
- *
- * @param string $name Name of the current block
- * @param Crafty_Compiler $compiler Crafty_Compiler object that created
- * this Block
- ***/
- public function __construct($name, crafty_compiler $compiler)
- {
- $this->name = $name;
- $this->compiler = $compiler;
- $this->template_path = $compiler->template_path;
- }
- /**
- * Compiles a block
- *
- * This method will compile a block {@link compileElement() compiling}
- * all elements} of the {@link $content}.
- **/
- public function compile()
- {
- ksort($this->content, SORT_NUMERIC);
- foreach ($this->content as $element) {
- $this->compileElement($element);
- }
- }
- /**
- * Recursively compile an Element
- *
- * In case the Element is a {@link Crafty_Block} object this method will
- * use its {@link compile()} method.<br/>
- * In case it is an array with a "core" index it writes the returned data of
- * {@link compileType()} in the binary template file.<br/>
- * In case it is a different array it will recursively handle the elements.<br/>
- * Otherwise it assumes that the Element a string or something castable as
- * string and writes it in the compiled templates. The last way however is
- * not currently used.
- *
- * @param mixed $element
- ***/
- protected function compileElement($element)
- {
- if ($element instanceof Crafty_Block) {
- $element->compile();
- } elseif (is_array($element)) {
- if (isset($element['core'])) {
- fputs($this->compiler->write_c_file_handle,
- $this->compileType($element));
- } else {
- ksort($element, SORT_NUMERIC);
- foreach ($element as $element_i) {
- $this->compileElement($element_i);
- }
- }
- } else {
- fputs($this->compiler->write_c_file_handle, $element);
- }
- }
- /**
- * Compiles a set of a core handler and a dataset
- *
- * This method makes use of the {@link Crafty_Core::compile()}.
- *
- * @param array $element Element to compile
- * @return string Binary data
- ***/
- protected function compileType($element)
- {
- try {
- $r = $element['core']->compile($element['data'], $this);
- } catch ( Crafty_Exception $e ) {
- $e->line = $element['line'];
- $e->file = $this->filename;
- throw $e;
- }
- return $r;
- }
- /**
- * Loads a raw blow of CraftyHTML
- *
- * First this method replaces the {@link Crafty_Compiler::$preparse_search}
- * preparse needles} with the {@link Crafty_Compiler::$preparse_replace}
- * preparse replacements}. Then it will try to match each {@link }
- * Crafty_Core::getPattern() core pattern}. On success it will hand over the
- * results to {@link Crafty_Core::pregCallback()}.<br/>
- * All parts of the CraftyHTML that are not matched by any core will be handed
- * to a {@link Crafty_Core_String} object.
- *
- * @param string $raw_content
- ***/
- public function load($raw_content)
- {
- /* We replace \\ , \' and \" for a better preg matching */
- $this->raw_content = $raw_content;
- $raw_content = str_replace($this->compiler->preparse_search,
- $this->compiler->preparse_replace,
- $raw_content);
- /* Strip the comments */
- $pattern = preg_quote($this->compiler->config->core->comment->left, '/')
- . '.*?'
- . preg_quote($this->compiler->config->core->comment->right, '/');
- $pattern = "/($pattern\\r?\\n?)/se";
- $replace = '$this->compiler->config->core->comment->left.'
- . 'preg_replace(\'/[^\r\n]/\',\'\',\'\\1\')'
- . '.$this->compiler->config->core->comment->right';
- $raw_content = preg_replace($pattern, $replace, $raw_content);
- $marker = array();
- foreach ($this->compiler->cores as $x => $core) {
- if ($core->getPattern()
- && (preg_match_all($core->getPattern(), $raw_content, $matches,
- PREG_SET_ORDER | PREG_OFFSET_CAPTURE)
- !== FALSE)) {
- foreach($matches as $match_num => $match) {
- $callback_handover = array();
- foreach($match as $sub) {
- $callback_handover[] = $sub[0];
- }
- try {
- $this->content[$match[0][1]] =
- $core->pregCallback($callback_handover);
- } catch (Crafty_Exception $e) {
- $e->file = $this->filename;
- $e->line = $this->compiler->line_number($raw_content,
- $match[0][1], $this->line_cont);
- throw $e;
- }
- $this->content[$match[0][1]]['line'] = $this->compiler->
- line_number($raw_content, $match[0][1], $this->line_cont);
- $marker[$match[0][1]] = strlen($match[0][0]);
- }
- }
- }
- /* Now it gets tricky. All noncaptured characters will be given to a
- core_string obj */
- if (!isset($this->compiler->cores['Crafty_Core_String'])
- || !($this->compiler->cores['Crafty_Core_String'] instanceof
- crafty_core_string)) {
- throw new Crafty_Exception('Crafty_Core_String object missing or bogus,
- check the "core/Crafty_Core_String.class.php"
- file.');
- }
- $obj = $this->compiler->cores['Crafty_Core_String'];
- $marker[strlen($raw_content)] = 0; /* This is finish.. but not the end */
- ksort($marker);
- $start = 0;
- foreach($marker as $offset => $length) {
- if ($start < $offset) {
- $string = substr($raw_content, $start, ($offset - $start));
- $this->content[$start] = array('core' => $obj,
- 'data' => array('string' => $string));
- }
- $start = $offset + $length;
- }
- }
- /**
- * Returns the linearized adress of the current block
- *
- * This method makes use of the {@link $name} but strips of the
- * "main." prefix.<br/>
- * If a suffix is set this will be conatenated to the adress path.
- * Useful for getting the adress of a subelement of the current block.
- *
- * @param string $suffix [optional,default=''] Element suffix
- * @return string Linearized adress of the current block
- ***/
- public function getVarPrefix($suffix = '')
- {
- /* we just need to strip off the "main." of the name. Thus we can use substr
- for now */
- return substr($this->name . '.' . $suffix, 5);
- }
- /**
- * Convert a $data entry to snippet that can be written in our compiled template.
- *
- * If $add_quotes is TRUE the snippet can be used just like echo <snippet>;<br/>
- * If $add_quotes is FALSE the snippet can be used inside single quoted strings.
- * <br/>
- * If $parse_array is true this will be done on all elements of the
- * $param['array'] array and an array of snippets will be returned.
- *
- * @param array $param
- * @param boolean $add_quotes [default=TRUE]
- * @param boolean $parse_array [default=FALSE]
- * @return mixed
- ***/
- public function varToStatic($param, $add_quotes = TRUE, $parse_array = FALSE)
- {
- if ($parse_array) {
- $return = array();
- foreach ($param['array'] as $key => $param_a) {
- $return[$key] = $this->varToStatic($param_a, $add_quotes, FALSE);
- }
- } elseif (isset($param['variable'])) {
- $return = "\$d['" . $this->getVarPrefix() . $param['variable'] . "']";
- if (!$add_quotes) {
- $return = "'.$return.'";
- }
- } elseif (isset($param['global'])) {
- $return = "\$d['" . $param['global'] . "']";
- if (!$add_quotes) {
- $return = "'.$return.'";
- }
- } else {
- $return = $param['static'];
- if ($add_quotes) {
- $return = "'$return'";
- }
- }
- return $return;
- }
- }
- ?>
Documentation generated on Sat, 19 Feb 2005 01:43:39 +0100 by phpDocumentor 1.3.0RC3