Updating TextHelper to not use create_function(). The create_function calls leaked memory and didn't work with the new helper constructors.

This commit is contained in:
mark_story 2010-07-03 14:55:42 -04:00
parent 2d791e1ed0
commit 6a88452072
2 changed files with 57 additions and 14 deletions

View file

@ -42,6 +42,13 @@ if (!class_exists('Multibyte')) {
*/ */
class TextHelper extends AppHelper { class TextHelper extends AppHelper {
/**
* helpers
*
* @var array
*/
public $helpers = array('Html');
/** /**
* Highlights a given phrase in a text. You can specify any expression in highlighter that * Highlights a given phrase in a text. You can specify any expression in highlighter that
* may include the \1 expression to include the $phrase found. * may include the \1 expression to include the $phrase found.
@ -118,12 +125,50 @@ class TextHelper extends AppHelper {
* @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619 * @link http://book.cakephp.org/view/1469/Text#autoLinkUrls-1619
*/ */
public function autoLinkUrls($text, $htmlOptions = array()) { public function autoLinkUrls($text, $htmlOptions = array()) {
$options = var_export($htmlOptions, true); $this->_linkOptions = $htmlOptions;
$text = preg_replace_callback('#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches', $text = preg_replace_callback(
'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text); '#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i',
array(&$this, '_linkBareUrl'),
$text
);
return preg_replace_callback(
'#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
array(&$this, '_linkUrls'),
$text
);
}
return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i', /**
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'), $text); * Links urls that include http://
*
* @param array $matches
* @return string
* @see TextHelper::autoLinkUrls()
*/
private function _linkBareUrl($matches) {
return $this->Html->link($matches[0], $matches[0], $this->_linkOptions);
}
/**
* Links urls missing http://
*
* @param array $matches
* @return string
* @see TextHelper::autoLinkUrls()
*/
private function _linkUrls($matches) {
return $this->Html->link($matches[0], 'http://' . $matches[0], $this->_linkOptions);
}
/**
* Links email addresses
*
* @param array $matches
* @return string
* @see TextHelper::autoLinkUrls()
*/
private function _linkEmails($matches) {
return $this->Html->link($matches[0], 'mailto:' . $matches[0], $this->_linkOptions);
} }
/** /**
@ -136,15 +181,12 @@ class TextHelper extends AppHelper {
* @link http://book.cakephp.org/view/1469/Text#autoLinkEmails-1618 * @link http://book.cakephp.org/view/1469/Text#autoLinkEmails-1618
*/ */
public function autoLinkEmails($text, $options = array()) { public function autoLinkEmails($text, $options = array()) {
$linkOptions = 'array('; $this->_linkOptions = $options;
foreach ($options as $option => $value) { return preg_replace_callback(
$value = var_export($value, true); '#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
$linkOptions .= "'$option' => $value, "; array(&$this, '_linkEmails'),
} $text
$linkOptions .= ')'; );
return preg_replace_callback('#([_A-Za-z0-9+-]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#',
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "mailto:" . $matches[0],' . $linkOptions . ');'), $text);
} }
/** /**

View file

@ -17,6 +17,7 @@
* @since CakePHP(tm) v 1.2.0.4206 * @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
App::import('Core', 'View');
App::import('Helper', 'Text'); App::import('Helper', 'Text');
/** /**