Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create HtmLawed service and DI extension for configuration
- Loading branch information
Showing
11 changed files
with
351 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace OpenSky\Bundle\HtmLawedBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
|
||
class Configuration | ||
{ | ||
/** | ||
* Generates the configuration tree. | ||
* | ||
* @return Symfony\Component\Config\Definition\NodeInterface | ||
*/ | ||
public function getConfigTree() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
|
||
// TODO: Uncomment children()/end() pairs for PR8+ compatibility | ||
return $treeBuilder | ||
->root('opensky_htmlawed', 'array') | ||
//->children() | ||
->useAttributeAsKey('id') | ||
->prototype('array') | ||
//->children() | ||
// TODO: Add htmLawed configuration structure | ||
->variableNode('config') | ||
->defaultValue(array()) | ||
->beforeNormalization() | ||
->ifTrue(function($v){ return !is_array($v); }) | ||
->thenEmptyArray() | ||
->end() | ||
->end() | ||
->scalarNode('spec')->defaultNull()->end() | ||
//->end() | ||
->end() | ||
//->end() | ||
->end() | ||
->buildTree() | ||
; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace OpenSky\Bundle\HtmLawedBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
class OpenSkyHtmLawedExtension extends Extension | ||
{ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('htmlawed.xml'); | ||
|
||
$processor = new Processor(); | ||
$configuration = new Configuration(); | ||
|
||
$config = $processor->process($configuration->getConfigTree(), $configs); | ||
|
||
foreach ($config as $id => $value) { | ||
$this->createHtmLawed($container, $id, $value); | ||
} | ||
} | ||
|
||
private function createHtmLawed(ContainerBuilder $container, $id, array $config) | ||
{ | ||
$serviceId = 'htmlawed.'.$id; | ||
|
||
$container | ||
->setDefinition($serviceId, new DefinitionDecorator('opensky.htmlawed.abstract')) | ||
->setArgument(0, $config['config']) | ||
->setArgument(1, $config['spec']) | ||
; | ||
} | ||
|
||
public function getAlias() | ||
{ | ||
return 'opensky_htmlawed'; | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace OpenSky\Bundle\HtmLawedBundle; | ||
|
||
use OpenSky\Bundle\HtmLawedBundle\DependencyInjection\OpenSkyHtmLawedExtension; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class OpenSkyHtmLawedBundle extends Bundle | ||
{ | ||
/** | ||
* @see Symfony\Component\HttpKernel\Bundle.Bundle::build() | ||
*/ | ||
public function build(ContainerBuilder $container) | ||
{ | ||
$container->registerExtension(new OpenSkyHtmLawedExtension()); | ||
} | ||
} |
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,71 @@ | ||
# HtmLawedBundle | ||
|
||
This bundle wraps the htmLawed library as a Symfony2 service and provides form | ||
fields. | ||
|
||
## Installation | ||
|
||
### Dependencies | ||
|
||
This bundle depends on [htmLawed](http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/). | ||
|
||
Since htmLawed is not packaged as a class, it cannot be autoloaded. You should | ||
manually require the library from your project's `autoload.php` file. | ||
|
||
### Submodule Creation | ||
|
||
Add HtmLawedBundle to your `src/` directory: | ||
|
||
$ git submodule add git://github.com/opensky/HtmLawedBundle.git src/OpenSky/Bundle/HtmLawedBundle | ||
|
||
### Class Autoloading | ||
|
||
If the `src/` directory is already configured in your project's `autoload.php` | ||
via `registerNamespaceFallback()`, no changes should be necessary. Otherwise, | ||
either define the fallback directory or explicitly add the "OpenSky" namespace: | ||
|
||
# src/autoload.php | ||
|
||
$loader->registerNamespaces(array( | ||
'OpenSky' => __DIR__, | ||
)); | ||
|
||
### Application Kernel | ||
|
||
Add HtmLawedBundle to the `registerBundles()` method of your application kernel: | ||
|
||
public function registerBundles() | ||
{ | ||
return array( | ||
new OpenSky\Bundle\HtmLawedBundle\OpenSkyHtmLawedBundle(), | ||
); | ||
} | ||
|
||
## Configuration | ||
|
||
This bundle defines an `HtmLawed` service, which is constructed with `$config` | ||
and `$spec` parameters. It implements a single `process()` method that filters a | ||
string value through the `htmLawed()` function. The `$config` and `$spec` | ||
parameters provided during construction will be passed to `htmLawed()`. | ||
|
||
### HtmLawed Extension | ||
|
||
HtmLawed services may be configured with the following: | ||
|
||
# app/config/config.yml | ||
|
||
opensky_htmlawed: | ||
custom: | ||
config: | ||
comment: 0 | ||
cdata: 1 | ||
spec: a=title | ||
default: ~ | ||
|
||
The above example would define two services: `htmlawed.custom` and `htmlawed.default`. | ||
Each service created by the extension is essentially a pre-configured profile | ||
for filtering input using `htmLawed()`. | ||
|
||
See also: | ||
|
||
* [htmLawed documentation](http://www.bioinformatics.org/phplabware/internal_utilities/htmLawed/htmLawed_README.htm) |
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,16 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<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"> | ||
|
||
<parameters> | ||
<parameter key="opensky.htmlawed.class">OpenSky\Bundle\HtmLawedBundle\Service\HtmLawed</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="opensky.htmlawed.abstract" class="%opensky.htmlawed.class%" abstract="true" public="false"> | ||
<argument /> <!-- Configuration --> | ||
<argument /> <!-- Spec --> | ||
</service> | ||
</services> | ||
</container> |
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,31 @@ | ||
<?php | ||
|
||
namespace OpenSky\Bundle\HtmLawedBundle\Service; | ||
|
||
class HtmLawed | ||
{ | ||
private $config; | ||
private $spec; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param array $config Configuration parameter for htmLawed() | ||
* @param string $spec HTML specification parameter for htmLawed() | ||
*/ | ||
public function __construct(array $config = array(), $spec = null) | ||
{ | ||
$this->config = $config; | ||
$this->spec = (string) $spec; | ||
} | ||
|
||
/** | ||
* Processes input through htmLawed(). | ||
* | ||
* @param string $input | ||
*/ | ||
public function process($input) | ||
{ | ||
return htmLawed($input, $this->config, $this->spec); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
Tests/DependencyInjection/OpenSkyHtmLawedExtensionTest.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,60 @@ | ||
<?php | ||
|
||
namespace OpenSky\Bundle\HtmLawedBundle\Tests\DependencyInjection; | ||
|
||
use OpenSky\Bundle\HtmLawedBundle\DependencyInjection\OpenSkyHtmLawedExtension; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass; | ||
|
||
class OpenSkyHtmlLawedExtensionExtensionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testShouldLoadAbstractDefinition() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$extension = new OpenSkyHtmLawedExtension(); | ||
|
||
$extension->load(array(array()), $container); | ||
|
||
$definitions = $container->getDefinitions(); | ||
$this->assertEquals(1, count($definitions)); | ||
$this->assertTrue($definitions['opensky.htmlawed.abstract']->isAbstract()); | ||
} | ||
|
||
public function testShouldCreateDefinitionsThatExtendAbstractDefinition() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$extension = new OpenSkyHtmLawedExtension(); | ||
|
||
$config = array( | ||
'custom' => array( | ||
'config' => array('comment' => 0, 'cdata' => 1), | ||
'spec' => 'a=title', | ||
), | ||
'default' => null, | ||
); | ||
|
||
$extension->load(array($config), $container); | ||
|
||
$this->compileContainer($container); | ||
|
||
$definitions = $container->getDefinitions(); | ||
$this->assertEquals(3, count($definitions)); | ||
|
||
$arguments = $definitions['htmlawed.custom']->getArguments(); | ||
$this->assertEquals($config['custom']['config'], $arguments[0]); | ||
$this->assertEquals($config['custom']['spec'], $arguments[1]); | ||
|
||
$arguments = $definitions['htmlawed.default']->getArguments(); | ||
$this->assertEquals(array(), $arguments[0]); | ||
$this->assertEquals('', $arguments[1]); | ||
} | ||
|
||
private function compileContainer(ContainerBuilder $container) | ||
{ | ||
$container->getCompilerPassConfig()->setOptimizationPasses(array( | ||
new ResolveDefinitionTemplatesPass(), | ||
)); | ||
$container->getCompilerPassConfig()->setRemovingPasses(array()); | ||
$container->compile(); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace OpenSky\Bundle\HtmLawedBundle\Tests\Service; | ||
|
||
use OpenSky\Bundle\HtmLawedBundle\Service\HtmLawed; | ||
|
||
class HtmlLawedTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testShouldProcessInput() | ||
{ | ||
$htmLawed = new HtmLawed(); | ||
|
||
// The bootstrap's htmLawed() declaration is an identity function | ||
$this->assertEquals('input', $htmLawed->process('input')); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
require_once $_SERVER['SYMFONY_SRC'].'/Symfony/Component/ClassLoader/UniversalClassLoader.php'; | ||
|
||
$loader = new Symfony\Component\ClassLoader\UniversalClassLoader(); | ||
$loader->registerNamespace('Symfony', $_SERVER['SYMFONY_SRC']); | ||
$loader->register(); | ||
|
||
spl_autoload_register(function($class) | ||
{ | ||
if (0 === strpos($class, 'OpenSky\\Bundle\\HtmLawedBundle\\')) { | ||
$path = implode('/', array_slice(explode('\\', $class), 3)).'.php'; | ||
require_once __DIR__.'/../'.$path; | ||
return true; | ||
} | ||
}); | ||
|
||
function htmLawed($input, $config, $spec) | ||
{ | ||
return $input; | ||
} |
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,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="./Tests/bootstrap.php" | ||
> | ||
<testsuites> | ||
<testsuite name="OpenSkyHtmLawedBundle Test Suite"> | ||
<directory>./Tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<php> | ||
<!-- <server name="SYMFONY_SRC" value="/path/to/symfony/src" /> --> | ||
</php> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./</directory> | ||
<exclude> | ||
<directory>./Resources</directory> | ||
<directory>./Tests</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |