Skip to content

Commit f0d256c

Browse files
committed
Initial commit - added basic bundle implementation
1 parent 65f136e commit f0d256c

6 files changed

Lines changed: 179 additions & 0 deletions

File tree

CaxyHtmlDiffBundle.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiffBundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class CaxyHtmlDiffBundle extends Bundle
8+
{
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiffBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\Config\FileLocator;
7+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
9+
10+
/**
11+
* This is the class that loads and manages your bundle configuration
12+
*
13+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
14+
*/
15+
class CaxyHtmlDiffExtension extends Extension
16+
{
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function load(array $configs, ContainerBuilder $container)
21+
{
22+
$configuration = new Configuration();
23+
$config = $this->processConfiguration($configuration, $configs);
24+
25+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26+
27+
foreach ($config as $key => $value) {
28+
$container->setParameter($this->getAlias() . '.' . $key, $value);
29+
}
30+
31+
$loader->load('services.yml');
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiffBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
/**
9+
* This is the class that validates and merges configuration from your app/config files
10+
*
11+
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
12+
*/
13+
class Configuration implements ConfigurationInterface
14+
{
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function getConfigTreeBuilder()
19+
{
20+
$treeBuilder = new TreeBuilder();
21+
$rootNode = $treeBuilder->root('caxy_html_diff');
22+
23+
$rootNode
24+
->children()
25+
->arrayNode('special_case_tags')
26+
->prototype('scalar')->end()
27+
->end()
28+
->scalarNode('encoding')->end()
29+
->end();
30+
31+
return $treeBuilder;
32+
}
33+
}

Resources/config/services.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
caxy.html_diff.class: Caxy\HtmlDiffBundle\Service\HtmlDiffService
3+
4+
services:
5+
caxy.html_diff:
6+
class: %caxy.html_diff.class%
7+
arguments: [@service_container]

Service/HtmlDiffService.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Caxy\HtmlDiffBundle\Service;
4+
5+
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use Caxy\HtmlDiff\HtmlDiff;
7+
8+
class HtmlDiffService
9+
{
10+
protected $container;
11+
12+
protected $specialCaseTags = array('strong', 'b', 'i', 'big', 'small', 'u', 'sub', 'sup', 'strike', 's', 'p');
13+
14+
protected $encoding = 'UTF-8';
15+
16+
public function __construct(ContainerInterface $container)
17+
{
18+
$this->container = $container;
19+
20+
$encoding = $this->container->getParameter('caxy_html_diff.encoding');
21+
if (!empty($encoding)) {
22+
$this->encoding = $encoding;
23+
}
24+
25+
$specialCaseTags = $this->container->getParameter('caxy_html_diff.special_case_tags');
26+
if (!empty($specialCaseTags)) {
27+
$this->specialCaseTags = $specialCaseTags;
28+
}
29+
}
30+
31+
public function diff($oldText, $newText)
32+
{
33+
$diff = new HtmlDiff($oldText, $newText, $this->encoding, $this->specialCaseTags);
34+
35+
return $diff->build();
36+
}
37+
38+
public function getEncoding()
39+
{
40+
return $this->encoding;
41+
}
42+
43+
public function setEncoding($encoding)
44+
{
45+
$this->encoding = $encoding;
46+
47+
return $this;
48+
}
49+
50+
public function getSpecialCaseTags()
51+
{
52+
return $this->specialCaseTags;
53+
}
54+
55+
public function addSpecialCaseTag($tag)
56+
{
57+
if (!in_array($tag, $this->specialCaseTags)) {
58+
$this->specialCaseTags[] = $tag;
59+
}
60+
61+
return $this;
62+
}
63+
64+
public function removeSpecialCaseTag($tag)
65+
{
66+
if (($key = array_search($tag, $this->specialCaseTags)) !== false) {
67+
unset($this->specialCaseTags[$key]);
68+
}
69+
70+
return $this;
71+
}
72+
73+
}

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "caxy/htmldiff-bundle",
3+
"type": "symfony-bundle",
4+
"description": "Symfony2 Bundle for php-htmldiff (caxy/php-htmldiff)",
5+
"license": "MIT",
6+
"keywords": [ "diff", "html", "diffing" ],
7+
"homepage": "https://github.com/caxy/HtmlDiffBundle",
8+
"authors": [
9+
{
10+
"name": "Josh Schroeder",
11+
"email": "jschroeder@caxy.com",
12+
"homepage": "http://www.caxy.com"
13+
}
14+
],
15+
"require": {
16+
"php": ">=5.3.3",
17+
"symfony/framework-bundle": "~2.1",
18+
"caxy/php-htmldiff": "*"
19+
},
20+
"target-dir": "Caxy/HtmlDiffBundle",
21+
"autoload": {
22+
"psr-0": { "Caxy\\HtmlDiffBundle": "" }
23+
}
24+
}

0 commit comments

Comments
 (0)