Skip to content

Commit

Permalink
Updating modules/cms/twig
Browse files Browse the repository at this point in the history
  • Loading branch information
stefantalen committed Oct 10, 2014
1 parent e114f29 commit 9c1dcb0
Show file tree
Hide file tree
Showing 25 changed files with 116 additions and 74 deletions.
2 changes: 1 addition & 1 deletion modules/cms/twig/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ public function compile(Twig_Compiler $compiler)

$compiler->write("unset(\$context['__cms_component_params']);\n");
}
}
}
8 changes: 6 additions & 2 deletions modules/cms/twig/ComponentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public function parse(Twig_Token $token)
break;

default:
throw new Twig_Error_Syntax(sprintf('Invalid syntax in the partial tag. Line %s', $lineno), $stream->getCurrent()->getLine(), $stream->getFilename());
throw new Twig_Error_Syntax(
sprintf('Invalid syntax in the partial tag. Line %s', $lineno),
$stream->getCurrent()->getLine(),
$stream->getFilename()
);
break;
}
}
Expand All @@ -66,4 +70,4 @@ public function getTag()
{
return 'component';
}
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/ContentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public function compile(Twig_Compiler $compiler)
->write(");\n")
;
}
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/ContentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public function getTag()
{
return 'content';
}
}
}
104 changes: 67 additions & 37 deletions modules/cms/twig/DebugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class DebugExtension extends Twig_Extension
*/
protected $commentMap = [];

protected $blockMethods = ['componentDetails', 'defineProperties', 'getPropertyOptions', 'offsetExists', 'offsetGet', 'offsetSet', 'offsetUnset'];
protected $blockMethods = [
'componentDetails',
'defineProperties',
'getPropertyOptions',
'offsetExists',
'offsetGet',
'offsetSet',
'offsetUnset'
];

