Browse Source

Finished Html helper.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
30d70be071
  1. 13
      framework/base/Application.php
  2. 16
      framework/db/ActiveRelation.php
  3. 40
      framework/util/ArrayHelper.php
  4. 1308
      framework/util/Html.php
  5. 47
      framework/web/Application.php
  6. 4
      framework/web/Controller.php
  7. 39
      framework/web/Request.php
  8. 1
      todo.md

13
framework/base/Application.php

@ -382,6 +382,15 @@ class Application extends Module
}
/**
* Returns the URL manager for this application.
* @return \yii\web\UrlManager the URL manager for this application.
*/
public function getUrlManager()
{
return $this->getComponent('urlManager');
}
/**
* Returns the internationalization (i18n) component
* @return \yii\i18n\I18N the internationalization component
*/
@ -411,8 +420,8 @@ class Application extends Module
'i18n' => array(
'class' => 'yii\i18n\I18N',
),
'securityManager' => array(
'class' => 'yii\base\SecurityManager',
'urlManager' => array(
'class' => 'yii\web\UrlManager',
),
));
}

16
framework/db/ActiveRelation.php

@ -55,16 +55,16 @@ class ActiveRelation extends ActiveQuery
/**
* Specifies the relation associated with the pivot table.
* @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]].
* @param callback $callback a PHP callback for customizing the relation associated with the pivot table.
* @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
* Its signature should be `function($query)`, where `$query` is the query to be customized.
* @return ActiveRelation the relation object itself.
*/
public function via($relationName, $callback = null)
public function via($relationName, $callable = null)
{
$relation = $this->primaryModel->getRelation($relationName);
$this->via = array($relationName, $relation);
if ($callback !== null) {
call_user_func($callback, $relation);
if ($callable !== null) {
call_user_func($callable, $relation);
}
return $this;
}
@ -75,11 +75,11 @@ class ActiveRelation extends ActiveQuery
* @param array $link the link between the pivot table and the table associated with [[primaryModel]].
* The keys of the array represent the columns in the pivot table, and the values represent the columns
* in the [[primaryModel]] table.
* @param callback $callback a PHP callback for customizing the relation associated with the pivot table.
* @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
* Its signature should be `function($query)`, where `$query` is the query to be customized.
* @return ActiveRelation
*/
public function viaTable($tableName, $link, $callback = null)
public function viaTable($tableName, $link, $callable = null)
{
$relation = new ActiveRelation(array(
'modelClass' => get_class($this->primaryModel),
@ -89,8 +89,8 @@ class ActiveRelation extends ActiveQuery
'asArray' => true,
));
$this->via = $relation;
if ($callback !== null) {
call_user_func($callback, $relation);
if ($callable !== null) {
call_user_func($callable, $relation);
}
return $this;
}

40
framework/util/ArrayHelper.php

@ -281,33 +281,59 @@ class ArrayHelper
call_user_func_array('array_multisort', $args);
}
/**
* Encodes special characters in an array of strings into HTML entities.
* Both the array keys and values will be encoded if needed.
* Both the array keys and values will be encoded.
* If a value is an array, this method will also encode it recursively.
* @param array $data data to be encoded
* @param boolean $valuesOnly whether to encode array values only. If false,
* both the array keys and array values will be encoded.
* @param string $charset the charset that the data is using. If not set,
* [[\yii\base\Application::charset]] will be used.
* @return array the encoded data
* @see http://www.php.net/manual/en/function.htmlspecialchars.php
*/
public static function htmlEncode($data, $charset = null)
public static function htmlEncode($data, $valuesOnly = false, $charset = null)
{
if ($charset === null) {
$charset = Yii::$app->charset;
}
$d = array();
foreach ($data as $key => $value) {
if (is_string($key)) {
if (!$valuesOnly && is_string($key)) {
$key = htmlspecialchars($key, ENT_QUOTES, $charset);
}
if (is_string($value)) {
$value = htmlspecialchars($value, ENT_QUOTES, $charset);
$d[$key] = htmlspecialchars($value, ENT_QUOTES, $charset);
} elseif (is_array($value)) {
$d[$key] = static::htmlEncode($value, $charset);
}
}
return $d;
}
/**
* Decodes HTML entities into the corresponding characters in an array of strings.
* Both the array keys and values will be decoded.
* If a value is an array, this method will also decode it recursively.
* @param array $data data to be decoded
* @param boolean $valuesOnly whether to decode array values only. If false,
* both the array keys and array values will be decoded.
* @return array the decoded data
* @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
*/
public static function htmlDecode($data, $valuesOnly = false)
{
$d = array();
foreach ($data as $key => $value) {
if (!$valuesOnly && is_string($key)) {
$key = htmlspecialchars_decode($key, ENT_QUOTES);
}
if (is_string($value)) {
$d[$key] = htmlspecialchars_decode($value, ENT_QUOTES);
} elseif (is_array($value)) {
$value = static::htmlEncode($value);
$d[$key] = static::htmlDecode($value);
}
$d[$key] = $value;
}
return $d;
}

1308
framework/util/Html.php

File diff suppressed because it is too large Load Diff

47
framework/web/Application.php

@ -6,6 +6,7 @@
*/
namespace yii\web;
use yii\base\InvalidParamException;
/**
* Application is the base class for all application classes.
@ -62,11 +63,48 @@ class Application extends \yii\base\Application
}
/**
* @return UrlManager
* Creates a URL using the given route and parameters.
*
* This method first normalizes the given route by converting a relative route into an absolute one.
* A relative route is a route without slash. If the route is an empty string, it stands for
* the route of the currently active [[controller]]. If the route is not empty, it stands for
* an action ID of the [[controller]].
*
* After normalizing the route, this method calls [[\yii\web\UrlManager::createUrl()]]
* to create a relative URL.
*
* @param string $route the route. This can be either an absolute or a relative route.
* @param array $params the parameters (name-value pairs) to be included in the generated URL
* @return string the created URL
* @throws InvalidParamException if a relative route is given and there is no active controller.
* @see createAbsoluteUrl
*/
public function getUrlManager()
public function createUrl($route, $params = array())
{
return $this->getComponent('urlManager');
if (strpos($route, '/') === false) {
// a relative route
if ($this->controller !== null) {
$route = $route === '' ? $this->controller->route : $this->controller->uniqueId . '/' . $route;
} else {
throw new InvalidParamException('No active controller exists for resolving a relative route.');
}
}
return $this->getUrlManager()->createUrl($route, $params);
}
/**
* Creates an absolute URL using the given route and parameters.
* This method first calls [[createUrl()]] to create a relative URL.
* It then prepends [[\yii\web\UrlManager::hostInfo]] to the URL to form an absolute one.
* @param string $route the route. This can be either an absolute or a relative route.
* See [[createUrl()]] for more details.
* @param array $params the parameters (name-value pairs)
* @return string the created URL
* @see createUrl
*/
public function createAbsoluteUrl($route, $params = array())
{
return $this->getUrlManager()->getHostInfo() . $this->createUrl($route, $params);
}
/**
@ -86,9 +124,6 @@ class Application extends \yii\base\Application
'session' => array(
'class' => 'yii\web\Session',
),
'urlManager' => array(
'class' => 'yii\web\UrlManager',
),
));
}
}

4
framework/web/Controller.php

@ -16,4 +16,8 @@ namespace yii\web;
*/
class Controller extends \yii\base\Controller
{
public function createUrl($route, $params = array())
{
}
}

39
framework/web/Request.php

@ -368,7 +368,7 @@ class Request extends \yii\base\Request
*/
protected function resolvePathInfo()
{
$pathInfo = $this->getRequestUri();
$pathInfo = $this->getUrl();
if (($pos = strpos($pathInfo, '?')) !== false) {
$pathInfo = substr($pathInfo, 0, $pos);
@ -407,42 +407,41 @@ class Request extends \yii\base\Request
}
/**
* Returns the currently requested URL.
* This is a shortcut to the concatenation of [[hostInfo]] and [[requestUri]].
* @return string the currently requested URL.
* Returns the currently requested absolute URL.
* This is a shortcut to the concatenation of [[hostInfo]] and [[url]].
* @return string the currently requested absolute URL.
*/
public function getUrl()
public function getAbsoluteUrl()
{
return $this->getHostInfo() . $this->getRequestUri();
return $this->getHostInfo() . $this->getUrl();
}
private $_requestUri;
private $_url;
/**
* Returns the portion after [[hostInfo]] for the currently requested URL.
* This refers to the portion that is after the [[hostInfo]] part. It includes the [[queryString]] part if any.
* The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework.
* @return string the request URI portion for the currently requested URL.
* Note that the URI returned is URL-encoded.
* @throws InvalidConfigException if the request URI cannot be determined due to unusual server configuration
* Returns the currently requested relative URL.
* This refers to the portion of the URL that is after the [[hostInfo]] part.
* It includes the [[queryString]] part if any.
* @return string the currently requested relative URL. Note that the URI returned is URL-encoded.
* @throws InvalidConfigException if the URL cannot be determined due to unusual server configuration
*/
public function getRequestUri()
public function getUrl()
{
if ($this->_requestUri === null) {
$this->_requestUri = $this->resolveRequestUri();
if ($this->_url === null) {
$this->_url = $this->resolveRequestUri();
}
return $this->_requestUri;
return $this->_url;
}
/**
* Sets the currently requested URI.
* Sets the currently requested relative URL.
* The URI must refer to the portion that is after [[hostInfo]].
* Note that the URI should be URL-encoded.
* @param string $value the request URI to be set
*/
public function setRequestUri($value)
public function setUrl($value)
{
$this->_requestUri = $value;
$this->_url = $value;
}
/**

1
todo.md

@ -18,6 +18,7 @@
* backend-specific unit tests
* dependency unit tests
- validators
* Refactor validators to add validateValue() for every validator, if possible. Check if value is an array.
* FileValidator: depends on CUploadedFile
* CaptchaValidator: depends on CaptchaAction
* DateValidator: should we use CDateTimeParser, or simply use strtotime()?

Loading…
Cancel
Save