diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php
index 7c4de59..53a3a98 100755
--- a/src/HtmlBuilder.php
+++ b/src/HtmlBuilder.php
@@ -2,12 +2,16 @@
namespace Collective\Html;
-use Illuminate\Contracts\Routing\UrlGenerator;
+use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Traits\Macroable;
+use Illuminate\Contracts\Routing\UrlGenerator;
class HtmlBuilder
{
- use Macroable;
+
+ use Macroable {
+ __call as callMacro;
+ }
/**
* The URL generator instance.
@@ -16,16 +20,30 @@ class HtmlBuilder
*/
protected $url;
+ /**
+ * The View Factory instance.
+ *
+ * @var \Illuminate\Contracts\View\Factory
+ */
+ private $view;
+
+ /**
+ * The registered components.
+ *
+ * @var array
+ */
+ protected static $components = [];
+
/**
* Create a new HTML builder instance.
*
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
- *
- * @return void
+ * @param \Illuminate\Contracts\View\Factory $view
*/
- public function __construct(UrlGenerator $url = null)
+ public function __construct(UrlGenerator $url = null, Factory $view)
{
$this->url = $url;
+ $this->view = $view;
}
/**
@@ -65,7 +83,7 @@ class HtmlBuilder
{
$attributes['src'] = $this->url->asset($url, $secure);
- return ''.PHP_EOL;
+ return '' . PHP_EOL;
}
/**
@@ -85,7 +103,7 @@ class HtmlBuilder
$attributes['href'] = $this->url->asset($url, $secure);
- return 'attributes($attributes).'>'.PHP_EOL;
+ return 'attributes($attributes) . '>' . PHP_EOL;
}
/**
@@ -102,7 +120,7 @@ class HtmlBuilder
{
$attributes['alt'] = $alt;
- return '
attributes($attributes).'>';
+ return '
attributes($attributes) . '>';
}
/**
@@ -122,7 +140,7 @@ class HtmlBuilder
$attributes['href'] = $this->url->asset($url, $secure);
- return 'attributes($attributes).'>'.PHP_EOL;
+ return 'attributes($attributes) . '>' . PHP_EOL;
}
/**
@@ -143,7 +161,7 @@ class HtmlBuilder
$title = $url;
}
- return 'attributes($attributes).'>'.$this->entities($title).'';
+ return 'attributes($attributes) . '>' . $this->entities($title) . '';
}
/**
@@ -236,9 +254,9 @@ class HtmlBuilder
$title = $title ?: $email;
- $email = $this->obfuscate('mailto:').$email;
+ $email = $this->obfuscate('mailto:') . $email;
- return 'attributes($attributes).'>'.$this->entities($title).'';
+ return 'attributes($attributes) . '>' . $this->entities($title) . '';
}
/**
@@ -295,9 +313,9 @@ class HtmlBuilder
foreach ($list as $key => $value) {
$value = (array) $value;
-
+
$html .= "
$key";
-
+
foreach ($value as $v_key => $v_value) {
$html .= "$v_value";
}
@@ -351,7 +369,7 @@ class HtmlBuilder
if (is_array($value)) {
return $this->nestedListing($key, $type, $value);
} else {
- return ''.e($value).'';
+ return '' . e($value) . '';
}
}
@@ -369,7 +387,7 @@ class HtmlBuilder
if (is_int($key)) {
return $this->listing($type, $value);
} else {
- return ''.$key.$this->listing($type, $value).'';
+ return '' . $key . $this->listing($type, $value) . '';
}
}
@@ -387,12 +405,12 @@ class HtmlBuilder
foreach ((array) $attributes as $key => $value) {
$element = $this->attributeElement($key, $value);
- if (!is_null($element)) {
+ if ( ! is_null($element)) {
$html[] = $element;
}
}
- return count($html) > 0 ? ' '.implode(' ', $html) : '';
+ return count($html) > 0 ? ' ' . implode(' ', $html) : '';
}
/**
@@ -412,8 +430,8 @@ class HtmlBuilder
$key = $value;
}
- if (!is_null($value)) {
- return $key.'="'.e($value).'"';
+ if ( ! is_null($value)) {
+ return $key . '="' . e($value) . '"';
}
}
@@ -438,10 +456,12 @@ class HtmlBuilder
// the randomly obfuscated letters out of the string on the responses.
switch (rand(1, 3)) {
case 1:
- $safe .= ''.ord($letter).';'; break;
+ $safe .= '' . ord($letter) . ';';
+ break;
case 2:
- $safe .= ''.dechex(ord($letter)).';'; break;
+ $safe .= '' . dechex(ord($letter)) . ';';
+ break;
case 3:
$safe .= $letter;
@@ -466,6 +486,62 @@ class HtmlBuilder
$attributes = array_merge($defaults, $attributes);
- return 'attributes($attributes).'>'.PHP_EOL;
+ return 'attributes($attributes) . '>' . PHP_EOL;
+ }
+
+ /**
+ * Register a custom component.
+ *
+ * @param $name
+ * @param $view
+ * @param array $signature
+ * @return void
+ */
+ public static function component($name, $view, array $signature)
+ {
+ static::$components[$name] = compact('view', 'signature');
+ }
+
+ /**
+ * Check if a component is registered.
+ *
+ * @param $name
+ * @return bool
+ */
+ public static function hasComponent($name)
+ {
+ return isset(static::$components[$name]);
+ }
+
+ /**
+ * Render a custom component.
+ *
+ * @param $name
+ * @param array $arguments
+ * @return string
+ */
+ public function renderComponent($name, array $arguments)
+ {
+ $component = static::$components[$name];
+
+ return $this->view->make($component['view'], array_combine($component['signature'], $arguments))->render();
+ }
+
+ /**
+ * Dynamically handle calls to the class.
+ *
+ * @param $method
+ * @param $parameters
+ * @return mixed|string
+ *
+ * @throws \BadMethodCallException
+ */
+ public function __call($method, $parameters)
+ {
+ if (static::hasComponent($method)) {
+ return $this->renderComponent($method, $parameters);
+ }
+
+ return $this->callMacro($method, $parameters);
}
}
diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php
index 12c9608..2d11ce5 100755
--- a/src/HtmlServiceProvider.php
+++ b/src/HtmlServiceProvider.php
@@ -36,7 +36,7 @@ class HtmlServiceProvider extends ServiceProvider
protected function registerHtmlBuilder()
{
$this->app->singleton('html', function ($app) {
- return new HtmlBuilder($app['url']);
+ return new HtmlBuilder($app['url'], $app['view']);
});
}
diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php
index 9ee220c..ce5deed 100644
--- a/tests/FormBuilderTest.php
+++ b/tests/FormBuilderTest.php
@@ -2,6 +2,7 @@
use Collective\Html\FormBuilder;
use Collective\Html\HtmlBuilder;
+use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Routing\RouteCollection;
use Illuminate\Routing\UrlGenerator;
@@ -16,7 +17,8 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
public function setUp()
{
$this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET'));
- $this->htmlBuilder = new HtmlBuilder($this->urlGenerator);
+ $this->viewFactory = m::mock(Factory::class);
+ $this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory);
$this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, 'abc');
}
diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php
index 274095a..10283f0 100644
--- a/tests/HtmlBuilderTest.php
+++ b/tests/HtmlBuilderTest.php
@@ -1,26 +1,35 @@
urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET'));
- $this->htmlBuilder = new HtmlBuilder($this->urlGenerator);
+ $this->viewFactory = m::mock(Factory::class);
+ $this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory);
+ }
+
+ public function tearDown()
+ {
+ m::close();
}
public function testDl()
{
$list = [
- 'foo' => 'bar',
- 'bing' => 'baz',
+ 'foo' => 'bar',
+ 'bing' => 'baz',
];
$attributes = ['class' => 'example'];
@@ -34,14 +43,14 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
{
$result = $this->htmlBuilder->meta('description', 'Lorem ipsum dolor sit amet.');
- $this->assertEquals(''.PHP_EOL, $result);
+ $this->assertEquals('' . PHP_EOL, $result);
}
public function testMetaOpenGraph()
{
$result = $this->htmlBuilder->meta(null, 'website', ['property' => 'og:type']);
- $this->assertEquals(''.PHP_EOL, $result);
+ $this->assertEquals('' . PHP_EOL, $result);
}
public function testFavicon()
@@ -50,6 +59,13 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
$target = $this->urlGenerator->to('bar.ico');
$result = $this->htmlBuilder->favicon('http://foo.com/bar.ico');
- $this->assertEquals(''.PHP_EOL, $result);
+ $this->assertEquals('' . PHP_EOL, $result);
}
-}
+
+ public function testComponentRegistration()
+ {
+ $this->htmlBuilder->component('tweet', 'components.tweet', ['handle', 'body', 'date']);
+
+ $this->assertTrue($this->htmlBuilder->hasComponent('tweet'));
+ }
+}
\ No newline at end of file