Additional optimization refactoring

Removed ability to use deprecated / with plugin, helper, etc combinations. The dot notation is only allowed from this point forward.
Corrected tests for the above changes.
Corrected formating in basics.php

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7623 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2008-09-18 03:09:19 +00:00
parent e3788dd6fa
commit 8ae5866a1c
11 changed files with 96 additions and 93 deletions

View file

@ -117,18 +117,18 @@ if (!function_exists('clone')) {
print "{$var}\n</pre>\n"; print "{$var}\n</pre>\n";
} }
} }
if (!function_exists('getMicrotime')) { if (!function_exists('getMicrotime')) {
/** /**
* Returns microtime for execution time checking * Returns microtime for execution time checking
* *
* @return float Microtime * @return float Microtime
*/ */
function getMicrotime() { function getMicrotime() {
list($usec, $sec) = explode(" ", microtime()); list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec); return ((float)$usec + (float)$sec);
}
} }
if (!function_exists('sortByKey')) { }
if (!function_exists('sortByKey')) {
/** /**
* Sorts given $array by key $sortby. * Sorts given $array by key $sortby.
* *
@ -138,28 +138,28 @@ if (!function_exists('clone')) {
* @param integer $type Type of sorting to perform * @param integer $type Type of sorting to perform
* @return mixed Sorted array * @return mixed Sorted array
*/ */
function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) { function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
if (!is_array($array)) { if (!is_array($array)) {
return null; return null;
}
foreach ($array as $key => $val) {
$sa[$key] = $val[$sortby];
}
if ($order == 'asc') {
asort($sa, $type);
} else {
arsort($sa, $type);
}
foreach ($sa as $key => $val) {
$out[] = $array[$key];
}
return $out;
} }
foreach ($array as $key => $val) {
$sa[$key] = $val[$sortby];
}
if ($order == 'asc') {
asort($sa, $type);
} else {
arsort($sa, $type);
}
foreach ($sa as $key => $val) {
$out[] = $array[$key];
}
return $out;
} }
if (!function_exists('array_combine')) { }
if (!function_exists('array_combine')) {
/** /**
* Combines given identical arrays by using the first array's values as keys, * Combines given identical arrays by using the first array's values as keys,
* and the second one's values as values. (Implemented for back-compatibility with PHP4) * and the second one's values as values. (Implemented for back-compatibility with PHP4)
@ -168,26 +168,26 @@ if (!function_exists('clone')) {
* @param array $a2 Array to use for values * @param array $a2 Array to use for values
* @return mixed Outputs either combined array or false. * @return mixed Outputs either combined array or false.
*/ */
function array_combine($a1, $a2) { function array_combine($a1, $a2) {
$a1 = array_values($a1); $a1 = array_values($a1);
$a2 = array_values($a2); $a2 = array_values($a2);
$c1 = count($a1); $c1 = count($a1);
$c2 = count($a2); $c2 = count($a2);
if ($c1 != $c2) { if ($c1 != $c2) {
return false; return false;
}
if ($c1 <= 0) {
return false;
}
$output=array();
for ($i = 0; $i < $c1; $i++) {
$output[$a1[$i]] = $a2[$i];
}
return $output;
} }
if ($c1 <= 0) {
return false;
}
$output = array();
for ($i = 0; $i < $c1; $i++) {
$output[$a1[$i]] = $a2[$i];
}
return $output;
} }
}
/** /**
* Convenience method for htmlspecialchars. * Convenience method for htmlspecialchars.
* *
@ -407,7 +407,7 @@ if (!function_exists('clone')) {
} }
return null; return null;
} }
if (!function_exists('file_put_contents')) { if (!function_exists('file_put_contents')) {
/** /**
* Writes data into file. * Writes data into file.
* *
@ -417,23 +417,24 @@ if (!function_exists('clone')) {
* @param mixed $data String or array. * @param mixed $data String or array.
* @return boolean Success * @return boolean Success
*/ */
function file_put_contents($fileName, $data) { function file_put_contents($fileName, $data) {
if (is_array($data)) { if (is_array($data)) {
$data = join('', $data); $data = join('', $data);
}
$res = @fopen($fileName, 'w+b');
if ($res) {
$write = @fwrite($res, $data);
if ($write === false) {
return false;
} else {
@fclose($res);
return $write;
}
}
return false;
} }
$res = @fopen($fileName, 'w+b');
if ($res) {
$write = @fwrite($res, $data);
if ($write === false) {
return false;
} else {
@fclose($res);
return $write;
}
}
return false;
} }
}
/** /**
* Reads/writes temporary data to cache files or session. * Reads/writes temporary data to cache files or session.
* *

View file

@ -164,13 +164,15 @@ class Component extends Object {
if (is_array($object->components)) { if (is_array($object->components)) {
$normal = Set::normalize($object->components); $normal = Set::normalize($object->components);
foreach ($normal as $component => $config) { foreach ($normal as $component => $config) {
$parts = preg_split('/\/|\./', $component); $plugin = null;
if (count($parts) === 1) { if (isset($this->__controllerVars['plugin'])) {
$plugin = $this->__controllerVars['plugin'] . '.'; $plugin = $this->__controllerVars['plugin'] . '.';
} else { }
$plugin = Inflector::underscore($parts['0']) . '.';
$component = array_pop($parts); if (strpos($component, '.') !== false) {
list($plugin, $component) = explode('.', $component);
$plugin = $plugin . '.';
} }
$componentCn = $component . 'Component'; $componentCn = $component . 'Component';

View file

@ -299,7 +299,8 @@ class DbAcl extends AclBase {
$inherited = array(); $inherited = array();
$acoIDs = Set::extract($acoPath, '{n}.' . $this->Aco->alias . '.id'); $acoIDs = Set::extract($acoPath, '{n}.' . $this->Aco->alias . '.id');
for ($i = 0 ; $i < count($aroPath); $i++) { $count = count($aroPath);
for ($i = 0 ; $i < $count; $i++) {
$permAlias = $this->Aro->Permission->alias; $permAlias = $this->Aro->Permission->alias;
$perms = $this->Aro->Permission->find('all', array( $perms = $this->Aro->Permission->find('all', array(

View file

@ -564,7 +564,7 @@ class SecurityComponent extends Object {
} }
foreach ($this->disabledFields as $value) { foreach ($this->disabledFields as $value) {
$parts = preg_split('/\/|\./', $value); $parts = explode('.', $value);
if (count($parts) == 1) { if (count($parts) == 1) {
$key1[] = $controller->modelClass . '.' . $parts['0']; $key1[] = $controller->modelClass . '.' . $parts['0'];

View file

@ -228,8 +228,9 @@ class Debugger extends Object {
$backtrace = debug_backtrace(); $backtrace = debug_backtrace();
$back = array(); $back = array();
$count = count($backtrace);
for ($i = $options['start']; $i < count($backtrace) && $i < $options['depth']; $i++) { for ($i = $options['start']; $i < $count && $i < $options['depth']; $i++) {
$trace = array_merge( $trace = array_merge(
array( array(
'file' => '[internal]', 'file' => '[internal]',

View file

@ -143,8 +143,8 @@ class Flay extends Object{
} }
} }
if (count($links)) { if ($count = count($links)) {
for ($ii = 0; $ii < count($links); $ii++) { for ($ii = 0; $ii < $count; $ii++) {
if (preg_match("#^(http|https|ftp|nntp)://#", $links[$ii])) { if (preg_match("#^(http|https|ftp|nntp)://#", $links[$ii])) {
$prefix = null; $prefix = null;
} else { } else {

View file

@ -1238,7 +1238,8 @@ class DboSource extends DataSource {
function buildStatement($query, $model) { function buildStatement($query, $model) {
$query = array_merge(array('offset' => null, 'joins' => array()), $query); $query = array_merge(array('offset' => null, 'joins' => array()), $query);
if (!empty($query['joins'])) { if (!empty($query['joins'])) {
for ($i = 0; $i < count($query['joins']); $i++) { $count = count($query['joins']);
for ($i = 0; $i < $count; $i++) {
if (is_array($query['joins'][$i])) { if (is_array($query['joins'][$i])) {
$query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]); $query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]);
} }

View file

@ -332,7 +332,7 @@ class Helper extends Overloadable {
$model = $view->model; $model = $view->model;
$sameScope = $hasField = false; $sameScope = $hasField = false;
$parts = array_values(Set::filter(preg_split('/\/|\./', $entity), true)); $parts = array_values(Set::filter(explode('.', $entity), true));
if (empty($parts)) { if (empty($parts)) {
return; return;

View file

@ -100,8 +100,8 @@ class FormHelper extends AppHelper {
$defaultModel = $this->params['models'][0]; $defaultModel = $this->params['models'][0];
} elseif (empty($model) && empty($this->params['models'])) { } elseif (empty($model) && empty($this->params['models'])) {
$model = false; $model = false;
} elseif (is_string($model) && (strpos($model, '/') !== false || strpos($model, '.') !== false)) { } elseif (is_string($model) && strpos($model, '.') !== false) {
$path = preg_split('/\/|\./', $model); $path = explode('.', $model);
$model = $path[count($path) - 1]; $model = $path[count($path) - 1];
} }
@ -327,7 +327,7 @@ class FormHelper extends AppHelper {
if (isset($this->params['_Token']) && !empty($this->params['_Token'])) { if (isset($this->params['_Token']) && !empty($this->params['_Token'])) {
if (!empty($this->params['_Token']['disabledFields'])) { if (!empty($this->params['_Token']['disabledFields'])) {
foreach ($this->params['_Token']['disabledFields'] as $value) { foreach ($this->params['_Token']['disabledFields'] as $value) {
$parts = preg_split('/\/|\./', $value); $parts = explode('.', $value);
if (count($parts) == 1) { if (count($parts) == 1) {
if ($parts[0] === $field || $parts[0] === $fieldSuffix) { if ($parts[0] === $field || $parts[0] === $fieldSuffix) {
return; return;
@ -463,8 +463,8 @@ class FormHelper extends AppHelper {
} }
if ($text === null) { if ($text === null) {
if (strpos($fieldName, '/') !== false || strpos($fieldName, '.') !== false) { if (strpos($fieldName, '.') !== false) {
$text = array_pop(preg_split('/[\/\.]+/', $fieldName)); $text = array_pop(explode('.', $fieldName));
} else { } else {
$text = $fieldName; $text = $fieldName;
} }

View file

@ -702,13 +702,10 @@ class View extends Object {
$options = $helper; $options = $helper;
$helper = $i; $helper = $i;
} }
$parts = preg_split('/\/|\./', $helper); $plugin = $this->plugin;
if (count($parts) === 1) { if (strpos($helper, '.') !== false) {
$plugin = $this->plugin; list($plugin, $helper) = explode('.', $helper);
} else {
$plugin = Inflector::underscore($parts['0']);
$helper = $parts[count($parts) - 1];
} }
$helperCn = $helper . 'Helper'; $helperCn = $helper . 'Helper';

View file

@ -1158,7 +1158,7 @@ class FormHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->hidden('Contact/idontexist'); $result = $this->Form->hidden('Contact.idontexist');
$expected = array( $expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Contact][idontexist]', 'value' => '', 'id' => 'ContactIdontexist'), 'input' => array('type' => 'hidden', 'name' => 'data[Contact][idontexist]', 'value' => '', 'id' => 'ContactIdontexist'),
); );
@ -1186,7 +1186,7 @@ class FormHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->input('Contact/password'); $result = $this->Form->input('Contact.password');
$expected = array( $expected = array(
'div' => array('class' => 'input password'), 'div' => array('class' => 'input password'),
'label' => array('for' => 'ContactPassword'), 'label' => array('for' => 'ContactPassword'),
@ -1438,7 +1438,7 @@ class FormHelperTest extends CakeTestCase {
'/div' '/div'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
for ($i = 1; $i < 5; $i++) { for ($i = 1; $i < 5; $i++) {
$result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i)); $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
$expected = array( $expected = array(
@ -1451,7 +1451,7 @@ class FormHelperTest extends CakeTestCase {
'/div' '/div'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
} }
} }
/** /**
* testFormInputs method * testFormInputs method
@ -1683,7 +1683,7 @@ class FormHelperTest extends CakeTestCase {
* @return void * @return void
*/ */
function testLabel() { function testLabel() {
$this->Form->text('Person/name'); $this->Form->text('Person.name');
$result = $this->Form->label(); $result = $this->Form->label();
$this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label')); $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
@ -1726,12 +1726,12 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'theID'))); $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'theID')));
$this->Form->data['Model']['text'] = 'test <strong>HTML</strong> values'; $this->Form->data['Model']['text'] = 'test <strong>HTML</strong> values';
$result = $this->Form->text('Model/text'); $result = $this->Form->text('Model.text');
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText'))); $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
$this->Form->validationErrors['Model']['text'] = 1; $this->Form->validationErrors['Model']['text'] = 1;
$this->Form->data['Model']['text'] = 'test'; $this->Form->data['Model']['text'] = 'test';
$result = $this->Form->text('Model/text', array('id' => 'theID')); $result = $this->Form->text('Model.text', array('id' => 'theID'));
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'))); $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
} }
/** /**
@ -1795,7 +1795,7 @@ class FormHelperTest extends CakeTestCase {
$this->Form->validationErrors['Model']['passwd'] = 1; $this->Form->validationErrors['Model']['passwd'] = 1;
$this->Form->data['Model']['passwd'] = 'test'; $this->Form->data['Model']['passwd'] = 'test';
$result = $this->Form->password('Model/passwd', array('id' => 'theID')); $result = $this->Form->password('Model.passwd', array('id' => 'theID'));
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'))); $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
} }
/** /**
@ -1993,7 +1993,7 @@ class FormHelperTest extends CakeTestCase {
'/div' '/div'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe'))); $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
$expected = array( $expected = array(
'div' => array('class' => 'input radio'), 'div' => array('class' => 'input radio'),
@ -2490,7 +2490,7 @@ class FormHelperTest extends CakeTestCase {
array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value')) array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->checkbox('Test.test', array('name' => 'myField')); $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
$expected = array( $expected = array(
'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'), 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
@ -3546,7 +3546,7 @@ class FormHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->textarea('Model/tmp'); $result = $this->Form->textarea('Model.tmp');
$expected = array( $expected = array(
'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'), 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
'/textarea', '/textarea',