소스 검색

drop initRuntime() in favor of needs_environment

Grégoire Paris 9 년 전
부모
커밋
dcb47ba227
2개의 변경된 파일23개의 추가작업 그리고 37개의 파일을 삭제
  1. 6 8
      Tests/Twig/Extension/SonataAdminExtensionTest.php
  2. 17 29
      Twig/Extension/SonataAdminExtension.php

+ 6 - 8
Tests/Twig/Extension/SonataAdminExtensionTest.php

@@ -108,8 +108,6 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
         $this->environment->addExtension(new RoutingExtension($urlGenerator));
         $this->environment->addExtension(new \Twig_Extensions_Extension_Text());
 
-        $this->twigExtension->initRuntime($this->environment);
-
         // initialize object
         $this->object = new \stdClass();
 
@@ -227,7 +225,7 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
                 }
             }));
 
-        $this->assertSame($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($this->object, $this->fieldDescription))));
+        $this->assertSame($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription))));
     }
 
     public function getRenderListElementTests()
@@ -381,7 +379,7 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
             ->method('warning')
             ->with(($this->stringStartsWith('An error occured trying to load the template "SonataAdminBundle:CRUD:list_nonexistent_template.html.twig" for the field "Foo_name", the default template "SonataAdminBundle:CRUD:base_list_field.html.twig" was used instead: "Unable to find template "list_nonexistent_template.html.twig')));
 
-        $this->twigExtension->renderListElement($this->object, $this->fieldDescription);
+        $this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription);
     }
 
     /**
@@ -399,7 +397,7 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
             ->method('getTemplate')
             ->will($this->returnValue('SonataAdminBundle:CRUD:list_nonexistent_template.html.twig'));
 
-        $this->twigExtension->renderListElement($this->object, $this->fieldDescription);
+        $this->twigExtension->renderListElement($this->environment, $this->object, $this->fieldDescription);
     }
 
     /**
@@ -460,7 +458,7 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
                 }
             }));
 
-        $this->assertSame($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderViewElement($this->fieldDescription, $this->object))));
+        $this->assertSame($expected, trim(preg_replace('/\s+/', ' ', $this->twigExtension->renderViewElement($this->environment, $this->fieldDescription, $this->object))));
     }
 
     public function getRenderViewElementTests()
@@ -675,11 +673,11 @@ class SonataAdminExtensionTest extends \PHPUnit_Framework_TestCase
         $template = $this->environment->loadTemplate('SonataAdminBundle:CRUD:base_list_field.html.twig');
 
         $this->assertSame('<td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td>',
-                trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
+                trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters, $this->environment))));
 
         $this->environment->enableDebug();
         $this->assertSame('<!-- START fieldName: fd_name template: SonataAdminBundle:CRUD:base_list_field.html.twig compiled template: SonataAdminBundle:CRUD:base_list_field.html.twig --> <td class="sonata-ba-list-field sonata-ba-list-field-" objectId="12345"> foo </td> <!-- END - fieldName: fd_name -->',
-                trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters))));
+                trim(preg_replace('/\s+/', ' ', $this->twigExtension->output($this->fieldDescription, $template, $parameters, $this->environment))));
     }
 
     public function testRenderRelationElementNoObject()

+ 17 - 29
Twig/Extension/SonataAdminExtension.php

@@ -17,14 +17,10 @@ use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Admin\Pool;
 use Sonata\AdminBundle\Exception\NoValueException;
 use Symfony\Component\PropertyAccess\PropertyAccess;
+use Twig_Environment;
 
 class SonataAdminExtension extends \Twig_Extension
 {
-    /**
-     * @var \Twig_Environment
-     */
-    protected $environment;
-
     /**
      * @var Pool
      */
@@ -45,22 +41,14 @@ class SonataAdminExtension extends \Twig_Extension
         $this->logger = $logger;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function initRuntime(\Twig_Environment $environment)
