Skip to content

Commit 9080e4e

Browse files
committed
Use new HtmlDiffConfig class for configuration
1 parent 53fbc92 commit 9080e4e

File tree

6 files changed

+312
-80
lines changed

6 files changed

+312
-80
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Bootstrap
22
app/bootstrap*
33

4+
composer.lock
5+
46
# Symfony directories
57
vendor/*
68
*/logs/*

DependencyInjection/CaxyHtmlDiffExtension.php

+134-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Caxy\HtmlDiffBundle\DependencyInjection;
44

5+
use Caxy\HtmlDiff\HtmlDiffConfig;
6+
use Doctrine\Bundle\DoctrineCacheBundle\DependencyInjection\CacheProviderLoader;
7+
use Symfony\Component\DependencyInjection\Alias;
58
use Symfony\Component\DependencyInjection\ContainerBuilder;
69
use Symfony\Component\Config\FileLocator;
10+
use Symfony\Component\DependencyInjection\Reference;
711
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
812
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
913

@@ -14,6 +18,21 @@
1418
*/
1519
class CaxyHtmlDiffExtension extends Extension
1620
{
21+
/**
22+
* @var null|CacheProviderLoader
23+
*/
24+
private $cacheProviderLoader;
25+
26+
/**
27+
* CaxyHtmlDiffExtension constructor.
28+
*/
29+
public function __construct()
30+
{
31+
if (class_exists('Doctrine\\Bundle\\DoctrineCacheBundle\\DependencyInjection\\CacheProviderLoader')) {
32+
$this->cacheProviderLoader = new CacheProviderLoader();
33+
}
34+
}
35+
1736
/**
1837
* {@inheritDoc}
1938
*/
@@ -22,12 +41,126 @@ public function load(array $configs, ContainerBuilder $container)
2241
$configuration = new Configuration();
2342
$config = $this->processConfiguration($configuration, $configs);
2443

25-
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
44+
$cacheDriverId = null;
45+
46+
47+
if (!empty($config['doctrine_cache_driver']) && $config['doctrine_cache_driver']['enabled']) {
48+
$cacheDriverId = $this->loadCacheDriver('doctrine_cache_driver', $config['doctrine_cache_driver'], $container);
49+
}
2650

2751
foreach ($config as $key => $value) {
2852
$container->setParameter($this->getAlias() . '.' . $key, $value);
2953
}
3054

55+
$this->loadHtmlDiffConfig($config, $container, $cacheDriverId);
56+
}
57+
58+
/**
59+
* @param array $config
60+
* @param ContainerBuilder $container
61+
* @param string $cacheDriverId
62+
*/
63+
protected function loadHtmlDiffConfig(array $config, ContainerBuilder $container, $cacheDriverId = null)
64+
{
65+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3166
$loader->load('services.yml');
67+
68+
$definition = $container->getDefinition('caxy.html_diff.config');
69+
70+
$methodsToCall = array(
71+
'special_case_tags' => 'setSpecialCaseTags',
72+
'encoding' => 'setEncoding',
73+
'special_case_chars' => 'setSpecialCaseChars',
74+
'group_diffs' => 'setGroupDiffs',
75+
'insert_space_in_replace' => 'setInsertSpaceInReplace',
76+
'match_threshold' => 'setMatchThreshold',
77+
'use_table_diffing' => 'setUseTableDiffing',
78+
);
79+
80+
foreach ($methodsToCall as $key => $methodName) {
81+
if (array_key_exists($key, $config)) {
82+
$definition->addMethodCall($methodName, array($config[$key]));
83+
}
84+
}
85+
86+
if (null !== $cacheDriverId) {
87+
$definition->addMethodCall('setCacheProvider', array(new Reference($cacheDriverId)));
88+
}
89+
}
90+
91+
/**
92+
* @param string $cacheName
93+
* @param array $driverMap
94+
* @param ContainerBuilder $container
95+
*
96+
* @return string
97+
* @throws \Exception
98+
*/
99+
protected function loadCacheDriver($cacheName, array $driverMap, ContainerBuilder $container)
100+
{
101+
if (null === $this->cacheProviderLoader) {
102+
throw new \Exception('DoctrineCacheBundle required to use doctrine_cache_driver.');
103+
}
104+
105+
$aliasId = $this->getAlias().'_'.$cacheName;
106+
107+
if (!empty($driverMap['cache_provider'])) {
108+
$serviceId = sprintf('doctrine_cache.providers.%s', $driverMap['cache_provider']);
109+
$container->setAlias($aliasId, new Alias($serviceId, false));
110+
111+
return $aliasId;
112+
}
113+
114+
$id = $aliasId;
115+
$host = isset($driverMap['host']) ? $driverMap['host'] : null;
116+
$port = isset($driverMap['port']) ? $driverMap['port'] : null;
117+
$password = isset($driverMap['password']) ? $driverMap['password'] : null;
118+
$database = isset($driverMap['database']) ? $driverMap['database'] : null;
119+
$type = $driverMap['type'];
120+
121+
if ($type == 'service') {
122+
$container->setAlias($id, new Alias($driverMap['id'], false));
123+
124+
return $id;
125+
}
126+
127+
$config = array(
128+
'aliases' => array($id),
129+
$type => array(),
130+
'type' => $type,
131+
'namespace' => null,
132+
);
133+
134+
if (!isset($driverMap['namespace'])) {
135+
// generate a unique namespace for the given application
136+
$environment = $container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment');
137+
$hash = hash('sha256', $environment);
138+
$namespace = 'sf2' . $this->getAlias() . '_' . $hash;
139+
140+
$driverMap['namespace'] = $namespace;
141+
}
142+
143+
$config['namespace'] = $driverMap['namespace'];
144+
145+
if (in_array($type, array('memcache', 'memcached'))) {
146+
$host = !empty($host) ? $host : 'localhost';
147+
$config[$type]['servers'][$host] = array(
148+
'host' => $host,
149+
'port' => !empty($port) ? $port : 11211,
150+
);
151+
}
152+
153+
if ($type === 'redis') {
154+
$config[$type] = array(
155+
'host' => !empty($host) ? $host : 'localhost',
156+
'port' => !empty($port) ? $port : 6379,
157+
'password' => !empty($password) ? $password : null,
158+
'database' => !empty($database) ? $database : 0
159+
);
160+
}
161+
162+
$this->cacheProviderLoader->loadCacheProvider($id, $config, $container);
163+
164+
return $id;
32165
}
33166
}

