Skip to content

Commit

Permalink
Add 'manager-class' and 'generator-class' config options
Browse files Browse the repository at this point in the history
  • Loading branch information
d13r committed Sep 14, 2017
1 parent 0e8a864 commit 368d7b3
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 35 deletions.
83 changes: 51 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,27 +677,43 @@ $current = Breadcrumbs::generate()->where('current', '!==', 'false)->last();
```


### Macros
### Switching views at runtime

The BreadcrumbsManager class is [macroable](https://unnikked.ga/understanding-the-laravel-macroable-trait-dab051f09172), so you can add your own methods. For example:
You can use `Breadcrumbs::view()` in place of `Breadcrumbs::render()` to render a template other than the [default one](#3-choose-a-template):

```php
Breadcrumbs::macro('pageTitle', function () {
$title = ($breadcrumb = Breadcrumbs::current()) ? "{$breadcrumb->title} – " : '';
```blade
{{ Breadcrumbs::view('partials.breadcrumbs2', 'category', $category) }}
```

if (($page = (int) request('page')) > 1) {
$title .= "Page $page – ";
}
Or you can override the config setting to affect all future `render()` calls:

return $title . 'Demo App';
});
```php
Config::set('breadcrumbs.view', 'partials.breadcrumbs2');
```

```php
{{ Breadcrumbs::render('category', $category) }}
```

Or you could call `Breadcrumbs::generate()` to get the breadcrumbs Collection and load the view manually:

```blade
<title>{{ Breadcrumbs::pageTitle() }}</title>
@include('partials.breadcrumbs2', ['breadcrumbs' => Breadcrumbs::generate('category', $category)])
```


### Overriding the "current" route

If you call `Breadcrumbs::render()` or `Breadcrumbs::generate()` with no parameters, it will use the current route name and parameters by default (as returned by Laravel's `Route::current()` method).

You can override this by calling `Breadcrumbs::setCurrentRoute($name, $param1, $param2...)`.


### Checking if a breadcrumb exists

To check if a breadcrumb with a given name exists, call `Breadcrumbs::exists('name')`, which returns a boolean.


### Defining breadcrumbs in a different file

If you don't want to use `routes/breadcrumbs.php`, you can change it in the config file. First initialise the config file, if you haven't already:
Expand Down Expand Up @@ -771,41 +787,40 @@ class MyServiceProvider extends ServiceProvider
```


### Switching views at runtime

You can use `Breadcrumbs::view()` in place of `Breadcrumbs::render()` to render a template other than the [default one](#3-choose-a-template):

```blade
{{ Breadcrumbs::view('partials.breadcrumbs2', 'category', $category) }}
```
### Macros

Or you can override the config setting to affect all future `render()` calls:
The BreadcrumbsManager class is [macroable](https://unnikked.ga/understanding-the-laravel-macroable-trait-dab051f09172), so you can add your own methods. For example:

```php
Config::set('breadcrumbs.view', 'partials.breadcrumbs2');
```
Breadcrumbs::macro('pageTitle', function () {
$title = ($breadcrumb = Breadcrumbs::current()) ? "{$breadcrumb->title} – " : '';

```php
{{ Breadcrumbs::render('category', $category) }}
```
if (($page = (int) request('page')) > 1) {
$title .= "Page $page – ";
}

Or you could call `Breadcrumbs::generate()` to get the breadcrumbs Collection and load the view manually:
return $title . 'Demo App';
});
```

```blade
@include('partials.breadcrumbs2', ['breadcrumbs' => Breadcrumbs::generate('category', $category)])
<title>{{ Breadcrumbs::pageTitle() }}</title>
```


### Overriding the "current" route

If you call `Breadcrumbs::render()` or `Breadcrumbs::generate()` with no parameters, it will use the current route name and parameters by default (as returned by Laravel's `Route::current()` method).
### Advanced customisations

You can override this by calling `Breadcrumbs::setCurrentRoute($name, $param1, $param2...)`.
For more advanced customisations you can subclass BreadcrumbsManager and/or BreadcrumbsGenerator, then update the config file with the new class name:

```php
// Manager
'manager-class' => DaveJamesMiller\Breadcrumbs\BreadcrumbsManager::class,

### Checking if a breadcrumb exists
// Generator
'generator-class' => DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator::class,
```

To check if a breadcrumb with a given name exists, call `Breadcrumbs::exists('name')`, which returns a boolean.
(**Note:** Anything that's not part of the public API (see below) may change between releases, so I suggest you write unit tests to ensure it doesn't break when upgrading.)


API Reference
Expand Down Expand Up @@ -888,6 +903,10 @@ Breadcrumbs::after('name', function (BreadcrumbsGenerator $breadcrumbs) {
*Laravel Breadcrumbs uses [Semantic Versioning](http://semver.org/).*


### [v4.1.0](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/4.2.0) (Thu 14 Sep 2017)

- Add `manager-class` and `generator-class` config options

### [v4.1.0](https://github.com/davejamesmiller/laravel-breadcrumbs/tree/4.1.0) (Mon 28 Aug 2017)

- Add Bulma template
Expand Down
15 changes: 15 additions & 0 deletions config/breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,19 @@
// When a named breadcrumb is used but doesn't exist (InvalidBreadcrumbException)
'invalid-named-breadcrumb-exception' => true,

/*
|--------------------------------------------------------------------------
| Classes
|--------------------------------------------------------------------------
|
| Subclass the default classes for more advanced customisations.
|
*/

// Manager
'manager-class' => DaveJamesMiller\Breadcrumbs\BreadcrumbsManager::class,

// Generator
'generator-class' => DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator::class,

];
9 changes: 6 additions & 3 deletions src/BreadcrumbsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ public function provides(): array
*/
public function register() //: void
{
// Register Manager class singleton with the app container
$this->app->singleton(BreadcrumbsManager::class);

// Load the default config values
$configFile = __DIR__ . '/../config/breadcrumbs.php';
$this->mergeConfigFrom($configFile, 'breadcrumbs');

// Publish the config/breadcrumbs.php file
$this->publishes([$configFile => config_path('breadcrumbs.php')], 'config');

// Register Manager class singleton with the app container
$this->app->singleton(BreadcrumbsManager::class, config('breadcrumbs.manager-class'));

// Register Generator class so it can be overridden
$this->app->bind(BreadcrumbsGenerator::class, config('breadcrumbs.generator-class'));

// Register 'breadcrumbs::' view namespace
$this->loadViewsFrom(__DIR__ . '/../views/', 'breadcrumbs');
}
Expand Down
38 changes: 38 additions & 0 deletions tests/CustomGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace BreadcrumbsTests;

use Breadcrumbs;
use Config;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsManager;
use Illuminate\Support\Collection;
use Route;
use URL;

class CustomGeneratorTest extends TestCase
{
protected function resolveApplicationConfiguration($app)
{
parent::resolveApplicationConfiguration($app);

// Need to inject this early, before the package is loaded, to simulate it being set in the config file
$app['config']['breadcrumbs.generator-class'] = CustomGenerator::class;
}

public function testCustomGenerator()
{
$breadcrumbs = Breadcrumbs::generate();

$this->assertSame('custom-generator', $breadcrumbs[0]);
}
}

class CustomGenerator extends BreadcrumbsGenerator
{
public function generate(array $callbacks, array $before, array $after, string $name, array $params): Collection
{
return new Collection(['custom-generator']);
}

}
37 changes: 37 additions & 0 deletions tests/CustomManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace BreadcrumbsTests;

use Breadcrumbs;
use Config;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator;
use DaveJamesMiller\Breadcrumbs\BreadcrumbsManager;
use Illuminate\Support\Collection;
use Route;
use URL;

class CustomManagerTest extends TestCase
{
protected function resolveApplicationConfiguration($app)
{
parent::resolveApplicationConfiguration($app);

// Need to inject this early, before the package is loaded, to simulate it being set in the config file
$app['config']['breadcrumbs.manager-class'] = CustomManager::class;
}

public function testCustomManager()
{
$breadcrumbs = Breadcrumbs::generate();

$this->assertSame('custom-manager', $breadcrumbs[0]);
}
}

class CustomManager extends BreadcrumbsManager
{
public function generate(string $name = null, ...$params): Collection
{
return new Collection(['custom-manager']);
}
}

0 comments on commit 368d7b3

Please sign in to comment.