/**
* Creates the extension instance.
Expand All @@ -54,7 +62,11 @@ public function __construct(Controller $controller)
public function getFunctions()
{
return array(
new Twig_SimpleFunction('dump', [$this, 'runDump'], array('is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true)),
new Twig_SimpleFunction('dump', [$this, 'runDump'], array(
'is_safe' => ['html'],
'needs_context' => true,
'needs_environment' => true
)),
);
}

Expand Down Expand Up @@ -84,8 +96,7 @@ public function runDump(Twig_Environment $env, $context)
}

$result .= $this->dump($vars, static::PAGE_CAPTION);
}
else {
} else {
$this->variablePrefix = false;
for ($i = 2; $i < $count; $i++) {
$var = func_get_arg($i);
Expand Down Expand Up @@ -124,19 +135,21 @@ public function dump($variables = null, $caption = null)
$info = [];

if (!is_array($variables)) {
if ($variables instanceof Paginator)
if ($variables instanceof Paginator) {
$variables = $this->paginatorToArray($variables);
elseif (is_object($variables))
} elseif (is_object($variables)) {
$variables = $this->objectToArray($variables);
else
} else {
$variables = [$variables];
}
}

$output = [];
$output[] = '<table>';

if ($caption)
if ($caption) {
$output[] = $this->makeTableHeader($caption);
}

foreach ($variables as $key => $item) {
$output[] = $this->makeTableRow($key, $item);
Expand Down Expand Up @@ -185,15 +198,12 @@ protected function evalKeyLabel($key)
{
if ($this->variablePrefix === true) {
$output = '{{ <span>%s</span> }}';
}
elseif (is_array($this->variablePrefix)) {
} elseif (is_array($this->variablePrefix)) {
$prefix = implode('.', $this->variablePrefix);
$output = '{{ <span>'.$prefix.'.%s</span> }}';
}
elseif ($this->variablePrefix) {
} elseif ($this->variablePrefix) {
$output = '{{ <span>'.$this->variablePrefix.'.%s</span> }}';
}
else {
} else {
$output = '%s';
}

Expand Down Expand Up @@ -228,8 +238,9 @@ protected function evalVarLabel($variable)
protected function getType($variable)
{
$type = gettype($variable);
if ($type == 'string' && substr($variable, 0, 12) == '___METHOD___')
if ($type == 'string' && substr($variable, 0, 12) == '___METHOD___') {
return 'method';
}

return $type;
}
Expand All @@ -244,17 +255,15 @@ protected function evalObjLabel($variable)
$class = get_class($variable);
$label = class_basename($variable);

if ($variable instanceof ComponentBase)
if ($variable instanceof ComponentBase) {
$label = '<strong>Component</strong>';

elseif ($variable instanceof Collection)
} elseif ($variable instanceof Collection) {
$label = 'Collection('.$variable->count().')';

elseif ($variable instanceof Paginator)
} elseif ($variable instanceof Paginator) {
$label = 'Paged Collection('.$variable->count().')';

elseif ($variable instanceof Model)
} elseif ($variable instanceof Model) {
$label = 'Model';
}

return '<abbr title="'.e($class).'">'.$label.'</abbr>';
}
Expand All @@ -268,17 +277,21 @@ protected function evalVarDesc($variable, $key)
{
$type = $this->getType($variable);

if ($type == 'method')
if ($type == 'method') {
return $this->evalMethodDesc($variable);
}

if (isset($this->commentMap[$key]))
if (isset($this->commentMap[$key])) {
return $this->commentMap[$key];
}

if ($type == 'array')
if ($type == 'array') {
return $this->evalArrDesc($variable);
}

if ($type == 'object')
if ($type == 'object') {
return $this->evalObjDesc($variable);
}

return '';
}
Expand All @@ -291,8 +304,9 @@ protected function evalVarDesc($variable, $key)
protected function evalMethodDesc($variable)
{
$parts = explode('|', $variable);
if (count($parts) < 2)
if (count($parts) < 2) {
return null;
}

$method = $parts[1];
return isset($this->commentMap[$method]) ? $this->commentMap[$method] : null;
Expand Down Expand Up @@ -370,22 +384,38 @@ protected function objectToArray($object)

$methods = [];
foreach ($info->getMethods() as $method) {
if (!$method->isPublic()) continue; // Only public
if ($method->class != $class) continue; // Only locals
if (!$method->isPublic()) {
continue; // Only public
}
if ($method->class != $class) {
continue; // Only locals
}
$name = $method->getName();
if (in_array($name, $this->blockMethods)) continue; // Blocked methods
if (preg_match('/^on[A-Z]{1}[\w+]*$/', $name)) continue; // AJAX methods
if (preg_match('/^get[A-Z]{1}[\w+]*Options$/', $name)) continue; // getSomethingOptions
if (substr($name, 0, 1) == '_') continue; // Magic/hidden method
if (in_array($name, $this->blockMethods)) {
continue; // Blocked methods
}
if (preg_match('/^on[A-Z]{1}[\w+]*$/', $name)) {
continue; // AJAX methods
}
if (preg_match('/^get[A-Z]{1}[\w+]*Options$/', $name)) {
continue; // getSomethingOptions
}
if (substr($name, 0, 1) == '_') {
continue; // Magic/hidden method
}
$name .= '()';
$methods[$name] = '___METHOD___|'.$name;
$this->commentMap[$name] = $this->evalDocBlock($method);
}

$vars = [];
foreach ($info->getProperties() as $property) {
if (!$property->isPublic()) continue; // Only public
if ($property->class != $class) continue; // Only locals
if (!$property->isPublic()) {
continue; // Only public
}
if ($property->class != $class) {
continue; // Only locals
}
$name = $property->getName();
$vars[$name] = $object->{$name};
$this->commentMap[$name] = $this->evalDocBlock($property);
Expand Down Expand Up @@ -426,8 +456,9 @@ protected function getDataCss($variable)
];

$type = gettype($variable);
if ($type == 'NULL')
if ($type == 'NULL') {
$css['color'] = '#999';
}

return $this->arrayToCss($css);
}
Expand Down Expand Up @@ -484,5 +515,4 @@ protected function arrayToCss(array $rules)

return join('; ', $strings);
}

}
2 changes: 1 addition & 1 deletion modules/cms/twig/DefaultNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public function compile(Twig_Compiler $compiler)
->write("echo '<!-- X_OCTOBER_DEFAULT_BLOCK_CONTENT -->';\n")
;
}
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/DefaultTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public function getTag()
{
return 'default';
}
}
}
8 changes: 5 additions & 3 deletions modules/cms/twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ public function assetsFunction($type = null)
*/
public function placeholderFunction($name, $default = null)
{
if (($result = Block::get($name)) === null)
if (($result = Block::get($name)) === null) {
return null;
}

$result = str_replace('<!-- X_OCTOBER_DEFAULT_BLOCK_CONTENT -->', trim($default), $result);
return $result;
Expand Down Expand Up @@ -199,8 +200,9 @@ public function startBlock($name)
*/
public function displayBlock($name, $default = null)
{
if (($result = Block::placeholder($name)) === null)
if (($result = Block::placeholder($name)) === null) {
return null;
}

$result = str_replace('<!-- X_OCTOBER_DEFAULT_BLOCK_CONTENT -->', trim($default), $result);
return $result;
Expand All @@ -213,4 +215,4 @@ public function endBlock($append = true)
{
Block::endBlock($append);
}
}
}
5 changes: 2 additions & 3 deletions modules/cms/twig/FlashNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function compile(Twig_Compiler $compiler)
->outdent()
->write('}'.PHP_EOL)
;
}
else {
} else {
$compiler
->addDebugInfo($this)
->write('$context["type"] = ')
Expand All @@ -66,4 +65,4 @@ public function compile(Twig_Compiler $compiler)
->write('$context["message"] = $_message;')
;
}
}
}
5 changes: 2 additions & 3 deletions modules/cms/twig/FlashTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public function parse(Twig_Token $token)

