public function testSetProperty() { $key = 'test_key'; $value = 'test_value'; Liquid::set($key, $value); $this->assertSame($value, Liquid::get($key)); }
public function testValidPathWithCustomExtension() { Liquid::set('INCLUDE_PREFIX', ''); Liquid::set('INCLUDE_SUFFIX', 'tpl'); $root = dirname(__FILE__) . DIRECTORY_SEPARATOR . self::TEMPLATES_DIR . DIRECTORY_SEPARATOR; $templateName = 'mypartial'; $fileSystem = new LocalFileSystem($root); $this->assertEquals($root . Liquid::get('INCLUDE_PREFIX') . $templateName . '.' . Liquid::get('INCLUDE_SUFFIX'), $fileSystem->fullPath($templateName)); }
/** * Extracts tag attributes from a markup string. * * @param string $markup */ protected function extractAttributes($markup) { $this->attributes = array(); $attributeRegexp = new Regexp(Liquid::get('TAG_ATTRIBUTES')); $matches = $attributeRegexp->scan($markup); foreach ($matches as $match) { $this->attributes[$match[0]] = $match[1]; } }
/** * Resolves a given path to a full template file path, making sure it's valid * * @param string $templatePath * * @throws LiquidException * @return string */ public function fullPath($templatePath) { $nameRegex = Liquid::get('INCLUDE_ALLOW_EXT') ? new Regexp('/^[^.\\/][a-zA-Z0-9_\\.\\/]+$/') : new Regexp('/^[^.\\/][a-zA-Z0-9_\\/]+$/'); if (!$nameRegex->match($templatePath)) { throw new LiquidException("Illegal template name '{$templatePath}'"); } if (strpos($templatePath, '/') !== false) { $fullPath = Liquid::get('INCLUDE_ALLOW_EXT') ? $this->root . dirname($templatePath) . '/' . basename($templatePath) : $this->root . dirname($templatePath) . '/' . Liquid::get('INCLUDE_PREFIX') . basename($templatePath) . '.' . Liquid::get('INCLUDE_SUFFIX'); } else { $fullPath = Liquid::get('INCLUDE_ALLOW_EXT') ? $this->root . $templatePath : $this->root . Liquid::get('INCLUDE_PREFIX') . $templatePath . '.' . Liquid::get('INCLUDE_SUFFIX'); } $rootRegex = new Regexp('/' . preg_quote(realpath($this->root), '/') . '/'); if (!$rootRegex->match(realpath($fullPath))) { throw new LiquidException("Illegal template path '" . realpath($fullPath) . "'"); } return $fullPath; }
/** * Constructor * * @param string $markup */ public function __construct($markup) { $this->markup = $markup; $quotedFragmentRegexp = new Regexp('/\\s*(' . Liquid::get('QUOTED_FRAGMENT') . ')/'); $filterSeperatorRegexp = new Regexp('/' . Liquid::get('FILTER_SEPARATOR') . '\\s*(.*)/'); $filterSplitRegexp = new Regexp('/' . Liquid::get('FILTER_SEPARATOR') . '/'); $filterNameRegexp = new Regexp('/\\s*(\\w+)/'); $filterArgumentRegexp = new Regexp('/(?:' . Liquid::get('FILTER_ARGUMENT_SEPARATOR') . '|' . Liquid::get('ARGUMENT_SEPARATOR') . ')\\s*(' . Liquid::get('QUOTED_FRAGMENT_FILTER_ARGUMENT') . ')/'); $quotedFragmentRegexp->match($markup); $this->name = isset($quotedFragmentRegexp->matches[1]) ? $quotedFragmentRegexp->matches[1] : null; if ($filterSeperatorRegexp->match($markup)) { $filters = $filterSplitRegexp->split($filterSeperatorRegexp->matches[1]); foreach ($filters as $filter) { $filterNameRegexp->match($filter); $filtername = $filterNameRegexp->matches[1]; $filterArgumentRegexp->matchAll($filter); $matches = Liquid::arrayFlatten($filterArgumentRegexp->matches[1]); $this->filters[] = array($filtername, $matches); } } else { $this->filters = array(); } }
/** * Tokenizes the given source string * * @param string $source * * @return array */ public static function tokenize($source) { return empty($source) ? array() : preg_split(Liquid::get('TOKENIZATION_REGEXP'), $source, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); }
/** * Resolved the namespaced queries gracefully. * * @param string $key * * @throws LiquidException * @return mixed */ private function variable($key) { // Support [0] style array indicies if (preg_match("|\\[[0-9]+\\]|", $key)) { $key = preg_replace("|\\[([0-9]+)\\]|", ".\$1", $key); } $parts = explode(Liquid::get('VARIABLE_ATTRIBUTE_SEPARATOR'), $key); $object = $this->fetch(array_shift($parts)); if (is_object($object)) { if (!method_exists($object, 'toLiquid')) { throw new LiquidException("Method 'toLiquid' not exists!"); } $object = $object->toLiquid(); } if ($object === null) { return null; } while (count($parts) > 0) { if ($object instanceof Drop) { $object->setContext($this); } $nextPartName = array_shift($parts); if (is_array($object)) { // if the last part of the context variable is .size we just return the count if ($nextPartName == 'size' && count($parts) == 0 && !array_key_exists('size', $object)) { return count($object); } if (array_key_exists($nextPartName, $object)) { $object = $object[$nextPartName]; } else { return null; } } elseif (is_object($object)) { if ($object instanceof Drop) { // if the object is a drop, make sure it supports the given method if (!$object->hasKey($nextPartName)) { return null; } $object = $object->invokeDrop($nextPartName); } elseif (method_exists($object, Liquid::get('HAS_PROPERTY_METHOD'))) { if (!call_user_func(array($object, Liquid::get('HAS_PROPERTY_METHOD')), $nextPartName)) { return null; } call_user_func(array($object, Liquid::get('GET_PROPERTY_METHOD')), $nextPartName); } else { // if it's just a regular object, attempt to access a property if (!property_exists($object, $nextPartName)) { return null; } $object = $object->{$nextPartName}; } } if (is_object($object) && method_exists($object, 'toLiquid')) { $object = $object->toLiquid(); } } return $object; }
/** * Create a variable for the given token * * @param string $token * * @throws \Liquid\LiquidException * @return Variable */ private function createVariable($token) { $variableRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . '(.*)' . Liquid::get('VARIABLE_END') . '$/'); if ($variableRegexp->match($token)) { return new Variable($variableRegexp->matches[1]); } throw new LiquidException("Variable {$token} was not properly terminated"); }
protected function setup() { parent::setUp(); $this->regexp = new Regexp('/' . Liquid::get('QUOTED_FRAGMENT') . '/'); }