Removing direct reference between View and helpers, and explicitly defining all Helper object properties

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3959 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-11-25 08:55:16 +00:00
parent 1fa21aa551
commit 41c79da1c3
7 changed files with 147 additions and 83 deletions

View file

@ -43,12 +43,30 @@ uses('overloadable');
*/ */
class Helper extends Overloadable { class Helper extends Overloadable {
/**
* List of helpers used by this helper
*
* @var array
*/
var $helpers = null;
/** /**
* Base URL * Base URL
* *
* @var string * @var string
*/ */
var $base = null; var $base = null;
/**
* Webroot path
*
* @var string
*/
var $webroot = null;
/**
* Theme name
*
* @var string
*/
var $themeWeb = null;
/** /**
* URL to current action. * URL to current action.
* *
@ -68,11 +86,36 @@ class Helper extends Overloadable {
*/ */
var $action = null; var $action = null;
/** /**
* Enter description here... * Plugin path
*
* @var string
*/
var $plugin = null;
/**
* POST data for models
* *
* @var array * @var array
*/ */
var $data = null; var $data = null;
/**
* List of named arguments
*
* @var array
*/
var $namedArgs = null;
/**
* URL argument separator character
*
* @var string
*/
var $argSeparator = null;
/**
* Contains model validation errors of form post-backs
*
* @access public
* @var array
*/
var $validationErrors = null;
/** /**
* Holds tag templates. * Holds tag templates.
* *
@ -85,8 +128,8 @@ class Helper extends Overloadable {
* *
* @access protected * @access protected
*/ */
function __get__($name) {} function get__($name) {}
function __set__($name, $value) {} function set__($name, $value) {}
function __call__($method, $params) { function __call__($method, $params) {
trigger_error('Method ' . get_class($this) . '::' . $method . ' does not exist', E_USER_ERROR); trigger_error('Method ' . get_class($this) . '::' . $method . ' does not exist', E_USER_ERROR);
} }
@ -212,19 +255,21 @@ class Helper extends Overloadable {
* @param string $tagValue A field name, like "Modelname/fieldname" * @param string $tagValue A field name, like "Modelname/fieldname"
*/ */
function setFormTag($tagValue) { function setFormTag($tagValue) {
$view =& ClassRegistry::getObject('_view_');
$parts = explode("/", $tagValue); $parts = explode("/", $tagValue);
if (count($parts) == 1) { if (count($parts) == 1) {
$this->view->field = $parts[0]; $view->field = $parts[0];
} elseif (count($parts) == 2 && is_numeric($parts[0])) { } elseif (count($parts) == 2 && is_numeric($parts[0])) {
$this->view->modelId = $parts[0]; $view->modelId = $parts[0];
$this->view->field = $parts[1]; $view->field = $parts[1];
} elseif (count($parts) == 2) { } elseif (count($parts) == 2) {
$this->view->model = $parts[0]; $view->model = $parts[0];
$this->view->field = $parts[1]; $view->field = $parts[1];
} elseif (count($parts) == 3) { } elseif (count($parts) == 3) {
$this->view->model = $parts[0]; $view->model = $parts[0];
$this->view->modelId = $parts[1]; $view->modelId = $parts[1];
$this->view->field = $parts[2]; $view->field = $parts[2];
} }
} }
/** /**
@ -233,7 +278,8 @@ class Helper extends Overloadable {
* @return string * @return string
*/ */
function model() { function model() {
return $this->view->model; $view =& ClassRegistry::getObject('_view_');
return $view->model;
} }
/** /**
* Enter description here... * Enter description here...
@ -241,7 +287,8 @@ class Helper extends Overloadable {
* @return string * @return string
*/ */
function field() { function field() {
return $this->view->field; $view =& ClassRegistry::getObject('_view_');
return $view->field;
} }
/** /**
* Returns false if given FORM field has no errors. Otherwise it returns the constant set in the array Model->validationErrors. * Returns false if given FORM field has no errors. Otherwise it returns the constant set in the array Model->validationErrors.

View file

@ -191,6 +191,7 @@ class CacheHelper extends AppHelper {
*/ */
function __writeFile($file, $timestamp) { function __writeFile($file, $timestamp) {
$now = time(); $now = time();
$view =& ClassRegistry::getObject('_view_');
if (is_numeric($timestamp)) { if (is_numeric($timestamp)) {
$cacheTime = $now + $timestamp; $cacheTime = $now + $timestamp;
@ -200,21 +201,21 @@ class CacheHelper extends AppHelper {
$cache = convertSlash($this->here) . '.php'; $cache = convertSlash($this->here) . '.php';
$file = '<!--cachetime:' . $cacheTime . '--><?php $file = '<!--cachetime:' . $cacheTime . '--><?php
loadController(\'' . $this->view->name . '\'); loadController(\'' . $view->name . '\');
loadModels(); loadModels();
$this->controller = new ' . $this->view->name . 'Controller(); $this->controller = new ' . $view->name . 'Controller();
$this->helpers = unserialize(\'' . serialize($this->view->helpers) . '\'); $this->helpers = unserialize(\'' . serialize($view->helpers) . '\');
$this->base = \'' . $this->view->base . '\'; $this->base = \'' . $view->base . '\';
$this->layout = \'' . $this->view->layout. '\'; $this->layout = \'' . $view->layout. '\';
$this->webroot = \'' . $this->view->webroot . '\'; $this->webroot = \'' . $view->webroot . '\';
$this->here = \'' . $this->view->here . '\'; $this->here = \'' . $view->here . '\';
$this->namedArgs = \'' . $this->view->namedArgs . '\'; $this->namedArgs = \'' . $view->namedArgs . '\';
$this->argSeparator = \'' . $this->view->argSeparator . '\'; $this->argSeparator = \'' . $view->argSeparator . '\';
$this->params = unserialize(\'' . serialize($this->view->params) . '\'); $this->params = unserialize(\'' . serialize($view->params) . '\');
$this->action = unserialize(\'' . serialize($this->view->action) . '\'); $this->action = unserialize(\'' . serialize($view->action) . '\');
$this->data = unserialize(\'' . serialize($this->view->data) . '\'); $this->data = unserialize(\'' . serialize($view->data) . '\');
$this->themeWeb = \'' . $this->view->themeWeb . '\'; $this->themeWeb = \'' . $view->themeWeb . '\';
$this->plugin = \'' . $this->view->plugin . '\'; $this->plugin = \'' . $view->plugin . '\';
$loadedHelpers = array(); $loadedHelpers = array();
$loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers); $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
foreach(array_keys($loadedHelpers) as $helper) foreach(array_keys($loadedHelpers) as $helper)

View file

@ -45,10 +45,12 @@ class HtmlHelper extends AppHelper {
* *
* @var array * @var array
*/ */
var $tags = array('metalink' => '<link href="%s" title="%s"%s />', var $tags = array(
'metalink' => '<link href="%s" title="%s"%s />',
'link' => '<a href="%s" %s>%s</a>', 'link' => '<a href="%s" %s>%s</a>',
'mailto' => '<a href="mailto:%s" %s>%s</a>', 'mailto' => '<a href="mailto:%s" %s>%s</a>',
'form' => '<form %s>', 'form' => '<form %s>',
'formend' => '</form>',
'input' => '<input name="data[%s][%s]" %s/>', 'input' => '<input name="data[%s][%s]" %s/>',
'textarea' => '<textarea name="data[%s][%s]" %s>%s</textarea>', 'textarea' => '<textarea name="data[%s][%s]" %s>%s</textarea>',
'hidden' => '<input type="hidden" name="data[%s][%s]" %s/>', 'hidden' => '<input type="hidden" name="data[%s][%s]" %s/>',
@ -84,7 +86,8 @@ class HtmlHelper extends AppHelper {
'legend' => '<legend>%s</legend>', 'legend' => '<legend>%s</legend>',
'css' => '<link rel="%s" type="text/css" href="%s" %s/>', 'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
'style' => '<style type="text/css"%s>%s</style>', 'style' => '<style type="text/css"%s>%s</style>',
'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />'); 'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />'
);
/** /**
* Base URL * Base URL
* *
@ -206,7 +209,8 @@ class HtmlHelper extends AppHelper {
if ($inline) { if ($inline) {
return $out; return $out;
} else { } else {
$this->view->addScript($out); $view =& ClassRegistry::getObject('_view_');
$view->addScript($out);
} }
} }
/** /**
@ -275,7 +279,8 @@ class HtmlHelper extends AppHelper {
if ($inline) { if ($inline) {
return $out; return $out;
} else { } else {
$this->view->addScript($out); $view =& ClassRegistry::getObject('_view_');
$view->addScript($out);
} }
} }
/** /**
@ -647,9 +652,11 @@ class HtmlHelper extends AppHelper {
* @param string $type FORM type (POST/GET). * @param string $type FORM type (POST/GET).
* @param array $htmlAttributes * @param array $htmlAttributes
* @return string An formatted opening FORM tag. * @return string An formatted opening FORM tag.
* @deprecated This is very WYSIWYG unfriendly, use HtmlHelper::url() to get contents of "action" attribute. Version 0.9.2. * @deprecated
* @see FormHelper::create
*/ */
function formTag($target = null, $type = 'post', $htmlAttributes = array()) { function formTag($target = null, $type = 'post', $htmlAttributes = array()) {
trigger_error('(HtmlHelper::formTag) Deprecated: Use FormHelper::create instead', E_USER_WARNING);
$htmlAttributes['action'] = $this->url($target); $htmlAttributes['action'] = $this->url($target);
$htmlAttributes['method'] = low($type) == 'get' ? 'get' : 'post'; $htmlAttributes['method'] = low($type) == 'get' ? 'get' : 'post';
$type == 'file' ? $htmlAttributes['enctype'] = 'multipart/form-data' : null; $type == 'file' ? $htmlAttributes['enctype'] = 'multipart/form-data' : null;
@ -691,9 +698,11 @@ class HtmlHelper extends AppHelper {
* @param string $email E-mail address if different from title. * @param string $email E-mail address if different from title.
* @param array $options * @param array $options
* @return string Formatted A tag * @return string Formatted A tag
* @deprecated This should be done using a content filter. Version 0.9.2. * @deprecated
* @see HtmlHelper::link
*/ */
function linkEmail($title, $email = null, $options = null) { function linkEmail($title, $email = null, $options = null) {
trigger_error('(HtmlHelper::linkEmail) Deprecated: Use HtmlHelper::link instead', E_USER_WARNING);
// if no $email, then title contains the email. // if no $email, then title contains the email.
if (empty($email)) { if (empty($email)) {
$email = $title; $email = $title;

View file

@ -136,7 +136,8 @@ class JavascriptHelper extends AppHelper {
if ($inline) { if ($inline) {
return $out; return $out;
} else { } else {
$this->view->addScript($out); $view =& ClassRegistry::getObject('_view_');
$view->addScript($out);
} }
} }
/** /**

View file

@ -124,8 +124,10 @@ class RssHelper extends XmlHelper {
* @return string An RSS <channel /> * @return string An RSS <channel />
*/ */
function channel($attrib = array(), $elements = array(), $content = null) { function channel($attrib = array(), $elements = array(), $content = null) {
if (!isset($elements['title']) && !empty($this->view->pageTitle)) { $view =& ClassRegistry::getObject('_view_');
$elements['title'] = $this->view->pageTitle;
if (!isset($elements['title']) && !empty($view->pageTitle)) {
$elements['title'] = $view->pageTitle;
} }
if (!isset($elements['link'])) { if (!isset($elements['link'])) {
$elements['link'] = '/'; $elements['link'] = '/';

View file

@ -36,6 +36,12 @@
* *
*/ */
class SessionHelper extends CakeSession { class SessionHelper extends CakeSession {
/**
* List of helpers used by this helper
*
* @var array
*/
var $helpers = null;
/** /**
* Used to determine if methods implementation is used, or bypassed * Used to determine if methods implementation is used, or bypassed
* *

View file

@ -261,15 +261,14 @@ class View extends Object {
*/ */
function __construct(&$controller) { function __construct(&$controller) {
if(is_object($controller)) { if(is_object($controller)) {
$this->controller =& $controller; $count = count($this->__passedVars);
for ($j = 0; $j < $count; $j++) {
$c = count($this->__passedVars);
for ($j = 0; $j < $c; $j++) {
$var = $this->__passedVars[$j]; $var = $this->__passedVars[$j];
$this->{$var} = $controller->{$var}; $this->{$var} = $controller->{$var};
} }
} }
parent::__construct(); parent::__construct();
ClassRegistry::addObject('_view_', $this);
} }
/** /**
@ -686,8 +685,9 @@ class View extends Object {
${$camelBackedHelper} =& $loadedHelpers[$helper]; ${$camelBackedHelper} =& $loadedHelpers[$helper];
if (isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) { if (is_array(${$camelBackedHelper}->helpers) && !empty(${$camelBackedHelper}->helpers)) {
foreach(${$camelBackedHelper}->helpers as $subHelper) { $subHelpers = ${$camelBackedHelper}->helpers;
foreach($subHelpers as $subHelper) {
${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper]; ${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper];
} }
} }
@ -781,9 +781,7 @@ class View extends Object {
} }
$camelBackedHelper = Inflector::variable($helper); $camelBackedHelper = Inflector::variable($helper);
${$camelBackedHelper} =& new $helperCn(); ${$camelBackedHelper} =& new $helperCn();
${$camelBackedHelper}->view =& $this;
$vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'themeWeb', 'plugin', 'namedArgs', 'argSeparator'); $vars = array('base', 'webroot', 'here', 'params', 'action', 'data', 'themeWeb', 'plugin', 'namedArgs', 'argSeparator');
$c = count($vars); $c = count($vars);
@ -797,7 +795,7 @@ class View extends Object {
$loaded[$helper] =& ${$camelBackedHelper}; $loaded[$helper] =& ${$camelBackedHelper};
if (isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) { if (is_array(${$camelBackedHelper}->helpers)) {
$loaded = &$this->_loadHelpers($loaded, ${$camelBackedHelper}->helpers); $loaded = &$this->_loadHelpers($loaded, ${$camelBackedHelper}->helpers);
} }
} }