if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
$name = $token->getValue();
}
else {
} else {
$name = 'all';
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
Expand All @@ -53,4 +52,4 @@ public function getTag()
{
return 'flash';
}
}
}
11 changes: 7 additions & 4 deletions modules/cms/twig/FrameworkNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ public function compile(Twig_Compiler $compiler)

$compiler
->addDebugInfo($this)
->write("echo '<script src=\"'. Request::getBasePath() .'/modules/system/assets/js/framework.js\"></script>'.PHP_EOL;" . PHP_EOL)
->write("echo '<script src=\"'. Request::getBasePath()
.'/modules/system/assets/js/framework.js\"></script>'.PHP_EOL;" . PHP_EOL)
;

if ($includeExtras) {
$compiler
->write("echo '<script src=\"'. Request::getBasePath() .'/modules/system/assets/js/framework.extras.js\"></script>'.PHP_EOL;" . PHP_EOL)
->write("echo '<link href=\"'. Request::getBasePath() .'/modules/system/assets/css/framework.extras.css\" rel=\"stylesheet\">'.PHP_EOL;" . PHP_EOL)
->write("echo '<script src=\"'. Request::getBasePath()
.'/modules/system/assets/js/framework.extras.js\"></script>'.PHP_EOL;" . PHP_EOL)
->write("echo '<link href=\"'. Request::getBasePath()
.'/modules/system/assets/css/framework.extras.css\" rel=\"stylesheet\">'.PHP_EOL;" . PHP_EOL)
;
}
}
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/FrameworkTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ public function getTag()
{
return 'framework';
}
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public function isFresh($name, $time)
{
return $this->obj->isLoadedFromCache();
}
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/PageNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public function compile(Twig_Compiler $compiler)
->write("echo \$this->env->getExtension('CMS')->pageFunction();\n")
;
}
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/PageTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public function getTag()
{
return 'page';
}
}
}
2 changes: 1 addition & 1 deletion modules/cms/twig/PartialNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ public function compile(Twig_Compiler $compiler)

$compiler->write("unset(\$context['__cms_partial_params']);\n");
}
}
}
Loading

0 comments on commit 9c1dcb0

Please sign in to comment.