-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from jolicode/feature/built-in-bridges
Add built-in support for Symfony and Twig
- Loading branch information
Showing
8 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/JoliTypo/Bridge/Symfony/DependencyInjection/Configuration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace JoliTypo\Bridge\Symfony\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('joli_typo'); | ||
|
||
$rootNode | ||
->fixXmlConfig('preset') | ||
->children() | ||
->arrayNode('presets') | ||
->useAttributeAsKey('name') | ||
->isRequired() | ||
->prototype('array') | ||
->children() | ||
->scalarNode('locale')->end() | ||
->arrayNode('fixers') | ||
->prototype('scalar') | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/JoliTypo/Bridge/Symfony/DependencyInjection/JoliTypoExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace JoliTypo\Bridge\Symfony\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
class JoliTypoExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$configuration = new Configuration(); | ||
$config = $this->processConfiguration($configuration, $configs); | ||
$presets = $this->createPresetDefinition($container, $config); | ||
|
||
// Twig extension | ||
$twig_extension = new Definition('JoliTypo\Bridge\Twig\JoliTypoExtension'); | ||
$twig_extension->addTag('twig.extension'); | ||
$twig_extension->setArguments(array($presets)); | ||
|
||
$container->setDefinition('joli_typo.twig_extension', $twig_extension); | ||
} | ||
|
||
private function createPresetDefinition(ContainerBuilder $container, $config) | ||
{ | ||
$presets = array(); | ||
|
||
foreach ($config['presets'] as $name => $preset) { | ||
$definition = new Definition('JoliTypo\Fixer'); | ||
|
||
if ($preset['locale']) { | ||
$definition->addMethodCall('setLocale', array($preset['locale'])); | ||
} | ||
|
||
$fixers = array(); | ||
foreach ($preset['fixers'] as $fixer) { | ||
// Allow to use services as fixer? | ||
$fixers[] = $fixer; | ||
} | ||
|
||
$definition->addArgument($fixers); | ||
$container->setDefinition(sprintf('joli_typo.fixer.%s', $name), $definition); | ||
|
||
$presets[$name] = new Reference(sprintf('joli_typo.fixer.%s', $name)); | ||
} | ||
|
||
return $presets; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace JoliTypo\Bridge\Symfony; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class JoliTypoBundle extends Bundle | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
This bundle integrates the [JoliTypo](https://github.com/jolicode/JoliTypo) library into Symfony. | ||
|
||
Please refer to the [JoliTypo documentation](https://github.com/jolicode/JoliTypo/blob/master/README.md) to learn more | ||
about fixers and how to combine them. | ||
|
||
**Note:** there is no cache involved with JoliTypo, take care of it if you want to save some CPU cycles :grimacing: | ||
|
||
Not using Symfony Flex? Register the bundle `Joli\TypoBundle\JoliTypoBundle` in your kernel: | ||
|
||
```php | ||
new Joli\TypoBundle\JoliTypoBundle(), | ||
``` | ||
|
||
Define your Fixers preset as you want (in `config/packages/joli_typo.yaml` or `app/config/config.yml`): | ||
|
||
```yaml | ||
joli_typo: | ||
presets: | ||
fr: | ||
fixers: [ Ellipsis, Dimension, Dash, SmartQuotes, FrenchNoBreakSpace, CurlyQuote, Trademark ] | ||
locale: fr_FR | ||
en: | ||
fixers: [ Ellipsis, Dimension, Dash, SmartQuotes, CurlyQuote, Trademark ] | ||
locale: en_GB | ||
``` | ||
Twig function | ||
============= | ||
The Bundle makes use of the Twig bridge and it's method/filter named `jolitypo`, waiting for two arguments: HTML content | ||
to fix and the preset name. | ||
|
||
```twig | ||
{{ jolitypo('<p>Hi folk!</p>', 'fr') | raw }} | ||
{# or #} | ||
{{ '<p>Hi folk!</p>' | jolitypo('fr') }} | ||
``` | ||
|
||
Another way to use it is by passing a whole block to it: | ||
|
||
```twig | ||
{% block content %} | ||
{{ jolitypo(block('real_content'), 'fr') | raw }} | ||
{% endblock %} | ||
{% block real_content %} | ||
<h2>My whole dynamic page</h2> | ||
{% endblock %} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
namespace JoliTypo\Bridge\Symfony\Twig; | ||
|
||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
|
||
class JoliTypoExtension extends \Twig_Extension | ||
{ | ||
private $presets = array(); | ||
|
||
public function __construct($presets) | ||
{ | ||
$this->presets = $presets; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return array( | ||
new \Twig_SimpleFunction('jolitypo', array($this, 'translate')), | ||
); | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return array( | ||
new \Twig_SimpleFilter('jolitypo', array($this, 'translate'), array('pre_escape' => 'html', 'is_safe' => array('html'))), | ||
); | ||
} | ||
|
||
public function translate($text, $preset = "default") | ||
{ | ||
if (!isset($this->presets[$preset])) { | ||
throw new InvalidConfigurationException(sprintf("There is no '%s' preset configured.", $preset)); | ||
} | ||
|
||
return $this->presets[$preset]->fix($text); | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'jolitypo'; | ||
} | ||
} |