이미지 크기를 조정하는 Symfony2 애플리케이션에 이미지 서비스가 있습니다. 이 서비스를 유효한 이미지 크기를 나타내는 많은 매개 변수를 사용할 수있는 방식으로 구성 가능하게 만들고 싶습니다. 예 : 이것은 현재 나의 서비스 정의입니다 :사용자 정의 태그를 사용하여 Symfony2 서비스를 정의하십시오.
my.service.image:
class: My\Service\ImageService
arguments: ["@service_container"]
어떻게 든 유효한 크기의 이미지 크기를 표시하고 싶습니다. 태그를 사용하는 방법을 살펴 보았지만이 상황에서 사용하기에 적합한 지 확실하지 않습니다.
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 }
은 무엇이며 어떻게이 다양한 "크기"내 서비스를 알리기 위해이를 구현하는 가장 좋은 방법이 될 것입니다 : 이상적으로 나는 아마 다음과 같습니다 뭔가 끝내고 싶어?
업데이트 :
좀 진전을하지만, 난 여전히이 문제에 붙어있어. 이것이 내가 지금까지 성취 한 것입니다.
나는 다른 크기 구현하는 태그를 사용했습니다 :
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 as $defintion => $attributes)
{
foreach($attributes as $attribute)
{
$definition->addSize($attribute['alias'], $attribute['width'], $attribute['height']);
}
}
}
}
: 요리 책 문서 [1]을 따라하려고
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 }
을, 나는 나의 번들에 *의 CompilerPass 클래스를 생성 결국
위의 코드는 실제로 서비스에서 addSize
메서드를 호출하고 있습니다. 위의 내용이 맞는지 확실하지 않지만 제대로 작동하는 것 같습니다.
현재 실행중인 문제는 응용 프로그램 코드에서 my.service.image
을 컨테이너에서 요청할 때 처음 인스턴스를 다시 생성하는 대신 다시 인스턴스화하는 것처럼 보입니다.
모든 통찰력을 주시면 감사하겠습니다.
[1] http://symfony.com/doc/current/components/dependency_injection/tags.html