Permalink
Browse files

Initial import.

  • Loading branch information...
0 parents commit 59a5af1bd51c159db239810295a3363ed2d139e0 @richsage richsage committed Aug 9, 2011
@@ -0,0 +1,23 @@
+<?php
+
+namespace WhiteOctober\BreadcrumbsBundle\DependencyInjection;
+
+use Symfony\Component\Config\Definition\Builder\NodeBuilder;
+use Symfony\Component\Config\Definition\Builder\TreeBuilder;
+
+class Configuration
+{
+ /**
+ * Generates the configuration tree.
+ * No configuration is currently required.
+ *
+ * @return Symfony\Component\DependencyInjection\Configuration\NodeInterface
+ */
+ public function getConfigTree()
+ {
+ $treeBuilder = new TreeBuilder();
+ $rootNode = $treeBuilder->root("white_october_breadcrumbs", 'array');
+
+ return $treeBuilder->buildTree();
+ }
+}
@@ -0,0 +1,25 @@
+<?php
+
+namespace WhiteOctober\BreadcrumbsBundle\DependencyInjection;
+
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
+use Symfony\Component\Config\FileLocator;
+
+class WhiteOctoberBreadcrumbsExtension extends Extension
+{
+ /**
+ * Loads our service, accessible as "white_october_breadcrumbs"
+ *
+ * @param array $configs
+ * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
+ * @return void
+ */
+ public function load(array $configs, ContainerBuilder $container)
+ {
+ $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+ $loader->load('breadcrumbs.xml');
+ }
+}
@@ -0,0 +1,66 @@
+<?php
+
+namespace WhiteOctober\BreadcrumbsBundle\Model;
+
+class Breadcrumbs implements \Iterator, \ArrayAccess, \Countable
+{
+ private $breadcrumbs = array();
+
+ private $position = 0;
+
+ public function addItem($text, $url)
+ {
+ $b = new SingleBreadcrumb($text, $url);
+ $this->breadcrumbs[] = $b;
+ }
+
+ public function rewind()
+ {
+ return reset($this->breadcrumbs);
+ }
+
+ public function current()
+ {
+ return current($this->breadcrumbs);
+ }
+
+ public function key()
+ {
+ return key($this->breadcrumbs);
+ }
+
+ public function next()
+ {
+ return next($this->breadcrumbs);
+ }
+
+ public function valid()
+ {
+ return key($this->breadcrumbs) !== null;
+ }
+
+ public function offsetExists($offset)
+ {
+ return isset($this->breadcrumbs[$offset]);
+ }
+
+ public function offsetSet($offset, $value)
+ {
+ $this->breadcrumbs[$offset] = $value;
+ }
+
+ public function offsetGet($offset)
+ {
+ return isset($this->breadcrumbs[$offset]) ? $this->breadcrumbs[$offset] : null;
+ }
+
+ public function offsetUnset($offset)
+ {
+ unset($this->breadcrumbs[$offset]);
+ }
+
+ public function count()
+ {
+ return count($this->breadcrumbs);
+ }
+}
@@ -0,0 +1,15 @@
+<?php
+
+namespace WhiteOctober\BreadcrumbsBundle\Model;
+
+class SingleBreadcrumb
+{
+ public $url;
+ public $text;
+
+ public function __construct($text = "", $url = "")
+ {
+ $this->url = $url;
+ $this->text = $text;
+ }
+}
@@ -0,0 +1,67 @@
+Installation
+============
+
+ 1. Add this bundle to your vendor/ dir using the vendors script:
+
+ Add the following lines in your ``deps`` file:
+
+ [WhiteOctoberBreadcrumbsBundle]
+ git=git://github.com/whiteoctober/BreadcrumbsBundle.git
+ target=/bundles/WhiteOctober/BreadcrumbsBundle
+
+ Run the vendors script:
+
+ ./bin/vendors install
+
+ 2. Add the WhiteOctober namespace to your autoloader:
+
+ // app/autoload.php
+ $loader->registerNamespaces(array(
+ 'WhiteOctober' => __DIR__.'/../vendor/bundles',
+ ));
+
+ 3. Add this bundle to your application's kernel:
+
+ // app/AppKernel.php
+ public function registerBundles()
+ {
+ return array(
+ // ...
+ new WhiteOctober\BreadcrumbsBundle\WhiteOctoberBreadcrumbsBundle(),
+ // ...
+ );
+ }
+
+ 4. Configure the `white_october_breadcrumbs` service in your config.yml:
+
+ white_october_breadcrumbs: ~
+
+
+That's it for configuration.
+
+Usage
+=====
+
+In your application controller methods:
+
+ public function yourAction()
+ {
+ $breadcrumbs = $this->get("white_october_breadcrumbs");
+ $bc->addItem("Home", $this->get("router")->generate("index"));
+ $bc->addItem("Register", $this->get("router")->generate("register"));
+ }
+
+and then in your template:
+
+ {{ wo_render_breadcrumbs() }
+
+The last item in the breadcrumbs collection will automatically be rendered
+as plain text rather than a `<a>...</a>` tag.
+
+Overriding the template
+=======================
+
+You can override the template used by copying the
+`Resources/views/breadcrumbs.html.twig` file out of the bundle and placing it
+into `app/Resources/WhiteOctoberBreadcrumbsBundle/views`, then customising
+as you see fit.
@@ -0,0 +1,26 @@
+<?xml version="1.0" ?>
+<container xmlns="http://symfony.com/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <services>
+
+ <!-- Our service, for controllers -->
+ <service id="white_october_breadcrumbs" class="WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs"></service>
+
+ <!-- Templating helper -->
+ <service id="white_october_breadcrumbs.helper" class="WhiteOctober\BreadcrumbsBundle\Templating\Helper\BreadcrumbsHelper">
+ <argument type="service" id="templating" />
+ <argument type="service" id="white_october_breadcrumbs" />
+ <tag name="templating.helper" alias="breadcrumbs" />
+ </service>
+
+ <!-- Twig extension -->
+ <service id="white_october_breadcrumbs.twig" class="WhiteOctober\BreadcrumbsBundle\Twig\Extension\BreadcrumbsExtension">
+ <argument type="service" id="service_container" />
+ <tag name="twig.extension" />
+ </service>
+
+ </services>
+
+</container>
@@ -0,0 +1,11 @@
+{% if wo_breadcrumbs()|length %}
+ <ul id="wo-breadcrumbs">
+ {% for b in wo_breadcrumbs() %}
+ {% if b|wo_is_final_breadcrumb %}
+ <li>{{ b.text }}</li>
+ {% else %}
+ <li><a href="{{ b.url }}">{{ b.text }}</a></li>
+ {% endif %}
+ {% endfor %}
+ </ul>
+{% endif %}
@@ -0,0 +1,38 @@
+<?php
+
+namespace WhiteOctober\BreadcrumbsBundle\Templating\Helper;
+
+use Symfony\Component\Templating\Helper\Helper;
+use Symfony\Component\Templating\EngineInterface;
+use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
+
+class BreadcrumbsHelper extends Helper
+{
+ protected $templating;
+ protected $breadcrumbs;
+
+ public function __construct(EngineInterface $templating, Breadcrumbs $breadcrumbs)
+ {
+ $this->templating = $templating;
+ $this->breadcrumbs = $breadcrumbs;
+ }
+
+ /**
+ * Returns the HTML for the breadcrumbs
+ *
+ * @param $name
+ * @return string A HTML string
+ */
+ public function breadcrumbs()
+ {
+ return $this->templating->render("WhiteOctoberBreadcrumbsBundle::breadcrumbs.html.twig");
+ }
+
+ /**
+ * @codeCoverageIgnore
+ */
+ public function getName()
+ {
+ return 'breadcrumbs';
+ }
+}
@@ -0,0 +1,83 @@
+<?php
+
+namespace WhiteOctober\BreadcrumbsBundle\Twig\Extension;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\Templating\Helper\Helper;
+
+/**
+ * Provides an extension for Twig to output breadcrumbs
+ *
+ */
+class BreadcrumbsExtension extends \Twig_Extension
+{
+ protected $container;
+ protected $breadcrumbs;
+
+ public function __construct(ContainerInterface $container)
+ {
+ $this->container = $container;
+ $this->breadcrumbs = $container->get("white_october_breadcrumbs");
+ }
+
+ /**
+ * Returns a list of functions to add to the existing list.
+ *
+ * @return array An array of functions
+ */
+ public function getFunctions()
+ {
+ return array(
+ "wo_breadcrumbs" => new \Twig_Function_Method($this, "getBreadcrumbs", array("is_safe" => array("html"))),
+ "wo_render_breadcrumbs" => new \Twig_Function_Method($this, "renderBreadcrumbs", array("is_safe" => array("html"))),
+ );
+ }
+
+ public function getFilters()
+ {
+ return array(
+ "wo_is_final_breadcrumb" => new \Twig_Filter_Method($this, "isLastBreadcrumb"),
+ );
+ }
+
+ /**
+ * Returns the breadcrumbs object
+ *
+ * @return \WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs
+ */
+ public function getBreadcrumbs()
+ {
+ return $this->breadcrumbs;
+ }
+
+ /**
+ * Renders the breadcrumbs in a list
+ *
+ * @return string
+ */
+ public function renderBreadcrumbs()
+ {
+ return $this->container->get("white_october_breadcrumbs.helper")->breadcrumbs();
+ }
+
+ /**
+ * Checks if this breadcrumb is the last one in the collection
+ *
+ * @param $crumb
+ * @return boolean
+ */
+ public function isLastBreadcrumb($crumb)
+ {
+ return ($this->breadcrumbs[count($this->breadcrumbs)-1] === $crumb);
+ }
+
+ /**
+ * Returns the name of the extension.
+ *
+ * @return string The extension name
+ */
+ public function getName()
+ {
+ return "breadcrumbs";
+ }
+}
@@ -0,0 +1,9 @@
+<?php
+
+namespace WhiteOctober\BreadcrumbsBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class WhiteOctoberBreadcrumbsBundle extends Bundle
+{
+}

0 comments on commit 59a5af1

Please sign in to comment.