From 4c9b44a817543bd0ef7f0ca8c8a63e5556504274 Mon Sep 17 00:00:00 2001 From: Adam Engebretson Date: Wed, 2 Dec 2015 02:15:22 -0600 Subject: [PATCH] Extracting Components to Trait & Adding to Forms Signed-off-by: Adam Engebretson --- src/Componentable.php | 111 +++++++++++++++++++++++++++ src/FormBuilder.php | 48 +++++++++++- src/HtmlBuilder.php | 101 ++++--------------------- src/HtmlServiceProvider.php | 2 +- tests/FormAccessibleTest.php | 2 +- tests/FormBuilderTest.php | 143 ++++++++++++++++++++--------------- 6 files changed, 255 insertions(+), 152 deletions(-) create mode 100644 src/Componentable.php diff --git a/src/Componentable.php b/src/Componentable.php new file mode 100644 index 0000000..2f3e073 --- /dev/null +++ b/src/Componentable.php @@ -0,0 +1,111 @@ +getComponentData($component['signature'], $arguments); + + return new HtmlString( + $this->view->make($component['view'], $data)->render() + ); + } + + /** + * Prepare the component data, while respecting provided defaults. + * + * @param array $signature + * @param array $arguments + * + * @return array + */ + protected function getComponentData(array $signature, array $arguments) + { + $data = []; + + $i = 0; + foreach ($signature as $variable => $default) { + // If the "variable" value is actually a numeric key, we can assume that + // no default had been specified for the component argument and we'll + // just use null instead, so that we can treat them all the same. + if (is_numeric($variable)) { + $variable = $default; + $default = null; + } + + $data[$variable] = array_get($arguments, $i) ?: $default; + + $i++; + } + + return $data; + } + + /** + * Dynamically handle calls to the class. + * + * @param string $method + * @param array $parameters + * + * @return \Illuminate\Contracts\View\View|mixed + * + * @throws \BadMethodCallException + */ + public function __call($method, $parameters) + { + if (static::hasComponent($method)) { + return $this->renderComponent($method, $parameters); + } + + throw new BadMethodCallException("Method {$method} does not exist."); + } + +} \ No newline at end of file diff --git a/src/FormBuilder.php b/src/FormBuilder.php index debfd7a..af66750 100755 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -3,8 +3,11 @@ namespace Collective\Html; use DateTime; +use BadMethodCallException; use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; +use Illuminate\Contracts\View\Factory; +use Illuminate\Database\Eloquent\Model; use Illuminate\Session\SessionInterface; use Illuminate\Support\Traits\Macroable; use Illuminate\Contracts\Routing\UrlGenerator; @@ -12,7 +15,10 @@ use Illuminate\Contracts\Routing\UrlGenerator; class FormBuilder { - use Macroable; + use Macroable, Componentable { + Macroable::__call as macroCall; + Componentable::__call as componentCall; + } /** * The HTML builder instance. @@ -28,6 +34,13 @@ class FormBuilder */ protected $url; + /** + * The View factory instance. + * + * @var \Illuminate\Contracts\View\Factory + */ + protected $view; + /** * The CSRF token used by the form builder. * @@ -80,14 +93,16 @@ class FormBuilder /** * Create a new form builder instance. * - * @param \Illuminate\Contracts\Routing\UrlGenerator $url * @param \Collective\Html\HtmlBuilder $html + * @param \Illuminate\Contracts\Routing\UrlGenerator $url + * @param \Illuminate\Contracts\View\Factory $view * @param string $csrfToken */ - public function __construct(HtmlBuilder $html, UrlGenerator $url, $csrfToken) + public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken) { $this->url = $url; $this->html = $html; + $this->view = $view; $this->csrfToken = $csrfToken; } @@ -1166,4 +1181,31 @@ class FormBuilder return $this; } + + /** + * Dynamically handle calls to the class. + * + * @param string $method + * @param array $parameters + * + * @return \Illuminate\Contracts\View\View|mixed + * + * @throws \BadMethodCallException + */ + public function __call($method, $parameters) + { + try { + return $this->componentCall($method, $parameters); + } catch (BadMethodCallException $e) { + // + } + + try { + return $this->macroCall($method, $parameters); + } catch (BadMethodCallException $e) { + // + } + + throw new BadMethodCallException("Method {$method} does not exist."); + } } diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index 8ee6314..0b3495f 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -2,6 +2,7 @@ namespace Collective\Html; +use BadMethodCallException; use Illuminate\Support\HtmlString; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Traits\Macroable; @@ -10,8 +11,9 @@ use Illuminate\Contracts\Routing\UrlGenerator; class HtmlBuilder { - use Macroable { - __call as macroCall; + use Macroable, Componentable { + Macroable::__call as macroCall; + Componentable::__call as componentCall; } /** @@ -26,14 +28,7 @@ class HtmlBuilder * * @var \Illuminate\Contracts\View\Factory */ - private $view; - - /** - * The registered components. - * - * @var array - */ - protected static $components = []; + protected $view; /** * Create a new HTML builder instance. @@ -491,78 +486,6 @@ class HtmlBuilder return $this->toHtmlString('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 \Illuminate\Contracts\View\View - */ - protected function renderComponent($name, array $arguments) - { - $component = static::$components[$name]; - $data = $this->getComponentData($component['signature'], $arguments); - - return $this->view->make($component['view'], $data); - } - - /** - * Prepare the component data, while respecting provided defaults. - * - * @param array $signature - * @param array $arguments - * - * @return array - */ - protected function getComponentData(array $signature, array $arguments) - { - $data = []; - - $i = 0; - foreach ($signature as $variable => $default) { - // If the "variable" value is actually a numeric key, we can assume that - // no default had been specified for the component argument and we'll - // just use null instead, so that we can treat them all the same. - if (is_numeric($variable)) { - $variable = $default; - $default = null; - } - - $data[$variable] = array_get($arguments, $i) ?: $default; - - $i++; - } - - return $data; - } - /** * Transform the string to an Html serializable object * @@ -587,10 +510,18 @@ class HtmlBuilder */ public function __call($method, $parameters) { - if (static::hasComponent($method)) { - return $this->renderComponent($method, $parameters); + try { + return $this->componentCall($method, $parameters); + } catch (BadMethodCallException $e) { + // } - return $this->macroCall($method, $parameters); + try { + return $this->macroCall($method, $parameters); + } catch (BadMethodCallException $e) { + // + } + + throw new BadMethodCallException("Method {$method} does not exist."); } } diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index 6eac93c..c632e56 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -49,7 +49,7 @@ class HtmlServiceProvider extends ServiceProvider protected function registerFormBuilder() { $this->app->singleton('form', function ($app) { - $form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken()); + $form = new FormBuilder($app['html'], $app['url'], $app['view'], $app['session.store']->getToken()); return $form->setSessionStore($app['session.store']); }); diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php index 58e5661..77d9524 100644 --- a/tests/FormAccessibleTest.php +++ b/tests/FormAccessibleTest.php @@ -32,7 +32,7 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase $this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET')); $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, $this->viewFactory, 'abc'); } public function testItCanMutateValuesForForms() diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 20c8a2e..77195d5 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -11,24 +11,25 @@ use Mockery as m; class FormBuilderTest extends PHPUnit_Framework_TestCase { - /** - * Setup the test environment. - */ - public function setUp() - { - $this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET')); - $this->viewFactory = m::mock(Factory::class); - $this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory); - $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, 'abc'); - } - /** - * Destroy the test environment. - */ - public function tearDown() - { - m::close(); - } + /** + * Setup the test environment. + */ + public function setUp() + { + $this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET')); + $this->viewFactory = m::mock(Factory::class); + $this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory); + $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, $this->viewFactory, 'abc'); + } + + /** + * Destroy the test environment. + */ + public function tearDown() + { + m::close(); + } public function testOpeningForm() { @@ -39,10 +40,13 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $form5 = $this->formBuilder->open(['method' => 'PUT']); $this->assertEquals('
', $form1); - $this->assertEquals('', $form2); + $this->assertEquals('', + $form2); $this->assertEquals('', $form3); - $this->assertEquals('', $form4); - $this->assertEquals('', $form5); + $this->assertEquals('', + $form4); + $this->assertEquals('', + $form5); } public function testClosingForm() @@ -201,7 +205,8 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals('', $form1); $this->assertEquals('', $form2); - $this->assertEquals('', $form3); + $this->assertEquals('', + $form3); $this->assertEquals('', $form4); } @@ -212,7 +217,8 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $form3 = $this->formBuilder->time('foo', null, ['class' => 'span2']); $this->assertEquals('', $form1); - $this->assertEquals('', $form2); + $this->assertEquals('', + $form2); $this->assertEquals('', $form3); } @@ -241,33 +247,36 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase public function testSelect() { $select = $this->formBuilder->select( - 'size', - ['L' => 'Large', 'S' => 'Small'] - ); - $this->assertEquals($select, ''); + 'size', + ['L' => 'Large', 'S' => 'Small'] + ); + $this->assertEquals($select, + ''); $select = $this->formBuilder->select( - 'size', - ['L' => 'Large', 'S' => 'Small'], - 'L' - ); - $this->assertEquals($select, ''); + 'size', + ['L' => 'Large', 'S' => 'Small'], + 'L' + ); + $this->assertEquals($select, + ''); $select = $this->formBuilder->select( - 'size', - ['L' => 'Large', 'S' => 'Small'], - null, - ['class' => 'class-name', 'id' => 'select-id'] - ); - $this->assertEquals($select, ''); + 'size', + ['L' => 'Large', 'S' => 'Small'], + null, + ['class' => 'class-name', 'id' => 'select-id'] + ); + $this->assertEquals($select, + ''); $this->formBuilder->label('select-name-id'); $select = $this->formBuilder->select( - 'select-name-id', - [], - null, - ['name' => 'select-name'] - ); + 'select-name-id', + [], + null, + ['name' => 'select-name'] + ); $this->assertEquals($select, ''); } @@ -279,34 +288,39 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $session->shouldReceive('getOldInput')->twice()->with('size')->andReturn('M'); $select = $this->formBuilder->select('size', $list, 'S'); - $this->assertEquals($select, ''); + $this->assertEquals($select, + ''); $session->shouldReceive('getOldInput')->twice()->with('size.multi')->andReturn(['L', 'S']); $select = $this->formBuilder->select('size[multi][]', $list, 'M', ['multiple' => 'multiple']); - $this->assertEquals($select, ''); + $this->assertEquals($select, + ''); $session->shouldReceive('getOldInput')->once()->with('size.key')->andReturn(null); $select = $this->formBuilder->select('size[key]', $list); - $this->assertEquals($select, ''); + $this->assertEquals($select, + ''); } public function testFormWithOptionalPlaceholder() { $select = $this->formBuilder->select( - 'size', - ['L' => 'Large', 'S' => 'Small'], - null, - ['placeholder' => 'Select One...'] - ); - $this->assertEquals($select, ''); + 'size', + ['L' => 'Large', 'S' => 'Small'], + null, + ['placeholder' => 'Select One...'] + ); + $this->assertEquals($select, + ''); $select = $this->formBuilder->select( - 'size', - ['L' => 'Large', 'S' => 'Small'], - 'L', - ['placeholder' => 'Select One...'] - ); - $this->assertEquals($select, ''); + 'size', + ['L' => 'Large', 'S' => 'Small'], + 'L', + ['placeholder' => 'Select One...'] + ); + $this->assertEquals($select, + ''); } public function testFormSelectYear() @@ -315,9 +329,12 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $select2 = (string) $this->formBuilder->selectYear('year', 2000, 2020, null, ['id' => 'foo']); $select3 = (string) $this->formBuilder->selectYear('year', 2000, 2020, '2000'); - $this->assertContains('', $select2); - $this->assertContains('', + $select1); + $this->assertContains('', + $select3); } public function testFormSelectRange() @@ -334,7 +351,8 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $month2 = (string) $this->formBuilder->selectMonth('month', '1'); $month3 = (string) $this->formBuilder->selectMonth('month', null, ['id' => 'foo']); - $this->assertContains('', + $month1); $this->assertContains('', $month3); } @@ -467,7 +485,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $url = 'http://laravel.com/'; $image = $this->formBuilder->image($url); - $this->assertEquals('', $image); + $this->assertEquals('', $image); } public function testFormColor() @@ -493,6 +511,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase class FormBuilderModelStub { + protected $data; public function __construct(array $data = [])