Starting Html Components
Signed-off-by: Adam Engebretson <adam.engebretson@bridgeconx.com>
This commit is contained in:
1 parent
46409f294e
commit
d2b7e24d72
4 files changed
+104
-10
No files matched your search
+83
-7
@@ -2,12 +2,16 @@
|
|||||||
|
|
||||||
namespace Collective\Html;
|
namespace Collective\Html;
|
||||||
|
|
||||||
use Illuminate\Contracts\Routing\UrlGenerator;
|
use Illuminate\Contracts\View\Factory;
|
||||||
use Illuminate\Support\Traits\Macroable;
|
use Illuminate\Support\Traits\Macroable;
|
||||||
|
use Illuminate\Contracts\Routing\UrlGenerator;
|
||||||
|
|
||||||
class HtmlBuilder
|
class HtmlBuilder
|
||||||
{
|
{
|
||||||
use Macroable;
|
|
||||||
|
use Macroable {
|
||||||
|
__call as callMacro;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The URL generator instance.
|
* The URL generator instance.
|
||||||
@@ -16,16 +20,30 @@ class HtmlBuilder
|
|||||||
*/
|
*/
|
||||||
protected $url;
|
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.
|
* Create a new HTML builder instance.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
|
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
|
||||||
*
|
* @param \Illuminate\Contracts\View\Factory $view
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function __construct(UrlGenerator $url = null)
|
public function __construct(UrlGenerator $url = null, Factory $view)
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
|
$this->view = $view;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -438,10 +456,12 @@ class HtmlBuilder
|
|||||||
// the randomly obfuscated letters out of the string on the responses.
|
// the randomly obfuscated letters out of the string on the responses.
|
||||||
switch (rand(1, 3)) {
|
switch (rand(1, 3)) {
|
||||||
case 1:
|
case 1:
|
||||||
$safe .= '&#'.ord($letter).';'; break;
|
$safe .= '&#' . ord($letter) . ';';
|
||||||
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
$safe .= '&#x'.dechex(ord($letter)).';'; break;
|
$safe .= '&#x' . dechex(ord($letter)) . ';';
|
||||||
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
$safe .= $letter;
|
$safe .= $letter;
|
||||||
@@ -468,4 +488,60 @@ class HtmlBuilder
|
|||||||
|
|
||||||
return '<meta' . $this->attributes($attributes) . '>' . PHP_EOL;
|
return '<meta' . $this->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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ class HtmlServiceProvider extends ServiceProvider
|
|||||||
protected function registerHtmlBuilder()
|
protected function registerHtmlBuilder()
|
||||||
{
|
{
|
||||||
$this->app->singleton('html', function ($app) {
|
$this->app->singleton('html', function ($app) {
|
||||||
return new HtmlBuilder($app['url']);
|
return new HtmlBuilder($app['url'], $app['view']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use Collective\Html\FormBuilder;
|
use Collective\Html\FormBuilder;
|
||||||
use Collective\Html\HtmlBuilder;
|
use Collective\Html\HtmlBuilder;
|
||||||
|
use Illuminate\Contracts\View\Factory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Routing\RouteCollection;
|
use Illuminate\Routing\RouteCollection;
|
||||||
use Illuminate\Routing\UrlGenerator;
|
use Illuminate\Routing\UrlGenerator;
|
||||||
@@ -16,7 +17,8 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET'));
|
$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');
|
$this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, 'abc');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,28 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Contracts\View\Factory;
|
||||||
use Collective\Html\HtmlBuilder;
|
use Collective\Html\HtmlBuilder;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Routing\RouteCollection;
|
use Illuminate\Routing\RouteCollection;
|
||||||
use Illuminate\Routing\UrlGenerator;
|
use Illuminate\Routing\UrlGenerator;
|
||||||
|
use Mockery as m;
|
||||||
|
|
||||||
class HtmlBuilderTest extends PHPUnit_Framework_TestCase
|
class HtmlBuilderTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup the test environment.
|
* Setup the test environment.
|
||||||
*/
|
*/
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET'));
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
m::close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDl()
|
public function testDl()
|
||||||
@@ -52,4 +61,11 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals('<link rel="shortcut icon" type="image/x-icon" href="' . $target . '">' . PHP_EOL, $result);
|
$this->assertEquals('<link rel="shortcut icon" type="image/x-icon" href="' . $target . '">' . PHP_EOL, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testComponentRegistration()
|
||||||
|
{
|
||||||
|
$this->htmlBuilder->component('tweet', 'components.tweet', ['handle', 'body', 'date']);
|
||||||
|
|
||||||
|
$this->assertTrue($this->htmlBuilder->hasComponent('tweet'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user