Skip to content

Commit

Permalink
Merge branch '2.0'
Browse files Browse the repository at this point in the history
* 2.0:
  Added badges in README
  Changed directory structure and added "autoload-dev"
  Fixed trimming in HTMLPurifierListener
  Fixed README and added CHANGELOG
  Updated travis.yaml to rely on PHPUnit bridge and PHP CS Fixer
  • Loading branch information
HeahDude committed Dec 14, 2019
2 parents d39f309 + 7a55d73 commit e7105b9
Show file tree
Hide file tree
Showing 28 changed files with 397 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.php_cs.cache
.phpunit.result.cache
phpunit.xml
composer.lock
vendor/
13 changes: 13 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('Resources')
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
])
->setFinder($finder)
;
69 changes: 65 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
language: php
php:
- '7.1'
install: composer install
script: php vendor/bin/phpunit

sudo: false

dist: xenial

env:
global:
- COMPOSER_MEMORY_LIMIT=-1
- SYMFONY_PHPUNIT_DIR=$HOME/.phpunit-bridge

cache:
directories:
- $HOME/.composer/cache
- $HOME/.phpunit-bridge

jobs:
include:
# Lowest
- php: 5.5
dist: trusty
env: NO_FLEX=1 COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
- php: 7.1
env: SYMFONY_REQUIRE="4.3.*" COMPOSER_FLAGS="--prefer-lowest"

# Stable
- php: 7.2
env: SYMFONY_REQUIRE="3.4.*" COMPOSER_FLAGS="--prefer-stable"
- php: 7.3
env: SYMFONY_REQUIRE="4.4.*" COMPOSER_FLAGS="--prefer-stable"
- php: 7.4
env: SYMFONY_REQUIRE="5.0.*" COMPOSER_FLAGS="--prefer-stable"

# Dev
- php: 7.4
env: STABILITY=dev

# QA
- stage: QA
name: PHP CS Fixer
php: 7.4
script: vendor/bin/php-cs-fixer fix --dry-run --diff
- name: Coverage
php: 7.4
before_script:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi
script:
- ./vendor/bin/simple-phpunit -v --coverage-text

allow_failures:
- env: STABILITY=dev

before_install:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
- composer self-update
- if [[ -z $NO_FLEX ]]; then composer global require --no-progress --no-scripts --no-plugins symfony/flex; fi;

install:
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS
- vendor/bin/simple-phpunit install

script:
- if [[ -v $STABILITY ]]; then composer config minimum-stability $STABILITY; fi;
- composer validate --strict --no-check-lock
- vendor/bin/simple-phpunit -v
14 changes: 14 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Version 2.0 (08/2018)

