Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rdrenth committed Nov 20, 2015
2 parents c1a85a0 + 4b94bef commit 85845e2
Show file tree
Hide file tree
Showing 11 changed files with 493 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Changelog

## Unreleased
## 1.0.0
* Initial version of the `rdrenth/twig-extension-bundle` bundle.
118 changes: 117 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,125 @@
RdrenthTwigExtensionBundle
=============

## About
This is a Symfony2 Bundle that provides you with some extensions to Twig!

## Twig extensions

### Stringy
This extension provides the following filters (provided by the [Stringy](https://github.com/danielstjules/Stringy) package).

For more information about each filter, please check the links.

#### [ascii](https://github.com/danielstjules/Stringy#toascii)

```twig
{{ 'fòôbàř'|ascii }} {# foobar #}
```

#### [camelize](https://github.com/danielstjules/Stringy#camelize)

```twig
{{ 'Camel-Case'|camelize }} {# camelCase #}
```

#### [dasherize](https://github.com/danielstjules/Stringy#dasherize)

```twig
{{ 'fooBar'|dasherize }} {# foo-bar #}
```

#### [delimit](https://github.com/danielstjules/Stringy#delimit)

```twig
{{ 'fooBar'|delimit('::') }} {# foo::bar #}
```

#### [humanize](https://github.com/danielstjules/Stringy#humanize)

```twig
{{ 'author_id'|humanize }} {# Author #}
```

#### [slugify](https://github.com/danielstjules/Stringy#slugify-string-replacement----)

```twig
{{ 'Using strings like fòô bàř'| slugify }} {# using-strings-like-foo-bar #}
```

#### [titleize](https://github.com/danielstjules/Stringy#titleize-array-ignore)

```twig
{{ 'i like to watch television'|titleize(['to']) }} {# I Like to Watch Television #}
```

#### [underscored](https://github.com/danielstjules/Stringy#underscored)

```twig
{{ 'TestUCase'|underscored }} {# test_u_case #}
```


## Installation
### Step 1: Install RdrenthTwigExtensionBundle using [Composer](http://getcomposer.org)

```bash
$ composer require rdrenth/twig-extension-bundle
```
### Step 2: Enable the bundle
```php
<?php

// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Rdrenth\Bundle\TwigExtensionBundle\RdrenthTwigExtensionBundle(),
// ...
);
}
```

### Step 3: Configure your `config.yml` file
By default the filters are disabled, enable the filters you want to use like this:

```yaml
# app/config/config.yml

rdrenth_twig_extension:
stringy:
filters:
ascii: ~
camelize: ~
```
It is also possible to modify the filter name that is being used in Twig:
```yaml
# app/config/config.yml

rdrenth_twig_extension:
stringy:
filters:
camelize:
filter: camels
```
Or provide extra filters which are not available by default from the Stringy package (the method has to exist in the [Stringy class](https://github.com/danielstjules/Stringy/blob/master/src/Stringy.php)):
```yaml
# app/config/config.yml

rdrenth_twig_extension:
stringy:
extra_filters:
- { filter: swap_case, method: swapCase }
```
## License
This bundle is under the MIT license. See the complete license in the bundle:
Resources/meta/LICENSE
Resources/meta/LICENSE
108 changes: 108 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Rdrenth\Bundle\TwigExtensionBundle\Tests\DependencyInjection;

use Rdrenth\Bundle\TwigExtensionBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Yaml;

/**
* ConfigurationTest
*
* @author Ronald Drenth <[email protected]>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://github.com/rdrenth/twig-extension-bundle
*/
class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function provideStringyFilters()
{
return array(
'ascii' => 'toAscii',
'camelize' => 'camelize',
'dasherize' => 'dasherize',
'delimit' => 'delimit',
'humanize' => 'humanize',
'slugify' => 'slugify',
'titleize' => 'titleize',
'underscored' => 'underscored',
);
}

/**
* Test to make sure the default configuration is correctly processed
*/
public function testDefaultConfigurationProcessing()
{
$rawConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/default.yml'));
$config = $this->processConfiguration($rawConfig);

$this->assertArrayHasKey('stringy', $config);
}

/**
* Test to make sure the stringy section of the configuration is correctly processed
*/
public function testStringyConfigurationProcessing()
{
$rawConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/stringy.yml'));
$config = $this->processConfiguration($rawConfig);

$this->assertArrayHasKey('stringy', $config);
$config = $config['stringy'];

$this->assertArrayHasKey('enabled', $config);
$this->assertArrayHasKey('encoding', $config);
$this->assertArrayHasKey('filters', $config);
$this->assertArrayHasKey('extra_filters', $config);

$filters = $config['filters'];
foreach ($this->provideStringyFilters() as $filterName => $methodName) {
$this->assertStringyFilter($filters, $filterName, $filterName, $methodName, true);
}

$this->assertStringyFilter($config['extra_filters'], 0, 'swap_case', 'swapCase');
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testInvalidStringyMethod()
{
$rawConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/stringy-invalid.yml'));
$this->processConfiguration($rawConfig);
}

/**
* @param array $filters
* @param mixed $indexName
* @param string $filterName
* @param string $methodName
* @param bool|null $enabled
*/
private function assertStringyFilter(array $filters, $indexName, $filterName, $methodName, $enabled = null)
{
if ($enabled !== null) {
$this->assertEquals($enabled, $filters[$indexName]['enabled']);
}

$this->assertEquals($filterName, $filters[$indexName]['filter']);
$this->assertEquals($methodName, $filters[$indexName]['method']);
}

/**
* Processes an array of raw configuration and returns a compiled version.
*
* @param array $config
* @return array
*/
private function processConfiguration(array $config)
{
$processor = new Processor();

return $processor->processConfiguration(new Configuration(), $config);
}
}
47 changes: 47 additions & 0 deletions Tests/DependencyInjection/ExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rdrenth\Bundle\TwigExtensionBundle\Tests\DependencyInjection;

use Rdrenth\Bundle\TwigExtensionBundle\DependencyInjection\RdrenthTwigExtensionExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Yaml\Yaml;

/**
* ExtenionTest
*
* @author Ronald Drenth <[email protected]>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link https://github.com/rdrenth/twig-extension-bundle
*/
class ExtenionTest extends \PHPUnit_Framework_TestCase
{
/**
* Test DI container with default config
*/
public function testDefaultContainer()
{
$config = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/default.yml'));
$container = $this->getContainer($config);

$this->assertTrue($container->hasDefinition('rdrenth_twig_extension.stringy'));

$this->assertEquals(
$container->getParameterBag()->resolveValue('%rdrenth_twig_extension.stringy.class%'),
$container->getDefinition('rdrenth_twig_extension.stringy')->getClass()
);
}

/**
* @param array $config
* @return ContainerBuilder
*/
private function getContainer(array $config)
{
$container = new ContainerBuilder();
$loader = new RdrenthTwigExtensionExtension();
$loader->load($config, $container);
$container->compile();

return $container;
}
}
1 change: 1 addition & 0 deletions Tests/Resources/config/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rdrenth_twig_extension: ~
5 changes: 5 additions & 0 deletions Tests/Resources/config/stringy-invalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rdrenth_twig_extension:
stringy:
filters:
ascii:
method: toAsciiMethodThatDoesNotExistAtAll
13 changes: 13 additions & 0 deletions Tests/Resources/config/stringy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
rdrenth_twig_extension:
stringy:
filters:
ascii: ~
camelize: ~
dasherize: ~
delimit: ~
humanize: ~
slugify: ~
titleize: ~
underscored: ~
extra_filters:
- { filter: swap_case, method: swapCase }
Loading

0 comments on commit 85845e2

Please sign in to comment.