Skip to content

Commit

Permalink
Added blade component (#13)
Browse files Browse the repository at this point in the history
* Added blade component
  • Loading branch information
tabuna authored Sep 28, 2020
1 parent 38a8746 commit 2142616
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 1 deletion.
85 changes: 85 additions & 0 deletions src/BreadcrumbsComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Tabuna\Breadcrumbs;

use Illuminate\View\Component;
use Illuminate\Support\Collection;

class BreadcrumbsComponent extends Component
{
/**
* @var Manager
*/
public $breadcrumbs;

/**
* @var string|null
*/
public $route;

/**
* @var mixed|null
*/
public $parameters;

/**
* @var string|null
*/
public $class;

/**
* @var string|null
*/
public $active;

/**
* Create a new component instance.
*
* @param Manager $manager
* @param string|null $route
* @param mixed|null $parameters
* @param string|null $class
* @param string|null $active
*/
public function __construct(
Manager $manager,
string $route = null,
$parameters = null,
string $class = null,
string $active = null
)
{
$this->breadcrumbs = $manager;
$this->route = $route;
$this->parameters = $parameters;
$this->class = $class;
$this->active = $active;
}

/**
* @return Collection
* @throws \Throwable
*/
public function generate(): Collection
{
if ($this->route !== null) {
return $this->breadcrumbs->generate($this->route, $this->parameters);
}

return $this->breadcrumbs->current($this->parameters);
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|string
*/
public function render()
{
return $this->breadcrumbs->has($this->route)
? view('breadcrumbs::breadcrumbs')
: '';
}
}
10 changes: 10 additions & 0 deletions src/BreadcrumbsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
namespace Tabuna\Breadcrumbs;

use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use function Opis\Closure\serialize;

class BreadcrumbsServiceProvider extends ServiceProvider
{
/**
* Bootstrap your package's services.
*/
public function boot()
{
Blade::component('tabuna-breadcrumbs', BreadcrumbsComponent::class);
}

/**
* Register the service provider.
*
Expand All @@ -18,6 +27,7 @@ class BreadcrumbsServiceProvider extends ServiceProvider
public function register()
{
$this->app->singleton(Manager::class);
$this->loadViewsFrom(__DIR__ . '/../views', 'breadcrumbs');

\Illuminate\Support\Facades\Route::middlewareGroup('breadcrumbs', [
BreadcrumbsMiddleware::class,
Expand Down
67 changes: 67 additions & 0 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php


namespace Tabuna\Breadcrumbs\Tests;

use Illuminate\Support\Facades\Route;
use Illuminate\Testing\TestResponse;
use Tabuna\Breadcrumbs\Trail;

class ComponentTest extends TestCase
{

public function testSimple(): void
{
$this->getComponent('simple')->assertSee('Home');
}

public function testClass(): void
{
$this->getComponent('class')
->assertSee('Home')
->assertSee('<li class="item">', false)
->assertSee('<li class="item active">', false);
}

public function testRoute(): void
{
$this->getComponent('route')
->assertSee('Static Page');
}

public function testParameters(): void
{
$this->getComponent('parameters')
->assertSee('value 1')
->assertSee('value 2')
->assertSee('value 3');
}

/**
* @param string $template
*
* @return TestResponse
*/
protected function getComponent(string $template): TestResponse
{
Route::get('static', function (string $view) {
})
->name('static')
->breadcrumbs(function (Trail $trail) {
return $trail->push('Static Page');
});

Route::get('component/{view}', function (string $view) {
return view("test_breadcrumbs::$view");
})
->name('breadcrumbs.components')
->breadcrumbs(function (Trail $trail, $value = null, $value2 = null) {
return $trail
->push('Home')
->push('Arguments', $value)
->push('Arguments2', $value2);
});

return $this->get("component/$template");
}
}
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class TestCase extends BaseCase
protected function getPackageProviders($app): array
{
return [
BreadcrumbsServiceProvider::class
BreadcrumbsServiceProvider::class,
TestServiceProvider::class
];
}

Expand Down
20 changes: 20 additions & 0 deletions tests/TestServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tabuna\Breadcrumbs\Tests;

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->loadViewsFrom(__DIR__ . '/views', 'test_breadcrumbs');
}
}
4 changes: 4 additions & 0 deletions tests/views/class.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<x-tabuna-breadcrumbs
class="item"
active="active"
/>
3 changes: 3 additions & 0 deletions tests/views/parameters.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-tabuna-breadcrumbs
parameters="['value 1', 'value 2', 'value 3']"
/>
3 changes: 3 additions & 0 deletions tests/views/route.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-tabuna-breadcrumbs
route="static"
/>
1 change: 1 addition & 0 deletions tests/views/simple.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<x-tabuna-breadcrumbs/>
13 changes: 13 additions & 0 deletions views/breadcrumbs.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@foreach ($generate() as $crumbs)
@if ($crumbs->url() && !$loop->last)
<li class="{{$class}}">
<a href="{{ $crumbs->url() }}">
{{ $crumbs->title() }}
</a>
</li>
@else
<li class="{{$class}} {{$active}}">
{{ $crumbs->title() }}
</li>
@endif
@endforeach

0 comments on commit 2142616

Please sign in to comment.