/**
* Autoloads Twig classes.
*
- * @package twig
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Twig_Autoloader
{
*
* @return Twig_Compiler The current compiler instance
*/
- public function compile(Twig_NodeInterface $node);
+ function compile(Twig_NodeInterface $node);
/**
* Gets the current PHP code after compilation.
*
* @return string The PHP code
*/
- public function getSource();
+ function getSource();
}
* file that was distributed with this source code.
*/
+/**
+ * Stores the Twig configuration.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
class Twig_Environment
{
const VERSION = '1.0.0-BETA1';
protected $filename;
protected $rawMessage;
+ /**
+ * Constructor.
+ *
+ * @param string $message The error message
+ * @param integer $lineno The template line where the error occurred
+ * @param string $filename The template file name where the error occurred
+ */
public function __construct($message, $lineno = -1, $filename = null)
{
$this->lineno = $lineno;
parent::__construct($this->message);
}
+ /**
+ * Gets the filename where the error occurred.
+ *
+ * @return string The filename
+ */
public function getFilename()
{
return $this->filename;
}
+ /**
+ * Sets the filename where the error occurred.
+ *
+ * @param string $filename The filename
+ */
public function setFilename($filename)
{
$this->filename = $filename;
*
* This is where you can load some file that contains filter functions for instance.
*
- * @param Twig_Environment $environement The current Twig_Environment instance
+ * @param Twig_Environment $environment The current Twig_Environment instance
*/
- public function initRuntime(Twig_Environment $environement)
+ public function initRuntime(Twig_Environment $environment)
{
}
*
* This is where you can load some file that contains filter functions for instance.
*
- * @param Twig_Environment $environement The current Twig_Environment instance
+ * @param Twig_Environment $environment The current Twig_Environment instance
*/
- public function initRuntime(Twig_Environment $environement);
+ function initRuntime(Twig_Environment $environment);
/**
* Returns the token parser instances to add to the existing list.
*
* @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
*/
- public function getTokenParsers();
+ function getTokenParsers();
/**
* Returns the node visitor instances to add to the existing list.
*
* @return array An array of Twig_NodeVisitorInterface instances
*/
- public function getNodeVisitors();
+ function getNodeVisitors();
/**
* Returns a list of filters to add to the existing list.
*
* @return array An array of filters
*/
- public function getFilters();
+ function getFilters();
/**
* Returns a list of tests to add to the existing list.
*
* @return array An array of tests
*/
- public function getTests();
+ function getTests();
/**
* Returns a list of functions to add to the existing list.
*
* @return array An array of functions
*/
- public function getFunctions();
+ function getFunctions();
/**
* Returns a list of operators to add to the existing list.
*
* @return array An array of operators
*/
- public function getOperators();
+ function getOperators();
/**
* Returns a list of global functions to add to the existing list.
*
* @return array An array of global functions
*/
- public function getGlobals();
+ function getGlobals();
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
- public function getName();
+ function getName();
}
*/
interface Twig_FilterInterface
{
- public function compile();
- public function needsEnvironment();
- public function getSafe(Twig_Node $filterArgs);
- public function getPreEscape();
+ /**
+ * Compiles a filter.
+ *
+ * @return string The PHP code for the filter
+ */
+ function compile();
+
+ function needsEnvironment();
+
+ function getSafe(Twig_Node $filterArgs);
+
+ function getPreEscape();
}
*/
interface Twig_FunctionInterface
{
- public function compile();
- public function needsEnvironment();
- public function getSafe(Twig_Node $filterArgs);
+ /**
+ * Compiles a function.
+ *
+ * @return string The PHP code for the function
+ */
+ function compile();
+
+ function needsEnvironment();
+
+ function getSafe(Twig_Node $filterArgs);
}
*/
interface Twig_GrammarInterface
{
- public function setParser(Twig_ParserInterface $parser);
+ function setParser(Twig_ParserInterface $parser);
- public function parse(Twig_Token $token);
+ function parse(Twig_Token $token);
- public function getName();
+ function getName();
}
*
* @return Twig_TokenStream A token stream instance
*/
- public function tokenize($code, $filename = null);
+ function tokenize($code, $filename = null);
}
*
* @return string The template source code
*/
- public function getSource($name);
+ function getSource($name);
/**
* Gets the cache key to use for the cache for a given template name.
*
* @return string The cache key
*/
- public function getCacheKey($name);
+ function getCacheKey($name);
/**
* Returns true if the template is still fresh.
* @param string $name The template name
* @param timestamp $time The last modification time of the cached template
*/
- public function isFresh($name, $time);
+ function isFresh($name, $time);
}
*
* @param Twig_Compiler A Twig_Compiler instance
*/
- public function compile($compiler);
+ function compile($compiler);
- public function getLine();
+ function getLine();
- public function getNodeTag();
+ function getNodeTag();
}
*
* @param Twig_NodeInterface The modified node
*/
- public function enterNode(Twig_NodeInterface $node, Twig_Environment $env);
+ function enterNode(Twig_NodeInterface $node, Twig_Environment $env);
/**
* Called after child nodes are visited.
*
* @param Twig_NodeInterface The modified node
*/
- public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env);
+ function leaveNode(Twig_NodeInterface $node, Twig_Environment $env);
/**
* Returns the priority for this visitor.
*
* @return integer The priority level
*/
- public function getPriority();
+ function getPriority();
}
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
+/**
+ * Default parser implementation.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
class Twig_Parser implements Twig_ParserInterface
{
protected $stream;
protected $reservedMacroNames;
protected $importedFunctions;
+ /**
+ * Constructor.
+ *
+ * @param Twig_Environment $env A Twig_Environment instance
+ */
public function __construct(Twig_Environment $env)
{
$this->env = $env;
/**
* Interface implemented by parser classes.
*
- * @package twig
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
interface Twig_ParserInterface
{
*
* @return Twig_Node_Module A node tree
*/
- public function parse(Twig_TokenStream $code);
+ function parse(Twig_TokenStream $code);
}
*/
interface Twig_Sandbox_SecurityPolicyInterface
{
- public function checkSecurity($tags, $filters, $functions);
+ function checkSecurity($tags, $filters, $functions);
- public function checkMethodAllowed($obj, $method);
+ function checkMethodAllowed($obj, $method);
- public function checkPropertyAllowed($obj, $method);
+ function checkPropertyAllowed($obj, $method);
}
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
+/**
+ * Default base class for compiled templates.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
abstract class Twig_Template implements Twig_TemplateInterface
{
static protected $cache = array();
protected $env;
protected $blocks;
+ /**
+ * Constructor.
+ *
+ * @param Twig_Environment $env A Twig_Environment instance
+ */
public function __construct(Twig_Environment $env)
{
$this->env = $env;
$this->blocks = array();
}
+ /**
+ * Returns the template name.
+ *
+ * @return string The template name
+ */
public function getTemplateName()
{
return null;
}
+ /**
+ * Returns the Twig environment.
+ *
+ * @return Twig_Environment The Twig environment
+ */
public function getEnvironment()
{
return $this->env;
}
+ /**
+ * Returns the parent template.
+ *
+ * @return Twig_TemplateInterface|false The parent template or false if there is no parent
+ */
public function getParent(array $context)
{
return false;
}
+ /**
+ * Displays a parent block.
+ *
+ * @param string $name The block name to display from the parent
+ * @param array $context The context
+ * @param array $blocks The current set of blocks
+ */
public function displayParentBlock($name, array $context, array $blocks = array())
{
if (false !== $parent = $this->getParent($context)) {
}
}
+ /**
+ * Displays a block.
+ *
+ * @param string $name The block name to display
+ * @param array $context The context
+ * @param array $blocks The current set of blocks
+ */
public function displayBlock($name, array $context, array $blocks = array())
{
if (isset($blocks[$name])) {
}
}
+ /**
+ * Renders a parent block.
+ *
+ * @param string $name The block name to render from the parent
+ * @param array $context The context
+ * @param array $blocks The current set of blocks
+ *
+ * @return string The rendered block
+ */
public function renderParentBlock($name, array $context, array $blocks = array())
{
ob_start();
return new Twig_Markup(ob_get_clean());
}
+ /**
+ * Renders a block.
+ *
+ * @param string $name The block name to render
+ * @param array $context The context
+ * @param array $blocks The current set of blocks
+ *
+ * @return string The rendered block
+ */
public function renderBlock($name, array $context, array $blocks = array())
{
ob_start();
return new Twig_Markup(ob_get_clean());
}
+ /**
+ * Returns whether a block exists or not.
+ *
+ * @param string $name The block name
+ *
+ * @return Boolean true if the block exists, false otherwise
+ */
public function hasBlock($name)
{
return isset($this->blocks[$name]);
}
+ /**
+ * Returns all block names.
+ *
+ * @return array An array of block names
+ */
public function getBlockNames()
{
return array_keys($this->blocks);
return ob_get_clean();
}
+ /**
+ * Returns a variable from the context.
+ *
+ * @param array $context The context
+ * @param string $item The variable to return from the context
+ * @param integer $line The line where the variable is get
+ *
+ * @param mixed The variable value in the context
+ *
+ * @throws Twig_Error_Runtime if the variable does not exist
+ */
protected function getContext($context, $item, $line = -1)
{
if (!array_key_exists($item, $context)) {
return $context[$item];
}
+ /**
+ * Returns the attribute value for a given array/object.
+ *
+ * @param mixed $object The object or array from where to get the item
+ * @param mixed $item The item to get from the array or object
+ * @param array $arguments An array of arguments to pass if the item is an object method
+ * @param integer $type The type of attribute (@see Twig_Node_Expression_GetAttr)
+ * @param Boolean $noStrictCheck Whether to throw an exception if the item does not exist ot not
+ */
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $noStrictCheck = false)
{
// array
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
+/**
+ * Interface implemented by all compiled templates.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
interface Twig_TemplateInterface
{
/**
*
* @return string The rendered template
*/
- public function render(array $context);
+ function render(array $context);
/**
* Displays the template with the given context.
*
* @param array $context An array of parameters to pass to the template
*/
- public function display(array $context);
+ function display(array $context);
/**
* Returns the bound environment for this template.
*
* @return Twig_Environment The current environment
*/
- public function getEnvironment();
+ function getEnvironment();
}
*/
interface Twig_TestInterface
{
- public function compile();
+ /**
+ * Compiles a test.
+ *
+ * @return string The PHP code for the test
+ */
+ function compile();
}
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
+/**
+ * Represents a Token.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
class Twig_Token
{
protected $value;
const OPERATOR_TYPE = 8;
const PUNCTUATION_TYPE = 9;
+ /**
+ * Constructor.
+ *
+ * @param integer $type The type of the token
+ * @param string $value The token value
+ * @param integer $lineno The line positionl in the source
+ */
public function __construct($type, $value, $lineno)
{
$this->type = $type;
$this->lineno = $lineno;
}
+ /**
+ * Returns a string representation of the token.
+ *
+ * @return string A string representation of the token
+ */
public function __toString()
{
return sprintf('%s(%s)', self::typeToString($this->type, true), $this->value);
}
/**
- * Test the current token for a type. The first argument is the type
+ * Tests the current token for a type.
+ *
+ * The first argument is the type
* of the token (if not given Twig_Token::NAME_TYPE), the second the
* value of the token (if not given value is not checked).
- * the token value can be an array if multiple checks should be
+ *
+ * The token value can be an array if multiple checks should be
* performed.
+ *
+ * @param integer $type The type to test
+ * @param array|string|null $values The token value
+ *
+ * @return Boolean
*/
public function test($type, $values = null)
{
);
}
+ /**
+ * Gets the line.
+ *
+ * @return integer The source line
+ */
public function getLine()
{
return $this->lineno;
}
+ /**
+ * Gets the token type.
+ *
+ * @return integer The token type
+ */
public function getType()
{
return $this->type;
}
+ /**
+ * Gets the token value.
+ *
+ * @return string The token value
+ */
public function getValue()
{
return $this->value;
}
+ /**
+ * Returns the constant representation (internal) of a given type.
+ *
+ * @param integer $type The type as an integer
+ * @param Boolean $short Whether to return a short representation or not
+ *
+ * @return string The string representation
+ */
static public function typeToString($type, $short = false)
{
switch ($type) {
return $short ? $name : 'Twig_Token::'.$name;
}
+ /**
+ * Returns the english representation of a given type.
+ *
+ * @param integer $type The type as an integer
+ * @param Boolean $short Whether to return a short representation or not
+ *
+ * @return string The string representation
+ */
static public function typeToEnglish($type)
{
switch ($type) {
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
+/**
+ * Base class for all token parsers.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
abstract class Twig_TokenParser implements Twig_TokenParserInterface
{
protected $parser;
+ /**
+ * Sets the parser associated with this token parser
+ *
+ * @param $parser A Twig_Parser instance
+ */
public function setParser(Twig_Parser $parser)
{
$this->parser = $parser;
/**
* Default implementation of a token parser broker.
*
- * @package twig
- * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
+ * @package twig
+ * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
*/
class Twig_TokenParserBroker implements Twig_TokenParserBrokerInterface
{
protected $brokers = array();
/**
- * Constructor
+ * Constructor.
*
* @param array|Iterable $parsers An Iterable of Twig_TokenParserInterface instances
* @param array|Iterable $brokers An Iterable of Twig_TokenParserBrokerInterface instances
}
/**
- * Adds a TokenParser
+ * Adds a TokenParser.
*
* @param Twig_TokenParserInterface $parser A Twig_TokenParserInterface instance
*/
}
/**
- * Adds a TokenParserBroker
+ * Adds a TokenParserBroker.
*
* @param Twig_TokenParserBroker $broker A Twig_TokenParserBroker instance
*/
}
/**
- * Get a suitable TokenParser for $tag
+ * Gets a suitable TokenParser for a tag.
*
* First looks in parsers, then in brokers.
*
* @param string $tag A tag name
+ *
* @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
*/
public function getTokenParser($tag)
*
* Token parser brokers allows to implement custom logic in the process of resolving a token parser for a given tag name.
*
- * @package twig
- * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
+ * @package twig
+ * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
*/
interface Twig_TokenParserBrokerInterface
{
/**
- * Get a TokenParser suitable for $tag
+ * Gets a TokenParser suitable for a tag.
+ *
+ * @param string $tag A tag name
*
- * @param string $tag A tag name
* @return null|Twig_TokenParserInterface A Twig_TokenParserInterface or null if no suitable TokenParser was found
*/
function getTokenParser($tag);
/**
- * Calls Twig_TokenParserInterface::setParser on all parsers the implementation knowns of
+ * Calls Twig_TokenParserInterface::setParser on all parsers the implementation knowns of.
*
* @param Twig_ParserInterface $parser A Twig_ParserInterface interface
*/
function setParser(Twig_ParserInterface $parser);
/**
- * Get the Twig_ParserInterface
+ * Gets the Twig_ParserInterface.
*
* @return null|Twig_ParserInterface A Twig_ParserInterface instance of null
*/
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
+/**
+ * Interface implemented by token parsers.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
interface Twig_TokenParserInterface
{
- public function setParser(Twig_Parser $parser);
+ /**
+ * Sets the parser associated with this token parser
+ *
+ * @param $parser A Twig_Parser instance
+ */
+ function setParser(Twig_Parser $parser);
/**
* Parses a token and returns a node.
*
* @return Twig_NodeInterface A Twig_NodeInterface instance
*/
- public function parse(Twig_Token $token);
+ function parse(Twig_Token $token);
/**
* Gets the tag name associated with this token parser.
*
* @param string The tag name
*/
- public function getTag();
+ function getTag();
}
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
+/**
+ * Represents a token stream.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
class Twig_TokenStream
{
protected $tokens;
protected $filename;
/**
- * @param array $tokens Array of tokens
- * @param string $filename Name which $tokens are associated with
+ * Constructor.
+ *
+ * @param array $tokens An array of tokens
+ * @param string $filename The name of the filename which tokens are associated with
*/
public function __construct(array $tokens, $filename = null)
{
$this->filename = $filename;
}
+ /**
+ * Returns a string representation of the token stream.
+ *
+ * @return string
+ */
public function __toString()
{
return implode("\n", $this->tokens);
}
/**
- * test()s a token and returns it or throws a syntax error.
+ * Tests a token and returns it or throws a syntax error.
*
* @return Twig_Token
*/
}
/**
- * test() current token
+ * Tests the current token
*
* @return bool
*/