Skip to content

Commit

Permalink
Add of disable breadcrumbs collection
Browse files Browse the repository at this point in the history
  • Loading branch information
JuGid committed Oct 27, 2023
1 parent 2ba9bd7 commit 31c219a
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 42 deletions.
13 changes: 13 additions & 0 deletions docs/collection_functions_to_add_breadcrumbs.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Collection function to manipulate breadcrumbs

`Jugid\AutomaticBreadcrumbs\Collection\BreadcrumbsCollection` :
```php
public function addBreadcrumb(BreadcrumbInterface $breadcrumb, string $namespace = 'default'): self;
public function addItem(string $text, string $url) : self;
Expand All @@ -19,4 +20,16 @@ public function extract(string $namespace = 'default') : array;
public function setIncludes(array $includes) : void;
public function isIncluded(string $route) : bool;
public function isEmpty(string $namespace = 'default') : bool;
```

`Jugid\AutomaticBreadcrumbs\Collection\DisableBreadcrumbsCollection` adds more :
```php
public function addDisableItem(string $text, string $url) : self;
public function addDisableRouteItem(string $text, string $route, array $parameters = []): self;
public function addDisableItemNamespace(string $namespace, string $text, string $url) : self;
public function addDisableRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): self;
public function prependDisableItem(string $text, string $url) : self;
public function prependDisableRouteItem(string $text, string $route, array $parameters = []): self;
public function prependDisableItemNamespace(string $namespace, string $text, string $url) : self;
public function prependDisableRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): self;
```
27 changes: 7 additions & 20 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,10 @@ You can create your own template and set it in the configuration. The Renderer i
You can also create your own method storing breadcrumbs. Simply create a class implementing the Jugid\AutomaticBreadcrumbs\Collection\CollectionInterface, tag it as `jugid.automati_breadcrumbs.collection` in your `services.yaml` and set it in the bundle configuration.
The default collection works as the bundle is supposed to work.

```php
public function addBreadcrumb(BreadcrumbInterface $breadcrumb, string $namespace = 'default'): self;
public function addItem(string $text, string $url) : self;
public function addRouteItem(string $text, string $route, array $parameters = []): self;
public function addItemNamespace(string $namespace, string $text, string $url) : self;
public function addRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): self;
public function prependItem(string $text, string $url) : self;
public function prependRouteItem(string $text, string $route, array $parameters = []): self;
public function prependItemNamespace(string $namespace, string $text, string $url) : self;
public function prependRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): self;
public function add(string $namespace) : void;
public function clear(string $namespace = 'default') : bool;
public function removeItem(string $text, string $namespace = 'default') : bool;
public function hasItem(string $text, string $namespace = 'default') : bool;
public function hasNamespace(string $namespace = 'default') : bool;
public function extract(string $namespace = 'default') : array;
public function setIncludes(array $includes) : void;
public function isIncluded(string $route) : bool;
public function isEmpty(string $namespace = 'default') : bool;
```
#### Possible collections

There are predefined types of collection :
* `Jugid\AutomaticBreadcrumbs\Collection\BreadcrumbsCollection` (default)
* `Jugid\AutomaticBreadcrumbs\Collection\DisableBreadcrumbsCollection`

This types can help you create different kind of breadcrumbs : the `BreadcrumbsCollection` works as breadcrumbs we all know. `DisableBreadcrumbsCollection` is an ease to manage your breadcrumbs manually and is more flexible since you can add disabled breadcrumbs where you want manually. It is recommended to use this last with automatic to false or with another namespace than default.
8 changes: 6 additions & 2 deletions src/Breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jugid\AutomaticBreadcrumbs;

use Jugid\AutomaticBreadcrumbs\Collection\BreadcrumbsCollectionInterface;
use Jugid\AutomaticBreadcrumbs\Collection\DisableBreadcrumbsCollectionInterface;
use Jugid\AutomaticBreadcrumbs\Renderer\BreadcrumbsRendererInterface;
use Twig\Environment;

Expand All @@ -16,7 +17,7 @@ class Breadcrumbs

public function __construct(
private Environment $twig,
private BreadcrumbsCollectionInterface $collection,
private BreadcrumbsCollectionInterface|DisableBreadcrumbsCollectionInterface $collection,
private BreadcrumbsRendererInterface $renderer
) {}

Expand All @@ -40,7 +41,10 @@ public function render(array $breadcrumb_collection, array $options = []) : stri
return $this->getRenderer()->render($breadcrumb_collection, $options);
}

