22
33namespace Admingenerator \FormExtensionsBundle \Twig \Extension ;
44
5- use Symfony \Component \DependencyInjection \ContainerInterface ;
6- use Symfony \Component \DependencyInjection \Container ;
75use Twig \Extension \AbstractExtension ;
86use Twig \TwigFilter ;
97use Twig \TwigFunction ;
108
119/**
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
1614 * 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
1917 * image manipulation bundle is used.
20- *
18+ *
2119 * @author Piotr Gołębiewski <[email protected] > 2220 */
2321class ImageAssetsExtension extends AbstractExtension
2422{
25- protected $ container ;
23+ protected $ uploaderExtension ;
2624
27- public function __construct (ContainerInterface $ container )
25+ protected $ filterExtension ;
26+
27+ public function __construct ($ uploaderExtension , $ filterExtension )
2828 {
29- $ this ->container = $ container ;
29+ $ this ->uploaderExtension = $ uploaderExtension ;
30+ $ this ->filterExtension = $ filterExtension ;
3031 }
3132
3233 /**
@@ -48,7 +49,7 @@ public function getFilters()
4849 'image_filter ' => new TwigFilter ('image_filter ' , array ($ this , 'filter ' )),
4950 );
5051 }
51-
52+
5253 /**
5354 * Gets the browser path for the image and filter to apply.
5455 *
@@ -57,20 +58,16 @@ public function getFilters()
5758 public function asset ($ object , $ field )
5859 {
5960 $ 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 );
6865 }
69-
66+
7067 // In case no upload manager is used we expect object to have
7168 // a special method returning file's path
7269 $ getter = "get " .Container::Camelize ($ field )."WebPath " ;
73-
70+
7471 return $ object ->$ getter ();
7572 }
7673
@@ -83,54 +80,23 @@ public function filter()
8380 {
8481 $ params = func_get_args ();
8582 $ 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 );
9988 }
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 );
10793 }
108-
94+
10995 // In case no image manipulator is used we
11096 // return the unmodified path
11197 return $ path ;
11298 }
11399
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-
134100 /**
135101 * Returns the name of the extension.
136102 *
0 commit comments