* Added compatibility for Symfony 5 and Twig 3
* Updated minimum requirement of Twig to 1.35 and 2.4 to support runtime
* [BC break] Dropped support for Symfony 2. Symfony 3.4 minimum required.
* [BC break] Removed classes parameters.
* [BC break] Removed the form data transformer.
* Added an `HTMLPurifierTextTypeExtension` to add `purify_html` and
`purify_html_profile` options to all `TextType` children.
* Added an `HTMLPurifierListener` to purify submitted form data.
* Added an `HTMLPurifiersRegistryInterface` to lazy load purifiers by profile.
* Added a Twig `HTMLPurifierRuntime` to lazy load purifiers in templates.
* Added a pass to use custom `\HTMLPurifier` classes as custom profiles using
a new `exercise.html_purifier` tag.
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# ExerciseHTMLPurifierBundle [![Build Status](https://travis-ci.org/Exercise/HTMLPurifierBundle.svg?branch=master)](https://travis-ci.org/Exercise/HTMLPurifierBundle)
[![Total Downloads](https://poser.pugx.org/exercise/htmlpurifier-bundle/downloads)](https://packagist.org/packages/exercise/htmlpurifier-bundle)
[![Latest Stable Version](https://poser.pugx.org/exercise/htmlpurifier-bundle/v/stable)](https://packagist.org/packages/exercise/htmlpurifier-bundle)
[![License](https://poser.pugx.org/exercise/htmlpurifier-bundle/license)](https://packagist.org/packages/exercise/htmlpurifier-bundle)
[![Build Status](https://travis-ci.org/Exercise/HTMLPurifierBundle.svg?branch=2.0)](https://travis-ci.org/Exercise/HTMLPurifierBundle)

# ExerciseHTMLPurifierBundle

This bundle integrates [HTMLPurifier][] into Symfony.

Expand Down Expand Up @@ -90,9 +95,10 @@ exercise_html_purifier:
Cache.SerializerPath: '%kernel.cache_dir%/htmlpurifier'
```

The `default` profile is special in that it is used as the configuration for the
`exercise_html_purifier.default` service as well as the base configuration for
other profiles you might define.
The `default` profile is special, it is *always* defined and its configuration
is inherited by all custom profiles.
`exercise_html_purifier.default` is the default service using the base
configuration.

```yaml
# config/packages/exercise_html_purifier.yaml
Expand All @@ -115,13 +121,24 @@ this in you `app/config/services.yml` or `config/services.yaml`:
services:
# ...
\HTMLPurifier:
alias: exercise_html_purifier.custom
# or the equivalent as of Symfony 3.3
\HTMLPurifier: '@exercise_html_purifier.custom'
exercise_html_purifier.default: '@exercise_html_purifier.custom'
```

## Using a custom purifier class as default

If you want to use your own class as default purifier, define a new alias:

```yaml
# config/services.yaml
services:
# ...
exercise_html_purifier.default: '@App\Html\CustomHtmlPurifier'
```

In such case, the custom purifier will use its own defined configuration,
ignoring the bundle configuration.

## Form Type Extension

This bundles provides a form type extension for filtering form fields with
Expand Down Expand Up @@ -212,5 +229,7 @@ $builder
{{ html_string|purify('custom') }}
```

Your class will inherit the default config or the one from the same profile
used in the tag.
## Contributing

PRs are welcomed :). Please target the `2.0` branch for bug fixes and `master`
for new features.
30 changes: 20 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,36 @@
"homepage": "https://github.com/Exercise/HTMLPurifierBundle",
"license": "MIT",
"authors": [
{ "name": "contributors", "homepage": "https://github.com/Exercise/HTMLPurifierBundle/contributors" }
{
"name": "contributors",
"homepage": "https://github.com/Exercise/HTMLPurifierBundle/contributors"
}
],
"require": {
"php": "^5.5.9|>=7.0.8",
"ezyang/htmlpurifier": "~4.0",
"symfony/dependency-injection": "~3.4.1 || ^4.0.1",
"symfony/http-kernel": "~3.4.1 || ^4.0.1",
"symfony/config": "~3.3 || ~4.0"
"symfony/config": "~3.4 || ~4.0 || ^5.0",
"symfony/dependency-injection": "~3.4.1 || ^4.0.1 || ^5.0",
"symfony/http-kernel": "~3.4.1 || ^4.0.1 || ^5.0"
},
"require-dev": {
"phpunit/phpunit": "^7.2",
"symfony/form": "~3.4.1 || ^4.0.1",
"twig/twig": "^1.34.4 || ^2.4.3"
"friendsofphp/php-cs-fixer": "^2.0",
"symfony/form": "~3.4.1 || ^4.0.1 || ^5.0",
"symfony/phpunit-bridge": "4.4.*",
"twig/twig": "^1.35.0 || ^2.4.4 || ^3.0"
},
"autoload": {
"psr-4": { "Exercise\\HTMLPurifierBundle\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Exercise\\HTMLPurifierBundle\\Tests\\": "tests/" }
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-4": { "Exercise\\HTMLPurifierBundle\\": "" }
}
}
5 changes: 2 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
-->
<testsuites>
<testsuite name="ExerciseHTMLPurifierBundle">
<directory suffix="Test.php">./Tests</directory>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<directory suffix=".php">./src</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
</exclude>
</whitelist>
</filter>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Exercise\HTMLPurifierBundle\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Exercise\HTMLPurifierBundle\HTMLPurifiersRegistryInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class ExerciseHTMLPurifierExtension extends Extension
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class HTMLPurifierListener implements EventSubscriberInterface
private $profile;

/**
* @param HTMLPurifiersRegistryInterface $registry
* @param string $profile
* @param string $profile
*/
public function __construct(HTMLPurifiersRegistryInterface $registry, $profile)
{
Expand All @@ -30,6 +29,10 @@ public function purifySubmittedData(FormEvent $event)
}

if (0 === strlen($submittedData = trim($data))) {
if ($submittedData !== $data) {
$event->setData($submittedData);
}

return;
}

Expand Down
47 changes: 47 additions & 0 deletions src/Form/TypeExtension/ForwardCompatTypeExtensionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Exercise\HTMLPurifierBundle\Form\TypeExtension;

use Symfony\Component\Form\FormTypeExtensionInterface;

if (method_exists(FormTypeExtensionInterface::class, 'getExtendedTypes')) {
eval('
namespace Exercise\HTMLPurifierBundle\Form\TypeExtension;
/**
* @internal
*/
trait ForwardCompatTypeExtensionTrait
{
private static function doGetExtendedTypes(): iterable
{
}
public static function getExtendedTypes(): iterable
{
return self::doGetExtendedTypes();
}
}
');
} else {
/**
* @internal
*/
trait ForwardCompatTypeExtensionTrait
{
/**
* @return iterable
*/
private static function doGetExtendedTypes()
{
}

/**
* @return iterable
*/
public static function getExtendedTypes()
{
return self::doGetExtendedTypes();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class HTMLPurifierTextTypeExtension extends AbstractTypeExtension
{
use ForwardCompatTypeExtensionTrait;

private $purifiersRegistry;

public function __construct(HTMLPurifiersRegistryInterface $registry)
Expand All @@ -28,10 +30,7 @@ public function getExtendedType()
return TextType::class;
}

/**
* {@inheritdoc}
*/
public static function getExtendedTypes()
private static function doGetExtendedTypes()
{
return [TextType::class];
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class HTMLPurifierExtension extends AbstractExtension
*/
public function getFilters()
{
return array(
return [
new TwigFilter('purify', [HTMLPurifierRuntime::class, 'purify'], ['is_safe' => ['html']]),
);
];
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,35 @@ class SerializerCacheWarmerTest extends TestCase
{
public function testShouldBeRequired()
{
$cacheWarmer = new SerializerCacheWarmer(array(), new \HTMLPurifier());
$cacheWarmer = new SerializerCacheWarmer([], new \HTMLPurifier());
$this->assertFalse($cacheWarmer->isOptional());
}

public function testFailsWhenNotWriteable()
{
$path = sys_get_temp_dir().'/'.uniqid('htmlpurifierbundle_fails');

if (false === @mkdir($path, 0000)) {
$this->markTestSkipped('Tmp dir is not writeable.');
}

$this->expectException('RuntimeException');

$cacheWarmer = new SerializerCacheWarmer([$path], new \HTMLPurifier());
$cacheWarmer->warmUp(null);

@rmdir($path);
}

public function testShouldCreatePaths()
{
if (!is_writable(sys_get_temp_dir())) {
$this->markTestSkipped(sprintf('The system temp directory "%s" is not writeable for the current system user.', sys_get_temp_dir()));
}

$path = sys_get_temp_dir() . '/' . uniqid('htmlpurifierbundle');
$path = sys_get_temp_dir().'/'.uniqid('htmlpurifierbundle');

$cacheWarmer = new SerializerCacheWarmer(array($path), new \HTMLPurifier());
$cacheWarmer = new SerializerCacheWarmer([$path], new \HTMLPurifier());
$cacheWarmer->warmUp(null);

$this->assertTrue(is_dir($path));
Expand Down
Loading

0 comments on commit e7105b9

Please sign in to comment.