Skip to content

Commit

Permalink
Merge pull request #220 from llwt/develop
Browse files Browse the repository at this point in the history
add option to configure icon prefix
  • Loading branch information
Florian Eckerstorfer committed Apr 27, 2014
2 parents a03bc03 + 04e7609 commit 323f009
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/BraincraftedBootstrapExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('braincrafted_bootstrap.assets_dir', $config['assets_dir']);
$container->setParameter('braincrafted_bootstrap.output_dir', $config['output_dir']);
$container->setParameter('braincrafted_bootstrap.less_filter', $config['less_filter']);
$container->setParameter('braincrafted_bootstrap.icon_prefix', $config['icon_prefix']);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ private function buildConfigTree()
->thenInvalid('Invalid less filter "%s"')
->end()
->end()
->scalarNode('icon_prefix')
->defaultValue('glyphicon')
->end()
->arrayNode('customize')
->addDefaultsIfNotSet()
->children()
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/services/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
<parameter key="braincrafted_bootstrap.twig.label_extension.class">Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapLabelExtension</parameter>
<parameter key="braincrafted_bootstrap.twig.badge_extension.class">Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapBadgeExtension</parameter>
<parameter key="braincrafted_bootstrap.twig.form_extension.class">Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapFormExtension</parameter>
<parameter key="braincrafted_bootstrap.icon_prefix" />
</parameters>

<services>

<service id="braincrafted_bootstrap.twig.icon_extension" class="%braincrafted_bootstrap.twig.icon_extension.class%">
<tag name="twig.extension" />
<argument>%braincrafted_bootstrap.icon_prefix%</argument>
</service>

<service id="braincrafted_bootstrap.twig.label_extension" class="%braincrafted_bootstrap.twig.label_extension.class%">
Expand Down
37 changes: 21 additions & 16 deletions Tests/Twig/BootstrapIconExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,20 @@
*/
class BootstrapIconExtensionTest extends \PHPUnit_Framework_TestCase
{
/** @var BootstrapIconExtension */
private $extension;

/**
* Set up
*/
public function setUp()
{
$this->extension = new BootstrapIconExtension();
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension::getFilters()
*/
public function testGetFilters()
{
$this->assertCount(1, $this->extension->getFilters());
$this->assertCount(1, $this->getIconExtension()->getFilters());
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension::getFunctions()
*/
public function testGetFunctions()
{
$this->assertCount(1, $this->extension->getFunctions());
$this->assertCount(1, $this->getIconExtension()->getFunctions());
}

/**
Expand All @@ -62,9 +51,14 @@ public function testIconFilter()
{
$this->assertEquals(
'<span class="glyphicon glyphicon-heart"></span>',
$this->extension->iconFunction('heart'),
$this->getIconExtension('glyphicon')->iconFunction('heart'),
'->iconFunction() returns the HTML code for the given icon.'
);
$this->assertEquals(
'<span class="fa fa-heart"></span>',
$this->getIconExtension('fa')->iconFunction('heart'),
'->iconFunction() uses the iconPrefix passed into the IconExtension constructor.'
);
}

/**
Expand All @@ -74,16 +68,27 @@ public function testParseIconsFilter()
{
$this->assertEquals(
'<span class="glyphicon glyphicon-heart"></span> foobar',
$this->extension->parseIconsFilter('.icon-heart foobar'),
$this->getIconExtension('glyphicon')->parseIconsFilter('.icon-heart foobar'),
'->parseIconsFilter() returns the HTML code with the replaced icons.'
);

$this->assertEquals(
'<span class="fa fa-heart"></span> foobar',
$this->getIconExtension('fa')->parseIconsFilter('.icon-heart foobar'),
'->parseIconsFilter() uses the iconPrefix passed into the IconExtension constructor.'
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension::getName()
*/
public function testGetName()
{
$this->assertEquals('braincrafted_bootstrap_icon', $this->extension->getName());
$this->assertEquals('braincrafted_bootstrap_icon', $this->getIconExtension()->getName());
}

private function getIconExtension($iconPrefix = null)
{
return new BootstrapIconExtension($iconPrefix);
}
}
12 changes: 11 additions & 1 deletion Twig/BootstrapIconExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
*/
class BootstrapIconExtension extends Twig_Extension
{
/**
* @var string
*/
private $iconPrefix;

public function __construct($iconPrefix)
{
$this->iconPrefix = $iconPrefix;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -78,7 +88,7 @@ function ($matches) use ($that) {
*/
public function iconFunction($icon)
{
return sprintf('<span class="glyphicon glyphicon-%s"></span>', $icon);
return sprintf('<span class="%1$s %1$s-%2$s"></span>', $this->iconPrefix, $icon);
}

/**
Expand Down

0 comments on commit 323f009

Please sign in to comment.