i have image service in symfony2 application resizes images. want able make service configurable in such way can take number of parameters indicate valid image sizes. e.g. current service definition:
my.service.image: class: my\service\imageservice arguments: ["@service_container"]
somehow indicate valid number of image sizes. have looked using tags i'm not sure if appropriate use in situation. in ideal world want end looks this:
my.service.image: class: my\service\imageservice arguments: ["@service_container"] sizes: - { name: small, width: 100, height: 100 } - { name: medium, width: 100, height: 100 } - { name: large, width: 100, height: 100 }
what best way implement , how make service aware of various "sizes"?
update:
i have made progress i'm still stuck on issue. have accomplished far.
i've used tags implement different sizes:
my.service.image: class: my\service\imageservice arguments: ["@service_container"] tags: - { name: my.service.image.size, alias: small, width: 100, height: 100 } - { name: my.service.image.size, alias: medium, width: 200, height: 200 } - { name: my.service.image.size, alias: large, width: 300, height: 300 }
trying follow cookbook documentation [1], ended creating *compilerpass class in bundle:
namespace my\bundle\myimagebundle\dependencyinjection\compiler; use symfony\component\dependencyinjection\containerbuilder; use symfony\component\dependencyinjection\compiler\compilerpassinterface; use symfony\component\dependencyinjection\reference; class imageservicesizecompilerpass implements compilerpassinterface { public function process( containerbuilder $container ) { $definition = $container->get( 'my.service.image' ); $taggedservices = $container->findtaggedserviceids( 'my.service.image.size' ); foreach( $taggedservices $defintion => $attributes ) { foreach( $attributes $attribute ) { $definition->addsize( $attribute['alias'], $attribute['width'], $attribute['height'] ); } } } }
the above calling addsize
method on service. i'm not sure if above correct seems work ok.
the problem i'm running when in application code request my.service.image
container seems instantiate again rather returning instance created first time.
any insight appreciated.
[1] http://symfony.com/doc/current/components/dependency_injection/tags.html
i'm not sure exaclty use case want give following hints
are these images (their paths) saved attribute of entity ? why not use annotations @ entity directly?
if want have configureable why not creating real config out of ?
if want pass service (and not want create config) make paramters inside yml file out of sizes , pass them service or fetch parameters service using $container->getparameter('name'); (assuming have container injected)
hope help, nixo