Skip to content

Commit

Permalink
flattens array for better looking cache if the array is associative
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Apr 13, 2011
1 parent 7b3bdb0 commit f8e5b95
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
4 changes: 1 addition & 3 deletions lib/generator/sfThemeGenerator.class.php
Expand Up @@ -116,8 +116,6 @@ public function configToOptions($configs, $prefix = '')

public function linkTo($action, $params)
{
$action = strpos($action, '_') === 0 ? substr($action, 1) : $action;

$forObject = isset($params['object_link']) && $params['object_link'];

$params = array_merge(array('class' => $action), $params);
Expand Down Expand Up @@ -312,7 +310,7 @@ protected function renderCredentials($credentials)
$credentials = $credentials[0];
}

return $this->asPhp($credentials);
return is_array($credentials) ? $this->parser->renderArray($credentials) : $this->asPhp($credentials);
}

// Provides a hook to change generated files
Expand Down
46 changes: 36 additions & 10 deletions lib/util/sfThemeTokenParser.class.php
Expand Up @@ -31,17 +31,22 @@ public function renderArray($array)
$arrayLines = array();
foreach ($array as $key => $value) {
$line = ' ';
if (is_int($key)) {
$line .= $key;
}
elseif (is_bool($key)) {
$line .= $this->asPhp($key);
}
else {
$line .= $this->renderPhpText($key);
}

$line .= ' => ';
$isAssociative = $this->arrayKeyIsAssociative($key, $array);

if (!$isAssociative) {
if (is_int($key)) {
$line .= $key;
}
elseif (is_bool($key)) {
$line .= $this->asPhp($key);
}
else {
$line .= $this->renderPhpText($key);
}

$line .= ' => ';
}

if (is_int($value)) {
$line .= $value;
Expand Down Expand Up @@ -200,4 +205,25 @@ public function getTokenMatches($tokenMatches)
{
return $this->tokenMatches;
}

protected function arrayKeyIsAssociative($key, $array)
{
if (!is_int($key) || !isset($array[$key]) || count($array) < $key) {
return false;
}

$i = 0;
foreach ($array as $value) {
if ($i == $key) {
return true;
}

if ($i > $key) {
return false;
}
$i++;
}

return false;
}
}

0 comments on commit f8e5b95

Please sign in to comment.