22
33namespace Admingenerator \FormExtensionsBundle \Twig \Extension ;
44
5- use Symfony \Component \DependencyInjection \ContainerInterface ;
6- use Symfony \Component \DependencyInjection \Container ;
7-
85/**
9- * This extension adds common twig function for various upload manager
10- * bundles and common twig filter image manipulation bundles.
11- *
12- * Depending on %admingenerator.form.upload_manager% setting a diffrent
6+ * This extension adds common twig function for various upload manager
7+ * bundles and common twig filter image manipulation bundles.
8+ *
9+ * Depending on %admingenerator.form.upload_manager% setting a diffrent
1310 * upload manager bundle is used.
14- *
15- * Depending on %admingenerator.form.image_manipulator% setting a diffrent
11+ *
12+ * Depending on %admingenerator.form.image_manipulator% setting a diffrent
1613 * image manipulation bundle is used.
17- *
14+ *
1815 * @author Piotr Gołębiewski <[email protected] > 1916 */
2017class ImageAssetsExtension extends \Twig_Extension
2118{
22- protected $ container ;
19+ protected $ uploaderExtension ;
20+
21+ protected $ filterExtension ;
2322
24- public function __construct (ContainerInterface $ container )
23+ public function __construct ($ uploaderExtension , $ filterExtension )
2524 {
26- $ this ->container = $ container ;
25+ $ this ->uploaderExtension = $ uploaderExtension ;
26+ $ this ->filterExtension = $ filterExtension ;
2727 }
2828
2929 /**
@@ -45,7 +45,7 @@ public function getFilters()
4545 'image_filter ' => new \Twig_SimpleFilter ('image_filter ' , array ($ this , 'filter ' )),
4646 );
4747 }
48-
48+
4949 /**
5050 * Gets the browser path for the image and filter to apply.
5151 *
@@ -54,20 +54,16 @@ public function getFilters()
5454 public function asset ($ object , $ field )
5555 {
5656 $ params = func_get_args ();
57-
58- if ('vich_uploader ' === $ this ->getUploadManager ()) {
59- // Note: requires v0.13.0 or higher
60- $ ext = new \Vich \UploaderBundle \Twig \Extension \UploaderExtension (
61- $ this ->container ->get ('vich_uploader.templating.helper.uploader_helper ' )
62- );
63-
64- return call_user_func_array (array ($ ext , "asset " ), $ params );
57+
58+ if ($ this ->uploaderExtension instanceof \Vich \UploaderBundle \Templating \Helper \UploaderHelper
59+ || $ this ->uploaderExtension instanceof \Vich \UploaderBundle \Twig \Extension \UploaderExtension) {
60+ return call_user_func_array (array ($ this ->uploaderExtension , "asset " ), $ params );
6561 }
66-
62+
6763 // In case no upload manager is used we expect object to have
6864 // a special method returning file's path
6965 $ getter = "get " .Container::Camelize ($ field )."WebPath " ;
70-
66+
7167 return $ object ->$ getter ();
7268 }
7369
@@ -80,54 +76,23 @@ public function filter()
8076 {
8177 $ params = func_get_args ();
8278 $ path = $ params [0 ];
83-
84- if ('liip_imagine ' === $ this ->getImageManipulator ()) {
85- if (class_exists ('\Liip\ImagineBundle\Templating\ImagineExtension ' )) {
86- $ ext = new \Liip \ImagineBundle \Templating \ImagineExtension (
87- $ this ->container ->get ('liip_imagine.cache.manager ' )
88- );
89- } else {
90- $ ext = new \Liip \ImagineBundle \Templating \FilterExtension (
91- $ this ->container ->get ('liip_imagine.cache.manager ' )
92- );
93- }
94-
95- return call_user_func_array (array ($ ext , "filter " ), $ params );
79+
80+ if (($ this ->filterExtension instanceof \Liip \ImagineBundle \Templating \ImagineExtension)
81+ || ($ this ->filterExtension instanceof \Liip \ImagineBundle \Templating \FilterExtension)) {
82+
83+ return call_user_func_array (array ($ this ->filterExtension , "filter " ), $ params );
9684 }
97-
98- if ('avalanche_imagine ' === $ this ->getImageManipulator ()) {
99- $ ext = new \Avalanche \Bundle \ImagineBundle \Templating \ImagineExtension (
100- $ this ->container ->get ('imagine.cache.path.resolver ' )
101- );
102-
103- return call_user_func_array (array ($ ext , "applyFilter " ), $ params );
85+
86+ if ($ this ->filterExtension instanceof \Avalanche \Bundle \ImagineBundle \Templating \ImagineExtension) {
87+
88+ return call_user_func_array (array ($ this ->filterExtension , "applyFilter " ), $ params );
10489 }
105-
90+
10691 // In case no image manipulator is used we
10792 // return the unmodified path
10893 return $ path ;
10994 }
11095
111- /**
112- * Get upload manager name
113- *
114- * @return string|null Parameter value
115- */
116- public function getUploadManager ()
117- {
118- return $ this ->container ->getParameter ('admingenerator.form.upload_manager ' );
119- }
120-
121- /**
122- * Get image manipulator name
123- *
124- * @return string|null Parameter value
125- */
126- public function getImageManipulator ()
127- {
128- return $ this ->container ->getParameter ('admingenerator.form.image_manipulator ' );
129- }
130-
13196 /**
13297 * Returns the name of the extension.
13398 *
0 commit comments