Przeglądaj źródła

Merge pull request #3701 from greg0ire/twig_extension_refactoring

Twig extension refactoring
Christian Gripp 9 lat temu
rodzic
commit
1c6a15f39e

Plik diff jest za duży
+ 1053 - 141
Tests/Twig/Extension/SonataAdminExtensionTest.php


+ 103 - 26
Twig/Extension/SonataAdminExtension.php

@@ -17,7 +17,6 @@ use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Admin\Pool;
 use Sonata\AdminBundle\Exception\NoValueException;
-use Twig_Environment;
 
 /**
  * Class SonataAdminExtension.
@@ -52,12 +51,35 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
     public function getFilters()
     {
         return array(
-            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')),
-            new \Twig_SimpleFilter('sonata_xeditable_type', array($this, 'getXEditableType')),
+            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')
+            ),
+            new \Twig_SimpleFilter(
+                'sonata_xeditable_type',
+                array($this, 'getXEditableType')
+            ),
         );
     }
 
@@ -77,8 +99,11 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
      *
      * @return \Twig_Template
      */
-    protected function getTemplate(FieldDescriptionInterface $fieldDescription, $defaultTemplate, Twig_Environment $environment)
-    {
+    protected function getTemplate(
+        FieldDescriptionInterface $fieldDescription,
+        $defaultTemplate,
+        \Twig_Environment $environment
+    ) {
         $templateName = $fieldDescription->getTemplate() ?: $defaultTemplate;
 
         try {
@@ -87,7 +112,13 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
             $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()));
+                $this->logger->warning(sprintf(
+                    'An error occured trying to load the template "%s" for the field "%s", '.
+                    'the default template "%s" was used instead.',
+                    $templateName,
+                    $fieldDescription->getFieldName(),
+                    $defaultTemplate
+                ), array('exception' => $e));
             }
         }
 
@@ -103,9 +134,17 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
      *
      * @return string
      */
-    public function renderListElement(Twig_Environment $environment, $object, FieldDescriptionInterface $fieldDescription, $params = array())
-    {
-        $template = $this->getTemplate($fieldDescription, $fieldDescription->getAdmin()->getTemplate('base_list_field'), $environment);
+    public function renderListElement(
+        \Twig_Environment $environment,
+        $object,
+        FieldDescriptionInterface $fieldDescription,
+        $params = array()
+    ) {
+        $template = $this->getTemplate(
+            $fieldDescription,
+            $fieldDescription->getAdmin()->getTemplate('base_list_field'),
+            $environment
+        );
 
         return $this->output($fieldDescription, $template, array_merge($params, array(
             'admin'             => $fieldDescription->getAdmin(),
@@ -122,12 +161,28 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
      *
      * @return string
      */
-    public function output(FieldDescriptionInterface $fieldDescription, \Twig_Template $template, array $parameters = array(), Twig_Environment $environment)
-    {
+    public function output(
+        FieldDescriptionInterface $fieldDescription,
+        \Twig_Template $template,
+        array $parameters,
+        \Twig_Environment $environment
+    ) {
         $content = $template->render($parameters);
 
         if ($environment->isDebug()) {
-            return sprintf("\n<!-- START  \n  fieldName: %s\n  template: %s\n  compiled template: %s\n -->\n%s\n<!-- END - fieldName: %s -->",
+            $commentTemplate = <<<EOT
+
+<!-- START
+    fieldName: %s
+    template: %s
+    compiled template: %s
+    -->
+    %s
+<!-- END - fieldName: %s -->
+EOT;
+
+            return sprintf(
+                $commentTemplate,
                 $fieldDescription->getFieldName(),
                 $fieldDescription->getTemplate(),
                 $template->getTemplateName(),
@@ -151,8 +206,11 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
      *
      * @return mixed
      */
-    public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
-    {
+    public function getValueFromFieldDescription(
+        $object,
+        FieldDescriptionInterface $fieldDescription,
+        array $params = array()
+    ) {
         if (isset($params['loop']) && $object instanceof \ArrayAccess) {
             throw new \RuntimeException('remove the loop requirement');
         }
@@ -177,9 +235,16 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
      *
      * @return string
      */
-    public function renderViewElement(Twig_Environment $environment, FieldDescriptionInterface $fieldDescription, $object)
-    {
-        $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig', $environment);
+    public function renderViewElement(
+        \Twig_Environment $environment,
+        FieldDescriptionInterface $fieldDescription,
+        $object
+    ) {
+        $template = $this->getTemplate(
+            $fieldDescription,
+            'SonataAdminBundle:CRUD:base_show_field.html.twig',
+            $environment
+        );
 
         try {
             $value = $fieldDescription->getValue($object);
@@ -204,9 +269,17 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
      *
      * @return string
      */
-    public function renderViewElementCompare(Twig_Environment $environment, FieldDescriptionInterface $fieldDescription, $baseObject, $compareObject)
-    {
-        $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_show_field.html.twig', $environment);
+    public function renderViewElementCompare(
+        \Twig_Environment $environment,
+        FieldDescriptionInterface $fieldDescription,
+        $baseObject,
+        $compareObject
+    ) {
+        $template = $this->getTemplate(
+            $fieldDescription,
+            'SonataAdminBundle:CRUD:base_show_field.html.twig',
+            $environment
+        );
 
         try {
             $baseValue = $fieldDescription->getValue($baseObject);
@@ -265,14 +338,18 @@ class SonataAdminExtension extends \Twig_Extension implements \Twig_Extension_In
             $method = $fieldDescription->getOption('associated_tostring');
 
             if ($method) {
-                @trigger_error('Option "associated_tostring" is deprecated since version 2.3. Use "associated_property" instead.', E_USER_DEPRECATED);
+                @trigger_error(
+                    'Option "associated_tostring" is deprecated since version 2.3. Use "associated_property" instead.',
+                    E_USER_DEPRECATED
+                );
             } else {
                 $method = '__toString';
             }
 
             if (!method_exists($element, $method)) {
                 throw new \RuntimeException(sprintf(
-                    'You must define an `associated_property` option or create a `%s::__toString` method to the field option %s from service %s is ',
+                    'You must define an `associated_property` option or '.
+                    'create a `%s::__toString` method to the field option %s from service %s is ',
                     get_class($element),
                     $fieldDescription->getName(),
                     $fieldDescription->getAdmin()->getCode()