2
2
3
3
namespace Caxy \HtmlDiffBundle \DependencyInjection ;
4
4
5
+ use Caxy \HtmlDiff \HtmlDiffConfig ;
6
+ use Doctrine \Bundle \DoctrineCacheBundle \DependencyInjection \CacheProviderLoader ;
7
+ use Symfony \Component \DependencyInjection \Alias ;
5
8
use Symfony \Component \DependencyInjection \ContainerBuilder ;
6
9
use Symfony \Component \Config \FileLocator ;
10
+ use Symfony \Component \DependencyInjection \Reference ;
7
11
use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
8
12
use Symfony \Component \DependencyInjection \Loader \YamlFileLoader ;
9
13
14
18
*/
15
19
class CaxyHtmlDiffExtension extends Extension
16
20
{
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
+
17
36
/**
18
37
* {@inheritDoc}
19
38
*/
@@ -22,12 +41,126 @@ public function load(array $configs, ContainerBuilder $container)
22
41
$ configuration = new Configuration ();
23
42
$ config = $ this ->processConfiguration ($ configuration , $ configs );
24
43
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
+ }
26
50
27
51
foreach ($ config as $ key => $ value ) {
28
52
$ container ->setParameter ($ this ->getAlias () . '. ' . $ key , $ value );
29
53
}
30
54
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 ' ));
31
66
$ 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 ;
32
165
}
33
166
}
0 commit comments