2
2
3
3
namespace Admingenerator \FormExtensionsBundle \Twig \Extension ;
4
4
5
- use Symfony \Component \DependencyInjection \ContainerInterface ;
6
- use Symfony \Component \DependencyInjection \Container ;
7
5
use Twig \Extension \AbstractExtension ;
8
6
use Twig \TwigFilter ;
9
7
use Twig \TwigFunction ;
10
8
11
9
/**
12
- * This extension adds common twig function for various upload manager
13
- * bundles and common twig filter image manipulation bundles.
14
- *
15
- * Depending on %admingenerator.form.upload_manager% setting a diffrent
10
+ * This extension adds common twig function for various upload manager
11
+ * bundles and common twig filter image manipulation bundles.
12
+ *
13
+ * Depending on %admingenerator.form.upload_manager% setting a diffrent
16
14
* upload manager bundle is used.
17
- *
18
- * Depending on %admingenerator.form.image_manipulator% setting a diffrent
15
+ *
16
+ * Depending on %admingenerator.form.image_manipulator% setting a diffrent
19
17
* image manipulation bundle is used.
20
- *
18
+ *
21
19
* @author Piotr Gołębiewski <[email protected] >
22
20
*/
23
21
class ImageAssetsExtension extends AbstractExtension
24
22
{
25
- protected $ container ;
23
+ protected $ uploaderExtension ;
26
24
27
- public function __construct (ContainerInterface $ container )
25
+ protected $ filterExtension ;
26
+
27
+ public function __construct ($ uploaderExtension , $ filterExtension )
28
28
{
29
- $ this ->container = $ container ;
29
+ $ this ->uploaderExtension = $ uploaderExtension ;
30
+ $ this ->filterExtension = $ filterExtension ;
30
31
}
31
32
32
33
/**
@@ -48,7 +49,7 @@ public function getFilters()
48
49
'image_filter ' => new TwigFilter ('image_filter ' , array ($ this , 'filter ' )),
49
50
);
50
51
}
51
-
52
+
52
53
/**
53
54
* Gets the browser path for the image and filter to apply.
54
55
*
@@ -57,20 +58,16 @@ public function getFilters()
57
58
public function asset ($ object , $ field )
58
59
{
59
60
$ params = func_get_args ();
60
-
61
- if ('vich_uploader ' === $ this ->getUploadManager ()) {
62
- // Note: requires v0.13.0 or higher
63
- $ ext = new \Vich \UploaderBundle \Twig \Extension \UploaderExtension (
64
- $ this ->container ->get ('vich_uploader.templating.helper.uploader_helper ' )
65
- );
66
-
67
- return call_user_func_array (array ($ ext , "asset " ), $ params );
61
+
62
+ if ($ this ->uploaderExtension instanceof \Vich \UploaderBundle \Templating \Helper \UploaderHelper
63
+ || $ this ->uploaderExtension instanceof \Vich \UploaderBundle \Twig \Extension \UploaderExtension) {
64
+ return call_user_func_array (array ($ this ->uploaderExtension , "asset " ), $ params );
68
65
}
69
-
66
+
70
67
// In case no upload manager is used we expect object to have
71
68
// a special method returning file's path
72
69
$ getter = "get " .Container::Camelize ($ field )."WebPath " ;
73
-
70
+
74
71
return $ object ->$ getter ();
75
72
}
76
73
@@ -83,54 +80,23 @@ public function filter()
83
80
{
84
81
$ params = func_get_args ();
85
82
$ path = $ params [0 ];
86
-
87
- if ('liip_imagine ' === $ this ->getImageManipulator ()) {
88
- if (class_exists ('\Liip\ImagineBundle\Templating\ImagineExtension ' )) {
89
- $ ext = new \Liip \ImagineBundle \Templating \ImagineExtension (
90
- $ this ->container ->get ('liip_imagine.cache.manager ' )
91
- );
92
- } else {
93
- $ ext = new \Liip \ImagineBundle \Templating \FilterExtension (
94
- $ this ->container ->get ('liip_imagine.cache.manager ' )
95
- );
96
- }
97
-
98
- return call_user_func_array (array ($ ext , "filter " ), $ params );
83
+
84
+ if (($ this ->filterExtension instanceof \Liip \ImagineBundle \Templating \ImagineExtension)
85
+ || ($ this ->filterExtension instanceof \Liip \ImagineBundle \Templating \FilterExtension)) {
86
+
87
+ return call_user_func_array (array ($ this ->filterExtension , "filter " ), $ params );
99
88
}
100
-
101
- if ('avalanche_imagine ' === $ this ->getImageManipulator ()) {
102
- $ ext = new \Avalanche \Bundle \ImagineBundle \Templating \ImagineExtension (
103
- $ this ->container ->get ('imagine.cache.path.resolver ' )
104
- );
105
-
106
- return call_user_func_array (array ($ ext , "applyFilter " ), $ params );
89
+
90
+ if ($ this ->filterExtension instanceof \Avalanche \Bundle \ImagineBundle \Templating \ImagineExtension) {
91
+
92
+ return call_user_func_array (array ($ this ->filterExtension , "applyFilter " ), $ params );
107
93
}
108
-
94
+
109
95
// In case no image manipulator is used we
110
96
// return the unmodified path
111
97
return $ path ;
112
98
}
113
99
114
- /**
115
- * Get upload manager name
116
- *
117
- * @return string|null Parameter value
118
- */
119
- public function getUploadManager ()
120
- {
121
- return $ this ->container ->getParameter ('admingenerator.form.upload_manager ' );
122
- }
123
-
124
- /**
125
- * Get image manipulator name
126
- *
127
- * @return string|null Parameter value
128
- */
129
- public function getImageManipulator ()
130
- {
131
- return $ this ->container ->getParameter ('admingenerator.form.image_manipulator ' );
132
- }
133
-
134
100
/**
135
101
* Returns the name of the extension.
136
102
*
0 commit comments