symfony2自定义注释转换

时间:2013-12-23 10:53:46

标签: symfony annotations translation

请帮我翻译自定义注释。 我正在尝试翻译@Render(title =“Page”)。转换生成器没有找到这个,标题没有traslate。 我试着理解它是如何在组件验证Symfony中完成的,但没有任何反应。

<?php
    namespace Shooos\ProductBundle\Controller\Admin;

    use Sensio\Bundle\FrameworkExtraBundle\Configuration as PRS;
    use Shooos\CoreBundle\Controller\BaseController;
    use Aft\RenderParkingBundle\Annotations as CA;
    use Gedmo\Mapping\Annotation\Translatable;

    /**
     * @PRS\Route("/admin")
     * Class CategoryController
     * @package Shooos\ProductBundle\Controller\Admin
     */
    class CategoryController extends BaseController
    {
        /**
         * @CA\Render(title="Categories")
         * @PRS\Route("/categories", name="admin.categories")
         */
         public function indexAction()
         {
         }
    }

<?php
namespace Aft\RenderParkingBundle\Annotations\Driver;

use Doctrine\Common\Annotations\Reader;
use Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Aft\RenderParkingBundle\Annotations;
use Symfony\Component\Translation\TranslatorInterface;

class AnnotationDriver
{
    /**
     * @var Reader
     */
    private $reader;

    /**
     * @var TemplateGuesser
     */
    private $guesser;

    /**
     * @var TranslatorInterface
     */
    private $translator;

    public function __construct(Reader $reader, TemplateGuesser $guesser, TranslatorInterface $translator)
    {
        $this->reader = $reader;
        $this->guesser = $guesser;
        $this->translator = $translator;
    }

    /**
     * This event occurs when call any controller
     */
    public function onKernelController(FilterControllerEvent $event)
    {
        /** Controller exists */
        if (!is_array($controller = $event->getController())) {
            return;
        }

        /**
         * Controller
         * @var \ReflectionObject $object
         */
        $object = new \ReflectionObject($controller[0]);
        $method = $object->getMethod($controller[1]);
        foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
            if ($configuration instanceof Annotations\Render) {
                $request = $event->getRequest();

                $title = $this->translator->trans($configuration->getTitle());

                $request->attributes->set('_page_title', $title);

                if (null === $configuration->getTemplate()) {
                    $configuration->setTemplate(
                        $this->guesser->guessTemplateName(
                            $controller,
                            $request
                        ));
                }

                $request->attributes->set('_page_template', $configuration->getTemplate());
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在注释对象转换器上,注入注释阅读器,注入转换器服务并在转换过程中将值从注释转换为对象。

     $description = $this->translator->trans($transformedAnnotationObject->getDescription());