-    {
-        $this->environment = $environment;
-    }
-
     /**
      * {@inheritdoc}
      */
     public function getFilters()
     {
         return array(
-            new \Twig_SimpleFilter('render_list_element', array($this, 'renderListElement'), array('is_safe' => array('html'))),
-            new \Twig_SimpleFilter('render_view_element', array($this, 'renderViewElement'), array('is_safe' => array('html'))),
+            new \Twig_SimpleFilter('render_list_element', array($this, 'renderListElement'), array('is_safe' => array('html'), 'needs_environment' => true)),
+            new \Twig_SimpleFilter('render_view_element', array($this, 'renderViewElement'), array('is_safe' => array('html'), 'needs_environment' => true)),
             new \Twig_SimpleFilter('render_view_element_compare', array($this, 'renderViewElementCompare'), array('is_safe' => array('html'))),
             new \Twig_SimpleFilter('render_relation_element', array($this, 'renderRelationElement')),
             new \Twig_SimpleFilter('sonata_urlsafeid', array($this, 'getUrlsafeIdentifier')),
@@ -84,14 +72,14 @@ class SonataAdminExtension extends \Twig_Extension
      *
      * @return \Twig_Template
      */
-    protected function getTemplate(FieldDescriptionInterface $fieldDescription, $defaultTemplate)
+    protected function getTemplate(FieldDescriptionInterface $fieldDescription, $defaultTemplate, Twig_Environment $environment)
     {
         $templateName = $fieldDescription->getTemplate() ?: $defaultTemplate;
 
         try {
-            $template = $this->environment->loadTemplate($templateName);
+            $template = $environment->loadTemplate($templateName);
         } catch (\Twig_Error_Loader $e) {
-            $template = $this->environment->loadTemplate($defaultTemplate);
+            $template = $environment->loadTemplate($defaultTemplate);
 
             if (null !== $this->logger) {
                 $this->logger->warning(sprintf('An error occured trying to load the template "%s" for the field "%s", the default template "%s" was used instead: "%s". ', $templateName, $fieldDescription->getFieldName(), $defaultTemplate, $e->getMessage()));
@@ -110,16 +98,16 @@ class SonataAdminExtension extends \Twig_Extension
      *
      * @return string
      */
-    public function renderListElement($object, FieldDescriptionInterface $fieldDescription, $params = array())
+    public function renderListElement(Twig_Environment $environment, $object, FieldDescriptionInterface $fieldDescription, $params = array())
     {
-        $template = $this->getTemplate($fieldDescription, $fieldDescription->getAdmin()->getTemplate('base_list_field'));
+        $template = $this->getTemplate($fieldDescription, $fieldDescription->getAdmin()->getTemplate('base_list_field'), $environment);
 
         return $this->output($fieldDescription, $template, array_merge($params, array(
             'admin'             => $fieldDescription->getAdmin(),
             'object'            => $object,
             'value'             => $this->getValueFromFieldDescription($object, $fieldDescription),
             'field_description' => $fieldDescription,
-        )));
+        )), $environment);
     }
 
     /**
@@ -129,11 +117,11 @@ class SonataAdminExtension extends \Twig_Extension
      *
      * @return string
      */
-    public function output(FieldDescriptionInterface $fieldDescription, \Twig_Template $template, array $parameters = array())
+    public function output(FieldDescriptionInterface $fieldDescription, \Twig_Template $template, array $parameters = array(), Twig_Environment $environment)
     {
         $content = $template->render($parameters);
 
-        if ($this->environment->isDebug()) {
+        if ($environment->isDebug()) {
             return sprintf("\n<!-- START  \n  fieldName: %s\n  template: %s\n  compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->",
                 $fieldDescription->getFieldName(),
                 $fieldDescription->getTemplate(),
@@ -184,9 +172,9 @@ class SonataAdminExtension extends \Twig_Extension
      *
      * @return string
      */
-    public function renderViewElement(FieldDescriptionInterface $fieldDescription, $object)
+    public function renderViewElement(Twig_Environment $environment, FieldDescriptionInterface $fieldDescription, $object)
     {
-        $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
+        $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig', $environment);
 
         try {
             $value = $fieldDescription->getValue($object);
@@ -199,7 +187,7 @@ class SonataAdminExtension extends \Twig_Extension
             'object'            => $object,
             'value'             => $value,
             'admin'             => $fieldDescription->getAdmin(),
-        ));
+        ), $environment);
     }
 
     /**
@@ -211,9 +199,9 @@ class SonataAdminExtension extends \Twig_Extension
      *
      * @return string
      */
-    public function renderViewElementCompare(FieldDescriptionInterface $fieldDescription, $baseObject, $compareObject)
+    public function renderViewElementCompare(Twig_Environment $environment, FieldDescriptionInterface $fieldDescription, $baseObject, $compareObject)
     {
-        $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig');
+        $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig', $environment);
 
         try {
             $baseValue = $fieldDescription->getValue($baseObject);
@@ -248,7 +236,7 @@ class SonataAdminExtension extends \Twig_Extension
             'value_compare'     => $compareValue,
             'is_diff'           => $isDiff,
             'admin'             => $fieldDescription->getAdmin(),
-        ));
+        ), $environment);
     }
 
     /**