From 2a3bd6ffa8120b37b6121a15a48769db261960d8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 26 Mar 2013 14:02:06 -0500 Subject: [PATCH] Work on HTML builder. --- FormBuilder.php | 29 ++++++--- HtmlBuilder.php | 134 ++++++++++++++++++++++++++++++++++++---- HtmlServiceProvider.php | 33 ++++++++-- 3 files changed, 169 insertions(+), 27 deletions(-) diff --git a/FormBuilder.php b/FormBuilder.php index 85dde67..0d60238 100644 --- a/FormBuilder.php +++ b/FormBuilder.php @@ -6,6 +6,13 @@ use Illuminate\Session\Store as Session; class FormBuilder { + /** + * The HTML builder instance. + * + * @var Illuminate\Html\HtmlBuilder + */ + protected $html; + /** * The URL generator instance. * @@ -59,12 +66,14 @@ class FormBuilder { * Create a new form builder instance. * * @param Illuminate\Routing\UrlGenerator $url + * @param Illuminate\Html\HtmlBuilder $html * @param string $csrfToken * @return void */ - public function __construct(UrlGenerator $url, $csrfToken) + public function __construct(HtmlBuilder $html, UrlGenerator $url, $csrfToken) { $this->url = $url; + $this->html = $html; $this->csrfToken = $csrfToken; } @@ -109,7 +118,7 @@ class FormBuilder { // 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 // extra value for the hidden _method field if it's needed for the form. - $attributes = Html::attributes($attributes); + $attributes = $this->html->attributes($attributes); return ''.$append; } @@ -164,7 +173,7 @@ class FormBuilder { { $this->labels[] = $name; - $options = Html::attributes($options); + $options = $this->html->attributes($options); return ''; } @@ -196,7 +205,7 @@ class FormBuilder { // when creating the HTML element. Then, we will return the entire input. $options = array_merge($options, $merge); - return ''; + return 'html->attributes($options).'>'; } /** @@ -288,7 +297,7 @@ class FormBuilder { // 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 element. Then we'll create the final textarea elements HTML for us. - $options = Html::attributes($options); + $options = $this->html->attributes($options); return ''.e($value).''; } @@ -362,7 +371,7 @@ class FormBuilder { // 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 // build out a final select statement, which will contain all the values. - $options = Html::attributes($options); + $options = $this->html->attributes($options); $list = implode('', $html); @@ -421,7 +430,7 @@ class FormBuilder { $options = array('value' => e($value), 'selected' => $selected); - return ''.e($display).''; + return 'html->attributes($options).'>'.e($display).''; } /** @@ -531,7 +540,7 @@ class FormBuilder { $options['type'] = 'button'; } - return ''.e($value).''; + return 'html->attributes($options).'>'.e($value).''; } /** @@ -713,11 +722,13 @@ class FormBuilder { * Set the session store implementation. * * @param Illuminate\Session\Store $session - * @return void + * @return Illuminate\Html\FormBuilder */ public function setSessionStore(Session $session) { $this->session = $session; + + return $this; } /** diff --git a/HtmlBuilder.php b/HtmlBuilder.php index adbae24..ab4f4aa 100644 --- a/HtmlBuilder.php +++ b/HtmlBuilder.php @@ -1,14 +1,34 @@ url = $url; + } + /** * Convert an HTML string to entities. * * @param string $value * @return string */ - public static function entities($value) + public function entities($value) { return htmlentities($value, ENT_QUOTES, 'UTF-8', false); } @@ -19,11 +39,99 @@ class HtmlBuilder { * @param string $value * @return string */ - public static function decode($value) + public function decode($value) { 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 'attributes($attributes).'>'.$this->entities($title).''; + } + + /** + * 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. * @@ -31,9 +139,9 @@ class HtmlBuilder { * @param array $attributes * @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 * @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. 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}"; } @@ -87,7 +195,7 @@ class HtmlBuilder { { if (is_array($value)) { - return static::nestedListing($key, $type, $value); + return $this->nestedListing($key, $type, $value); } else { @@ -107,11 +215,11 @@ class HtmlBuilder { { if (is_int($key)) { - return static::listing($type, $value); + return $this->listing($type, $value); } else { - return '
  • '.$key.static::listing($type, $value).'
  • '; + return '
  • '.$key.$this->listing($type, $value).'
  • '; } } @@ -121,7 +229,7 @@ class HtmlBuilder { * @param array $attributes * @return string */ - public static function attributes($attributes) + public function attributes($attributes) { $html = array(); @@ -130,7 +238,7 @@ class HtmlBuilder { // form like required="required" instead of using incorrect numerics. foreach ((array) $attributes as $key => $value) { - $element = static::attributeElement($key, $value); + $element = $this->attributeElement($key, $value); if ( ! is_null($element)) $html[] = $element; } diff --git a/HtmlServiceProvider.php b/HtmlServiceProvider.php index 9842506..4082e08 100644 --- a/HtmlServiceProvider.php +++ b/HtmlServiceProvider.php @@ -17,14 +17,37 @@ class HtmlServiceProvider extends ServiceProvider { * @return void */ 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) { - $form = new FormBuilder($app['url'], $app['session']->getToken()); + $form = new FormBuilder($app['html'], $app['url'], $app['session']->getToken()); - $form->setSessionStore($app['session']); - - return $form; + return $form->setSessionStore($app['session']); }); } @@ -35,7 +58,7 @@ class HtmlServiceProvider extends ServiceProvider { */ public function provides() { - return array('form'); + return array('html', 'form'); } } \ No newline at end of file