continued ioc refactoring.

This commit is contained in:
Taylor Otwell
2011-08-26 21:42:04 -05:00
parent fb3a0df0dd
commit 1e49001dfc
44 changed files with 1388 additions and 1046 deletions

View File

@@ -2,6 +2,32 @@
class HTML {
/**
* The encoding being used by the application.
*
* @var string
*/
private $encoding;
/**
* The URL generator instance.
*
* @var URL
*/
private $url;
/**
* Create a new HTML writer instance.
*
* @param string $encoding
* @return void
*/
public function __construct(URL $url, $encoding)
{
$this->url = $url;
$this->encoding = $encoding;
}
/**
* Convert HTML characters to entities.
*
@@ -10,9 +36,9 @@ class HTML {
* @param string $value
* @return string
*/
public static function entities($value)
public function entities($value)
{
return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
return htmlentities($value, ENT_QUOTES, $this->encoding, false);
}
/**
@@ -22,11 +48,11 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function script($url, $attributes = array())
public function script($url, $attributes = array())
{
$url = static::entities(URL::to_asset($url));
$url = $this->entities($this->url->to_asset($url));
return '<script type="text/javascript" src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
return '<script type="text/javascript" src="'.$url.'"'.$this->attributes($attributes).'></script>'.PHP_EOL;
}
/**
@@ -36,13 +62,13 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function style($url, $attributes = array())
public function style($url, $attributes = array())
{
if ( ! array_key_exists('media', $attributes)) $attributes['media'] = 'all';
$attributes = array_merge($attributes, array('rel' => 'stylesheet', 'type' => 'text/css'));
return '<link href="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>'.PHP_EOL;
return '<link href="'.$this->entities($this->url->to_asset($url)).'"'.$this->attributes($attributes).'>'.PHP_EOL;
}
/**
@@ -52,9 +78,9 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function span($value, $attributes = array())
public function span($value, $attributes = array())
{
return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
return '<span'.$this->attributes($attributes).'>'.$this->entities($value).'</span>';
}
/**
@@ -67,11 +93,11 @@ class HTML {
* @param bool $asset
* @return string
*/
public static function link($url, $title, $attributes = array(), $https = false, $asset = false)
public function link($url, $title, $attributes = array(), $https = false, $asset = false)
{
$url = static::entities(URL::to($url, $https, $asset));
$url = $this->entities($this->url->to($url, $https, $asset));
return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}
/**
@@ -82,9 +108,9 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function link_to_secure($url, $title, $attributes = array())
public function link_to_secure($url, $title, $attributes = array())
{
return static::link($url, $title, $attributes, true);
return $this->link($url, $title, $attributes, true);
}
/**
@@ -95,9 +121,9 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function link_to_asset($url, $title, $attributes = array(), $https = false)
public function link_to_asset($url, $title, $attributes = array(), $https = false)
{
return static::link($url, $title, $attributes, $https, true);
return $this->link($url, $title, $attributes, $https, true);
}
/**
@@ -108,9 +134,9 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function link_to_secure_asset($url, $title, $attributes = array())
public function link_to_secure_asset($url, $title, $attributes = array())
{
return static::link_to_asset($url, $title, $attributes, true);
return $this->link_to_asset($url, $title, $attributes, true);
}
/**
@@ -132,9 +158,9 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false)
public function link_to_route($name, $title, $parameters = array(), $attributes = array(), $https = false)
{
return static::link(URL::to_route($name, $parameters, $https), $title, $attributes);
return $this->link($this->url->to_route($name, $parameters, $https), $title, $attributes);
}
/**
@@ -146,9 +172,9 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function link_to_secure_route($name, $title, $parameters = array(), $attributes = array())
public function link_to_secure_route($name, $title, $parameters = array(), $attributes = array())
{
return static::link_to_route($name, $title, $parameters, $attributes, true);
return $this->link_to_route($name, $title, $parameters, $attributes, true);
}
/**
@@ -161,15 +187,15 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function mailto($email, $title = null, $attributes = array())
public function mailto($email, $title = null, $attributes = array())
{
$email = static::email($email);
$email = $this->email($email);
if (is_null($title)) $title = $email;
$email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;
return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
return '<a href="'.$email.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}
/**
@@ -178,9 +204,9 @@ class HTML {
* @param string $email
* @return string
*/
public static function email($email)
public function email($email)
{
return str_replace('@', '&#64;', static::obfuscate($email));
return str_replace('@', '&#64;', $this->obfuscate($email));
}
/**
@@ -191,11 +217,11 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function image($url, $alt = '', $attributes = array())
public function image($url, $alt = '', $attributes = array())
{
$attributes['alt'] = static::entities($alt);
$attributes['alt'] = $this->entities($alt);
return '<img src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>';
return '<img src="'.$this->entities($this->url->to_asset($url)).'"'.$this->attributes($attributes).'>';
}
/**
@@ -205,9 +231,9 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function ol($list, $attributes = array())
public function ol($list, $attributes = array())
{
return static::list_elements('ol', $list, $attributes);
return $this->list_elements('ol', $list, $attributes);
}
/**
@@ -217,9 +243,9 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function ul($list, $attributes = array())
public function ul($list, $attributes = array())
{
return static::list_elements('ul', $list, $attributes);
return $this->list_elements('ul', $list, $attributes);
}
/**
@@ -230,16 +256,16 @@ class HTML {
* @param array $attributes
* @return string
*/
private static function list_elements($type, $list, $attributes = array())
private function list_elements($type, $list, $attributes = array())
{
$html = '';
foreach ($list as $key => $value)
{
$html .= (is_array($value)) ? static::list_elements($type, $value) : '<li>'.static::entities($value).'</li>';
$html .= (is_array($value)) ? $this->list_elements($type, $value) : '<li>'.$this->entities($value).'</li>';
}
return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
return '<'.$type.$this->attributes($attributes).'>'.$html.'</'.$type.'>';
}
/**
@@ -248,7 +274,7 @@ class HTML {
* @param array $attributes
* @return string
*/
public static function attributes($attributes)
public function attributes($attributes)
{
$html = array();
@@ -260,7 +286,7 @@ class HTML {
if ( ! is_null($value))
{
$html[] = $key.'="'.static::entities($value).'"';
$html[] = $key.'="'.$this->entities($value).'"';
}
}
@@ -273,7 +299,7 @@ class HTML {
* @param string $value
* @return string
*/
public static function obfuscate($value)
public function obfuscate($value)
{
$safe = '';
@@ -313,23 +339,23 @@ class HTML {
* echo HTML::link_to_profile('Profile', array('taylor'));
* </code>
*/
public static function __callStatic($method, $parameters)
public function __call($method, $parameters)
{
if (strpos($method, 'link_to_secure_') === 0)
{
array_unshift($parameters, substr($method, 15));
return forward_static_call_array('HTML::link_to_secure_route', $parameters);
return call_user_func_array(array($this, 'link_to_secure_route'), $parameters);
}
if (strpos($method, 'link_to_') === 0)
{
array_unshift($parameters, substr($method, 8));
return forward_static_call_array('HTML::link_to_route', $parameters);
return call_user_func_array(array($this, 'link_to_route'), $parameters);
}
throw new \Exception("Static method [$method] is not defined on the HTML class.");
throw new \Exception("Method [$method] is not defined on the HTML class.");
}
}