Work on HTML builder.

This commit is contained in:
Taylor Otwell committed 2013-03-26 14:02:06 -05:00
1 parent daa9fbe5c5
commit 2a3bd6ffa8
3 files changed
+169 -27

No files matched your search

+20 -9
View File
@@ -6,6 +6,13 @@ use Illuminate\Session\Store as Session;
class FormBuilder { class FormBuilder {
/**
* The HTML builder instance.
*
* @var Illuminate\Html\HtmlBuilder
*/
protected $html;
/** /**
* The URL generator instance. * The URL generator instance.
* *
@@ -59,12 +66,14 @@ class FormBuilder {
* Create a new form builder instance. * Create a new form builder instance.
* *
* @param Illuminate\Routing\UrlGenerator $url * @param Illuminate\Routing\UrlGenerator $url
* @param Illuminate\Html\HtmlBuilder $html
* @param string $csrfToken * @param string $csrfToken
* @return void * @return void
*/ */
public function __construct(UrlGenerator $url, $csrfToken) public function __construct(HtmlBuilder $html, UrlGenerator $url, $csrfToken)
{ {
$this->url = $url; $this->url = $url;
$this->html = $html;
$this->csrfToken = $csrfToken; $this->csrfToken = $csrfToken;
} }
@@ -109,7 +118,7 @@ class FormBuilder {
// Finally, we will concatenate all of the attributes into a single string so // Finally, we will concatenate all of the attributes into a single string so
// we can build out the final form open statement. We'll also append on an // we can build out the final form open statement. We'll also append on an
// extra value for the hidden _method field if it's needed for the form. // extra value for the hidden _method field if it's needed for the form.
$attributes = Html::attributes($attributes); $attributes = $this->html->attributes($attributes);
return '<form'.$attributes.'>'.$append; return '<form'.$attributes.'>'.$append;
} }
@@ -164,7 +173,7 @@ class FormBuilder {
{ {
$this->labels[] = $name; $this->labels[] = $name;
$options = Html::attributes($options); $options = $this->html->attributes($options);
return '<label for="'.$name.'"'.$options.'>'.e($value).'</label>'; return '<label for="'.$name.'"'.$options.'>'.e($value).'</label>';
} }
@@ -196,7 +205,7 @@ class FormBuilder {
// when creating the HTML element. Then, we will return the entire input. // when creating the HTML element. Then, we will return the entire input.
$options = array_merge($options, $merge); $options = array_merge($options, $merge);
return '<input'.Html::attributes($options).'>'; return '<input'.$this->html->attributes($options).'>';
} }
/** /**
@@ -288,7 +297,7 @@ class FormBuilder {
// Next we will convert the attributes into a string form. Also we have removed // Next we will convert the attributes into a string form. Also we have removed
// the size attribute, as it was merely a short-cut for the rows and cols on // the size attribute, as it was merely a short-cut for the rows and cols on
// the element. Then we'll create the final textarea elements HTML for us. // the element. Then we'll create the final textarea elements HTML for us.
$options = Html::attributes($options); $options = $this->html->attributes($options);
return '<textarea'.$options.'>'.e($value).'</textarea>'; return '<textarea'.$options.'>'.e($value).'</textarea>';
} }
@@ -362,7 +371,7 @@ class FormBuilder {
// Once we have all of this HTML, we can join this into a single element after // Once we have all of this HTML, we can join this into a single element after
// formatting the attributes into an HTML "attributes" string, then we will // formatting the attributes into an HTML "attributes" string, then we will
// build out a final select statement, which will contain all the values. // build out a final select statement, which will contain all the values.
$options = Html::attributes($options); $options = $this->html->attributes($options);
$list = implode('', $html); $list = implode('', $html);
@@ -421,7 +430,7 @@ class FormBuilder {
$options = array('value' => e($value), 'selected' => $selected); $options = array('value' => e($value), 'selected' => $selected);
return '<option'.Html::attributes($options).'>'.e($display).'</option>'; return '<option'.$this->html->attributes($options).'>'.e($display).'</option>';
} }
/** /**
@@ -531,7 +540,7 @@ class FormBuilder {
$options['type'] = 'button'; $options['type'] = 'button';
} }
return '<button'.Html::attributes($options).'>'.e($value).'</button>'; return '<button'.$this->html->attributes($options).'>'.e($value).'</button>';
} }
/** /**
@@ -713,11 +722,13 @@ class FormBuilder {
* Set the session store implementation. * Set the session store implementation.
* *
* @param Illuminate\Session\Store $session * @param Illuminate\Session\Store $session
* @return void * @return Illuminate\Html\FormBuilder
*/ */
public function setSessionStore(Session $session) public function setSessionStore(Session $session)
{ {
$this->session = $session; $this->session = $session;
return $this;
} }
/** /**
+121 -13
View File
@@ -1,14 +1,34 @@
<?php namespace Illuminate\Html; <?php namespace Illuminate\Html;
use Illuminate\Routing\UrlGenerator;
class HtmlBuilder { class HtmlBuilder {
/**
* The URL generator instance.
*
* @var Illuminate\Routing\UrlGenerator
*/
protected $url;
/**
* Create a new HTML builder instance.
*
* @param Illuminate\Routing\UrlGenerator $url
* @return void
*/
public function __construct(UrlGenerator $url = null)
{
$this->url = $url;
}
/** /**
* Convert an HTML string to entities. * Convert an HTML string to entities.
* *
* @param string $value * @param string $value
* @return string * @return string
*/ */
public static function entities($value) public function entities($value)
{ {
return htmlentities($value, ENT_QUOTES, 'UTF-8', false); return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
} }
@@ -19,11 +39,99 @@ class HtmlBuilder {
* @param string $value * @param string $value
* @return string * @return string
*/ */
public static function decode($value) public function decode($value)
{ {
return html_entity_decode($value, ENT_QUOTES, 'UTF-8'); return html_entity_decode($value, ENT_QUOTES, 'UTF-8');
} }
/**
* Generate a HTML link.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $secure
* @return string
*/
public function link($url, $title = null, $attributes = array(), $secure = null)
{
$url = $this->url->to($url, $secure);
$title = $title ?: $url;
return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}
/**
* Generate a HTTPS HTML link.
*
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
public function secureLink($url, $title = null, $attributes = array())
{
return $this->link($url, $title, $attributes, true);
}
/**
* Generate a HTML link to an asset.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $secure
* @return string
*/
public function linkAsset($url, $title = null, $attributes = array(), $secure = null)
{
$url = $this->url->asset($url, $secure);
return $this->to($url, $title ?: $url, $attributes, $secure);
}
/**
* Generate a HTML link to an asset.
*
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
public function linkSecureAsset($url, $title = null, $attributes = array())
{
return $this->linkAsset($url, $title, $attributes, true);
}
/**
* Generate a HTML link to a named route.
*
* @param string $name
* @param string $title
* @param array $parameters
* @param array $attributes
* @return string
*/
public function linkRoute($name, $title = null, $parameters = array(), $attributes = array())
{
return $this->link($this->url->route($name, $parameters), $title, $attributes);
}
/**
* Generate a HTML link to a controller action.
*
* @param string $action
* @param string $title
* @param array $parameters
* @param array $attributes
* @return string
*/
public function linkAction($action, $title = null, $parameters = array(), $attributes = array())
{
return $this->link($this->url->action($action, $parameters), $title, $attributes);
}
/** /**
* Generate an ordered list of items. * Generate an ordered list of items.
* *
@@ -31,9 +139,9 @@ class HtmlBuilder {
* @param array $attributes * @param array $attributes
* @return string * @return string
*/ */
public static function ol($list, $attributes = array()) public function ol($list, $attributes = array())
{ {
return static::listing('ol', $list, $attributes); return $this->listing('ol', $list, $attributes);
} }
/** /**
@@ -43,9 +151,9 @@ class HtmlBuilder {
* @param array $attributes * @param array $attributes
* @return string * @return string
*/ */
public static function ul($list, $attributes = array()) public function ul($list, $attributes = array())
{ {
return static::listing('ul', $list, $attributes); return $this->listing('ul', $list, $attributes);
} }
/** /**
@@ -67,10 +175,10 @@ class HtmlBuilder {
// present in the array. Then we will build out the final listing elements. // present in the array. Then we will build out the final listing elements.
foreach ($list as $key => $value) foreach ($list as $key => $value)
{ {
$html .= static::listingElement($key, $type, $value); $html .= $this->listingElement($key, $type, $value);
} }
$attributes = static::attributes($attributes); $attributes = $this->attributes($attributes);
return "<{$type}{$attributes}>{$html}</{$type}>"; return "<{$type}{$attributes}>{$html}</{$type}>";
} }
@@ -87,7 +195,7 @@ class HtmlBuilder {
{ {
if (is_array($value)) if (is_array($value))
{ {
return static::nestedListing($key, $type, $value); return $this->nestedListing($key, $type, $value);
} }
else else
{ {
@@ -107,11 +215,11 @@ class HtmlBuilder {
{ {
if (is_int($key)) if (is_int($key))
{ {
return static::listing($type, $value); return $this->listing($type, $value);
} }
else else
{ {
return '<li>'.$key.static::listing($type, $value).'</li>'; return '<li>'.$key.$this->listing($type, $value).'</li>';
} }
} }
@@ -121,7 +229,7 @@ class HtmlBuilder {
* @param array $attributes * @param array $attributes
* @return string * @return string
*/ */
public static function attributes($attributes) public function attributes($attributes)
{ {
$html = array(); $html = array();
@@ -130,7 +238,7 @@ class HtmlBuilder {
// form like required="required" instead of using incorrect numerics. // form like required="required" instead of using incorrect numerics.
foreach ((array) $attributes as $key => $value) foreach ((array) $attributes as $key => $value)
{ {
$element = static::attributeElement($key, $value); $element = $this->attributeElement($key, $value);
if ( ! is_null($element)) $html[] = $element; if ( ! is_null($element)) $html[] = $element;
} }
+28 -5
View File
@@ -17,14 +17,37 @@ class HtmlServiceProvider extends ServiceProvider {
* @return void * @return void
*/ */
public function register() public function register()
{
$this->registerHtmlBuilder();
$this->registerFormBuilder();
}
/**
* Register the HTML builder instance.
*
* @return void
*/
protected function registerHtmlBuilder()
{
$this->app['html'] = $this->app->share(function($app)
{
return new HtmlBuilder($app['url']);
});
}
/**
* Register the form builder instance.
*
* @return void
*/
protected function registerFormBuilder()
{ {
$this->app['form'] = $this->app->share(function($app) $this->app['form'] = $this->app->share(function($app)
{ {
$form = new FormBuilder($app['url'], $app['session']->getToken()); $form = new FormBuilder($app['html'], $app['url'], $app['session']->getToken());
$form->setSessionStore($app['session']); return $form->setSessionStore($app['session']);
return $form;
}); });
} }
@@ -35,7 +58,7 @@ class HtmlServiceProvider extends ServiceProvider {
*/ */
public function provides() public function provides()
{ {
return array('form'); return array('html', 'form');
} }
} }