public function getCollection() : BreadcrumbsCollectionInterface {
/**
* @return BreadcrumbsCollectionInterface|DisableBreadcrumbsCollectionInterface
*/
public function getCollection() : BreadcrumbsCollectionInterface|DisableBreadcrumbsCollectionInterface {
return $this->collection;
}

Expand Down
27 changes: 24 additions & 3 deletions src/Collection/BreadcrumbsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@
*/
class BreadcrumbsCollection implements BreadcrumbsCollectionInterface
{
private array $breadcrumbs = ['default' => []];
protected array $breadcrumbs = ['default' => []];

private array $includes = [];
protected array $includes = [];

public function __construct(
private UrlGeneratorInterface $urlGenerator
protected UrlGeneratorInterface $urlGenerator
) {}

/**
* @inheritDoc
*/
public function addBreadcrumb(BreadcrumbInterface $breadcrumb, string $namespace = 'default'): BreadcrumbsCollectionInterface {
if(!$this->isIncluded($breadcrumb->getPath(), $namespace)) {
return $this;
}

if(!$this->hasNamespace($namespace)) {
$this->add($namespace);
}
Expand All @@ -33,6 +37,23 @@ public function addBreadcrumb(BreadcrumbInterface $breadcrumb, string $namespace
return $this;
}

/**
* @inheritDoc
*/
public function prependBreadcrumb(BreadcrumbInterface $breadcrumb, string $namespace = 'default'): BreadcrumbsCollectionInterface {
if(!$this->isIncluded($breadcrumb->getPath(), $namespace)) {
return $this;
}

if(!$this->hasNamespace($namespace)) {
$this->add($namespace);
}

array_unshift($this->breadcrumbs[$namespace], $breadcrumb);

return $this;
}

/**
* @inheritDoc
*/
Expand Down
20 changes: 14 additions & 6 deletions src/Collection/BreadcrumbsCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@
*/
interface BreadcrumbsCollectionInterface {
/**
* Add a BreadcrumbInterface object to the namespace
* Add(append) a BreadcrumbInterface object to the namespace
* @param BreadcrumbInterface $breadcrumb
* @param string $namespace
* @return BreadcrumbsCollectionInterface
*/
public function addBreadcrumb(BreadcrumbInterface $breadcrumb, string $namespace = 'default'): self;

/**
* Add a BreadcrumbInterface object to the default namespace.
* Prepend a BreadcrumbInterface object to the namespace
* @param BreadcrumbInterface $breadcrumb
* @param string $namespace
* @return BreadcrumbsCollectionInterface
*/
public function prependBreadcrumb(BreadcrumbInterface $breadcrumb, string $namespace = 'default'): self;

/**
* Add(append) a BreadcrumbInterface object to the default namespace.
* @param string $text
* @param string $url
* @return BreadcrumbsCollectionInterface
*/
public function addItem(string $text, string $url) : self;

/**
* Add a BreadcrumbInterface object to the default namespace with the url generated
* Add(append) a BreadcrumbInterface object to the default namespace with the url generated
* @param string $text
* @param string $route
* @param array $parameters
Expand All @@ -34,7 +42,7 @@ public function addItem(string $text, string $url) : self;
public function addRouteItem(string $text, string $route, array $parameters = []): self;

/**
* Add a BreadcrumbInterface object to the specfied namespace
* Add(append) a BreadcrumbInterface object to the specfied namespace
* @param string $namespace
* @param string $text
* @param string $url
Expand All @@ -43,7 +51,7 @@ public function addRouteItem(string $text, string $route, array $parameters = []
public function addItemNamespace(string $namespace, string $text, string $url) : self;

/**
* Add a BreadcrumbInterface object to the specified namespace with the url generated
* Add(append) a BreadcrumbInterface object to the specified namespace with the url generated
* @param string $namespace
* @param string $text
* @param string $route
Expand Down Expand Up @@ -89,7 +97,7 @@ public function prependItemNamespace(string $namespace, string $text, string $ur
public function prependRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): self;

/**
* Add a specified namespace
* Add(append) a specified namespace
* @param string $namespace
* @return void
*/
Expand Down
70 changes: 70 additions & 0 deletions src/Collection/DisableBreadcrumbsCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Jugid\AutomaticBreadcrumbs\Collection;

use Jugid\AutomaticBreadcrumbs\Model\UrlBreadcrumb;

/**
* @author Julien Gidel <gidjulien@gmail.com>
*/
class DisableBreadcrumbsCollection extends BreadcrumbsCollection implements DisableBreadcrumbsCollectionInterface
{
/**
* @inheritDoc
*/
public function addDisableItem(string $text, string $url): DisableBreadcrumbsCollectionInterface {
return $this->addDisableItemNamespace('default', $text, $url);
}

/**
* @inheritDoc
*/
public function addDisableRouteItem(string $text, string $route, array $parameters = []): DisableBreadcrumbsCollectionInterface {
return $this->addDisableRouteItemNamespace('default', $text, $route, $parameters);
}

/**
* @inheritDoc
*/
public function addDisableItemNamespace(string $namespace, string $text, string $url): DisableBreadcrumbsCollectionInterface {
return $this->addBreadcrumb(new UrlBreadcrumb($text, $url, true), $namespace);
}

/**
* @inheritDoc
*/
public function addDisableRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): DisableBreadcrumbsCollectionInterface {
$url = $this->urlGenerator->generate($route, $parameters);
return $this->addDisableItemNamespace($namespace, $text, $url);
}

/**
* @inheritDoc
*/
public function prependDisableItem(string $text, string $url): DisableBreadcrumbsCollectionInterface {
return $this->prependDisableItemNamespace('default', $text, $url);
}

/**
* @inheritDoc
*/
public function prependDisableRouteItem(string $text, string $route, array $parameters = []): DisableBreadcrumbsCollectionInterface {
return $this->prependDisableRouteItemNamespace('default', $text, $route, $parameters);
}

/**
* @inheritDoc
*/
public function prependDisableItemNamespace(string $namespace, string $text, string $url): DisableBreadcrumbsCollectionInterface {
return $this->prependBreadcrumb(new UrlBreadcrumb($text, $url, true), $namespace);
}

/**
* @inheritDoc
*/
public function prependDisableRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): DisableBreadcrumbsCollectionInterface {
$url = $this->urlGenerator->generate($route, $parameters);
return $this->prependDisableItemNamespace($namespace, $text, $url);
}

}
82 changes: 82 additions & 0 deletions src/Collection/DisableBreadcrumbsCollectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Jugid\AutomaticBreadcrumbs\Collection;

/**
* @author Julien Gidel <gidjulien@gmail.com>
*/
interface DisableBreadcrumbsCollectionInterface extends BreadcrumbsCollectionInterface
{
/**
* Add a BreadcrumbInterface object representing a future element to the default namespace.
* @param string $text
* @param string $url
* @return DisableBreadcrumbsCollectionInterface
*/
public function addDisableItem(string $text, string $url) : self;

/**
* Add a BreadcrumbInterface object representing a future element to the default namespace with the url generated
* @param string $text
* @param string $route
* @param array $parameters
* @return DisableBreadcrumbsCollectionInterface
*/
public function addDisableRouteItem(string $text, string $route, array $parameters = []): self;

/**
* Add a BreadcrumbInterface object representing a future element to the specfied namespace
* @param string $namespace
* @param string $text
* @param string $url
* @return DisableBreadcrumbsCollectionInterface
*/
public function addDisableItemNamespace(string $namespace, string $text, string $url) : self;

/**
* Add a BreadcrumbInterface object representing a future element to the specified namespace with the url generated
* @param string $namespace
* @param string $text
* @param string $route
* @param array $parameters
* @return DisableBreadcrumbsCollectionInterface
*/
public function addDisableRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): self;

/**
* Prepend a BreadcrumbInterface object representing a future element to the namespace
* @param string $text
* @param string $url
* @return DisableBreadcrumbsCollectionInterface
*/
public function prependDisableItem(string $text, string $url) : self;

/**
* Prepend a BreadcrumbInterface object representing a future element to the default namespace with the url generated
* @param string $text
* @param string $route
* @param array $parameters
* @return DisableBreadcrumbsCollectionInterface
*/
public function prependDisableRouteItem(string $text, string $route, array $parameters = []): self;

/**
* Prepend a BreadcrumbInterface object representing a future element to the specfied namespace
* @param string $namespace
* @param string $text
* @param string $url
* @return DisableBreadcrumbsCollectionInterface
*/
public function prependDisableItemNamespace(string $namespace, string $text, string $url) : self;

/**
* Prepend a BreadcrumbInterface object to the specified namespace with the url generated representing a future element
* @param string $namespace
* @param string $text
* @param string $route
* @param array $parameters
* @return DisableBreadcrumbsCollectionInterface
*/
public function prependDisableRouteItemNamespace(string $namespace, string $text, string $route, array $parameters = []): self;

}
16 changes: 6 additions & 10 deletions src/DependencyInjection/AutomaticBreadcrumbsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,13 @@ public function load(array $configs, ContainerBuilder $container)
private function findId(ContainerBuilder $container, string $tag, string $class) : string
{
$available_ids = $container->findTaggedServiceIds($tag);

foreach($available_ids as $id => $tags) {
try {
$container->getAlias($class);
return $id;
} catch(InvalidArgumentException $e) {
continue;
}

try {
$alias = $container->getAlias($class);
return $alias->__toString();
} catch(InvalidArgumentException $e) {
throw new InvalidConfigurationException(sprintf('Class %s does not exist or is not registered as %s', $class, $tag));
}

throw new InvalidConfigurationException(sprintf('Class %s does not exist or is not registered as %s', $class, $tag));
}

public function getAlias(): string
Expand Down
6 changes: 6 additions & 0 deletions src/Model/BreadcrumbInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ public function getText() : string;
* @return string
*/
public function getPath() : string;

/**
* Return the disability's breadcrumb
* @return bool
*/
public function isDisable() : bool;
}
Loading

0 comments on commit 31c219a

Please sign in to comment.