From a61e8d9cf92c475af64b3c74a339c8ede9e22d26 Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Tue, 26 Nov 2013 21:52:44 +0100 Subject: [PATCH] Add ability to use param in breadcrumb title Now we can add parameters in the title as: /** * @Breadcrumb("{book}") */ public function showAction(Book $book) { } --- BreadcrumbTrail/Trail.php | 27 +++++++++++ README.md | 85 +++++++++++++++++++++++++++++++++++ Resources/config/services.xml | 2 +- 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/BreadcrumbTrail/Trail.php b/BreadcrumbTrail/Trail.php index c0e2bf5..9d671b7 100644 --- a/BreadcrumbTrail/Trail.php +++ b/BreadcrumbTrail/Trail.php @@ -69,6 +69,8 @@ public function getTemplate() * @param Boolean $routeAbsolute Whether to generate an absolute URL * @param integer $position Position of the breadcrumb (default = 0) * @param mixed $attributes Additional attributes for the breadcrumb + * @throws \RuntimeException + * @throws \InvalidArgumentException * @return self */ public function add($breadcrumb_or_title, $routeName = null, $routeParameters = array(), $routeAbsolute = false, $position = 0, $attributes = array()) @@ -87,6 +89,31 @@ public function add($breadcrumb_or_title, $routeName = null, $routeParameters = $request = $this->container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE); if ($request !== null) { + preg_match_all('#\{(?P\w+).?(?P\w*):?(?P(\w|,| )*)\}#', $breadcrumb_or_title, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); + foreach ($matches as $match) { + $varName = $match['variable'][0]; + $functionName = $match['function'][0]; + $parameters = explode(',', $match['parameters'][0]); + + if($request->attributes->has($varName)) { + $object = $request->attributes->get($varName); + + if(empty($functionName)) { + $objectValue = (string) $object; + } + elseif(is_callable(array($object, $fullFunctionName = 'get'.$functionName)) + || is_callable(array($object, $fullFunctionName = 'has'.$functionName)) + || is_callable(array($object, $fullFunctionName = 'is'.$functionName))) { + $objectValue = call_user_func_array(array($object, $fullFunctionName),$parameters); + } + else { + throw new \RuntimeException(sprintf('Function "%s" not found.', $functionName)); + } + + $breadcrumb_or_title = str_replace($match[0][0], $objectValue, $breadcrumb_or_title); + } + } + foreach ($routeParameters as $key => $value) { if (is_numeric($key)) { $routeParameters[$value] = $request->get($value); diff --git a/README.md b/README.md index 1da80af..48bf298 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,92 @@ class MyController extends Controller */ } + +} +``` + +#### Title with @ParamConverter + +The [@ParamConverter](http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#annotation-configuration) of the SensioFrameworkExtraBundle convert request parameters like 'id' to objects then injected as controller method arguments: + +It is possible to display values ​​of these objects in the breadcrumb. + + +``` +... +use APY\BreadcrumbTrailBundle\Annotation\Breadcrumb; +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; + +/** + * @Route("/book/{id}") + * @Breadcrumb("Books") + * @Breadcrumb("{book}") + */ +public function aShowAction(Book $book) +{ + + /* + + This action will show the following breacrumb trail: + Books > result of __toString method of $book's Object + + */ + +} + +/** + * @Route("/book/{id}") + * @Breadcrumb("Books") + * @Breadcrumb("{book.title}") + */ +public function bShowAction(Book $book) +{ + + /* + + This action will show the following breacrumb trail: + Books > result of getTitle method of $book's Object + + The bundle tries to call the methods : getTitle, hasTitle or isTitle + + */ + +} + +/** + * @Route("/book/{id}") + * @Breadcrumb("Books") + * @Breadcrumb("{book.title:argument1}") + */ +public function cShowAction(Book $book) +{ + + /* + + This action will show the following breacrumb trail: + Books > result of getTitle('argument1') method of $book's Object + + */ + +} + +/** + * @Route("/book/{id}") + * @Breadcrumb("Books") + * @Breadcrumb("{book.title:argument1: argument2}") + */ +public function dShowAction(Book $book) +{ + + /* + + This action will show the following breacrumb trail: + Books > result of getTitle('argument1', ' argument2') method of $book's Object + + */ + } + ``` --- diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 97ebd74..a217441 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -24,7 +24,7 @@ - +