DependencyInjection/Configuration.php

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Caxy\HtmlDiffBundle\DependencyInjection;
44

5+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
56
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
67
use Symfony\Component\Config\Definition\ConfigurationInterface;
78

@@ -33,8 +34,43 @@ public function getConfigTreeBuilder()
3334
->booleanNode('insert_space_in_replace')
3435
->defaultFalse()
3536
->end()
37+
->booleanNode('use_table_diffing')
38+
->defaultTrue()
39+
->end()
40+
->integerNode('match_threshold')->end()
41+
->append($this->getDoctrineCacheDriverNode('doctrine_cache_driver'))
3642
->end();
3743

3844
return $treeBuilder;
3945
}
46+
47+
/**
48+
* @param string $name
49+
*
50+
* @return ArrayNodeDefinition
51+
*/
52+
private function getDoctrineCacheDriverNode($name)
53+
{
54+
$treeBuilder = new TreeBuilder();
55+
$node = $treeBuilder->root($name);
56+
$node
57+
->canBeEnabled()
58+
->beforeNormalization()
59+
->ifString()
60+
->then(function ($v) { return array('type' => $v); })
61+
->end()
62+
->children()
63+
->scalarNode('type')->defaultValue('array')->end()
64+
->scalarNode('host')->end()
65+
->scalarNode('port')->end()
66+
->scalarNode('instance_class')->end()
67+
->scalarNode('class')->end()
68+
->scalarNode('id')->end()
69+
->scalarNode('namespace')->defaultNull()->end()
70+
->scalarNode('cache_provider')->defaultNull()->end()
71+
->end()
72+
;
73+
74+
return $node;
75+
}
4076
}

Resources/config/services.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
parameters:
22
caxy.html_diff.class: Caxy\HtmlDiffBundle\Service\HtmlDiffService
3+
caxy.html_diff.config.class: Caxy\HtmlDiff\HtmlDiffConfig
34
caxy.twig.html_diff_extension.class: Caxy\HtmlDiffBundle\Twig\HtmlDiffExtension
45

56
services:
7+
caxy.html_diff.config:
8+
class: %caxy.html_diff.config.class%
9+
factory: [%caxy.html_diff.config.class%, create]
610
caxy.html_diff:
711
class: %caxy.html_diff.class%
8-
arguments: [@service_container]
12+
calls:
13+
- [setConfig, [@caxy.html_diff.config]]
914
caxy.twig.html_diff_extension:
1015
class: %caxy.twig.html_diff_extension.class%
1116
arguments: [@service_container]
1217
tags:
13-
- { name: twig.extension }
18+
- { name: twig.extension }

0 commit comments

Comments
 (0)