Skip to content

Commit

Permalink
Micro optimizations in Helper class, as those functions are calles ve…
Browse files Browse the repository at this point in the history
…ry often the may sum up some milliseconds
  • Loading branch information
lorenzo committed Dec 7, 2010
1 parent 513eb53 commit 1d67d16
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/Cake/View/Helper.php
Expand Up @@ -257,11 +257,9 @@ public function webroot($file) {
* @return string Path with a timestamp added, or not.
*/
public function assetTimestamp($path) {
$timestampEnabled = (
(Configure::read('Asset.timestamp') === true && Configure::read('debug') > 0) ||
Configure::read('Asset.timestamp') === 'force'
);
if (strpos($path, '?') === false && $timestampEnabled) {
$stamp = Configure::read('Asset.timestamp');
$timestampEnabled = $stamp === 'force' || ($stamp === true && Configure::read('debug') > 0);
if ($timestampEnabled && strpos($path, '?') === false) {
$filepath = preg_replace('/^' . preg_quote($this->request->webroot, '/') . '/', '', $path);
$webrootPath = WWW_ROOT . str_replace('/', DS, $filepath);
if (file_exists($webrootPath)) {
Expand Down Expand Up @@ -347,18 +345,19 @@ public function clean($output) {
* @return string Composed attributes.
*/
public function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
if (is_array($options)) {
$options = array_merge(array('escape' => true), $options);
if (!is_string($options)) {
$options = $options + array('escape' => true);

if (!is_array($exclude)) {
$exclude = array();
}
$filtered = array_diff_key($options, array_merge(array_flip($exclude), array('escape' => true)));

$exclude = array('escape' => true) + array_flip($exclude);
$escape = $options['escape'];
$attributes = array();

foreach ($filtered as $key => $value) {
if ($value !== false && $value !== null) {
foreach ($options as $key => $value) {
if (!isset($exclude[$key]) && $value !== false && $value !== null) {
$attributes[] = $this->__formatAttribute($key, $value, $escape);
}
}
Expand All @@ -381,13 +380,20 @@ public function _parseAttributes($options, $exclude = null, $insertBefore = ' ',
function __formatAttribute($key, $value, $escape = true) {
$attribute = '';
$attributeFormat = '%s="%s"';
$minimizedAttributes = array('compact', 'checked', 'declare', 'readonly', 'disabled',
'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');
static $minimizedAttributes = array();
if (empty($minimizedAttributes)) {
$minimizedAttributes = array_flip(array(
'compact', 'checked', 'declare', 'readonly', 'disabled',
'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap',
'multiple', 'noresize'
));
}

if (is_array($value)) {
$value = '';
}

if (in_array($key, $minimizedAttributes)) {
if (isset($minimizedAttributes[$key])) {
if ($value === 1 || $value === true || $value === 'true' || $value === '1' || $value == $key) {
$attribute = sprintf($attributeFormat, $key, $key);
}
Expand Down

0 comments on commit 1d67d16

Please sign in to comment.