Skip to content

Commit

Permalink
Updating Inflector to employ internal caching.
Browse files Browse the repository at this point in the history
Refs #23
  • Loading branch information
predominant committed Dec 25, 2009
1 parent 1a387e6 commit 1992450
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
86 changes: 78 additions & 8 deletions cake/libs/inflector.php
Expand Up @@ -188,6 +188,46 @@ class Inflector {
*/
var $_singularized = array();

/**
* Cached Underscored Inflections
*
* @var array
* @access protected
*/
var $_underscored = array();

/**
* Cached Camelize Inflections
*
* @var array
* @access protected
*/
var $_camelize = array();

/**
* Classify cached inflecctions
*
* @var array
* @access protected
*/
var $_classify = array();

/**
* Tablized cached inflections
*
* @var array
* @access protected
*/
var $_tableize = array();

/**
* Humanize cached inflections
*
* @var array
* @access protected
*/
var $_humanize = array();

/**
* Gets a reference to the Inflector object instance
*
Expand Down Expand Up @@ -338,7 +378,12 @@ function singularize($word) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function camelize($lowerCaseAndUnderscoredWord) {
return str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
$_this =& Inflector::getInstance();

if (!isset($_this->_camelize[$lowerCaseAndUnderscoredWord])) {
$_this->_camelize[$lowerCaseAndUnderscoredWord] = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
}
return $_this->_camelize[$lowerCaseAndUnderscoredWord];
}

/**
Expand All @@ -351,7 +396,12 @@ function camelize($lowerCaseAndUnderscoredWord) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function underscore($camelCasedWord) {
return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
$_this =& Inflector::getInstance();

if (!isset($_this->_underscored[$camelCasedWord])) {
$_this->_underscored[$camelCasedWord] = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
}
return $_this->_underscored[$camelCasedWord];
}

/**
Expand All @@ -365,7 +415,12 @@ function underscore($camelCasedWord) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function humanize($lowerCaseAndUnderscoredWord) {
return ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
$_this =& Inflector::getInstance();

if (!isset($_this->_humanize[$lowerCaseAndUnderscoredWord])) {
$_this->_humanize[$lowerCaseAndUnderscoredWord] = ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
}
return $_this->_humanize[$lowerCaseAndUnderscoredWord];
}

/**
Expand All @@ -378,7 +433,12 @@ function humanize($lowerCaseAndUnderscoredWord) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function tableize($className) {
return Inflector::pluralize(Inflector::underscore($className));
$_this =& Inflector::getInstance();

if (!isset($_this->_tableize[$className])) {
$_this->_tableize[$className] = Inflector::pluralize(Inflector::underscore($className));
}
return $_this->_tableize[$className];
}

/**
Expand All @@ -391,7 +451,12 @@ function tableize($className) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function classify($tableName) {
return Inflector::camelize(Inflector::singularize($tableName));
$_this =& Inflector::getInstance();

if (!isset($_this->_classify[$tableName])) {
$_this->_classify[$tableName] = Inflector::camelize(Inflector::singularize($tableName));
}
return $_this->_classify[$tableName];
}

/**
Expand All @@ -404,9 +469,14 @@ function classify($tableName) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function variable($string) {
$string = Inflector::camelize(Inflector::underscore($string));
$replace = strtolower(substr($string, 0, 1));
return preg_replace('/\\w/', $replace, $string, 1);
$_this =& Inflector::getInstance();

if (!isset($_this->_variable[$string])) {
$string = Inflector::camelize(Inflector::underscore($string));
$replace = strtolower(substr($string, 0, 1));
$_this->_variable[$string] = preg_replace('/\\w/', $replace, $string, 1);
}
return $_this->_variable[$string];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion cake/tests/cases/libs/inflector.test.php
Expand Up @@ -59,7 +59,7 @@ function setUp() {
* @return void
*/
function testInstantiation() {
$this->assertEqual(new Inflector(), $this->Inflector);
$this->assertEqual(Inflector::getInstance(), $this->Inflector);
}

/**
Expand Down

0 comments on commit 1992450

Please sign in to comment.