[TASK] Make ViewHelpers compilable in ext:install 11/39211/2
authorWouter Wolters <typo3@wouterwolters.nl>
Sat, 2 May 2015 18:52:51 +0000 (20:52 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 5 May 2015 19:39:19 +0000 (21:39 +0200)
Resolves: #66757
Releases: master
Change-Id: I0770af5009cccd65eb632a5d9c14d3d61cf8790f
Reviewed-on: http://review.typo3.org/39211
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/install/Classes/ViewHelpers/ConstantViewHelper.php
typo3/sysext/install/Classes/ViewHelpers/File/ExistsViewHelper.php
typo3/sysext/install/Classes/ViewHelpers/File/ImageDimensionViewHelper.php
typo3/sysext/install/Classes/ViewHelpers/File/RelativePathViewHelper.php
typo3/sysext/install/Classes/ViewHelpers/File/SizeViewHelper.php
typo3/sysext/install/Classes/ViewHelpers/Format/CropViewHelper.php
typo3/sysext/install/Classes/ViewHelpers/Format/ImageMagickCommandsViewHelper.php
typo3/sysext/install/Classes/ViewHelpers/Format/PhpErrorCodeViewHelper.php
typo3/sysext/install/Classes/ViewHelpers/PhpInfoViewHelper.php
typo3/sysext/install/Tests/Unit/ViewHelpers/Format/PhpErrorCodeViewHelperTest.php

index c2e9d5d..b728e5f 100644 (file)
@@ -14,12 +14,16 @@ namespace TYPO3\CMS\Install\ViewHelpers;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+
 /**
  * Render value of a constant
  *
  * @internal
  */
-class ConstantViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class ConstantViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Render a constant
@@ -28,7 +32,24 @@ class ConstantViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHe
         * @return string Value of constant
         */
        public function render($name) {
-               return constant($name);
+               return self::renderStatic(
+                       array(
+                               'name' => $name,
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               return constant($arguments['name']);
        }
 
 }
index ae3aa3e..75f0d0b 100644 (file)
@@ -14,6 +14,10 @@ namespace TYPO3\CMS\Install\ViewHelpers\File;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+
 /**
  * Simple view helper to check if given file is a regular file
  *
@@ -29,7 +33,7 @@ namespace TYPO3\CMS\Install\ViewHelpers\File;
  *
  * @internal
  */
-class ExistsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class ExistsViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Check if given file is a regular file
@@ -38,6 +42,25 @@ class ExistsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelp
         * @return bool
         */
        public function render($file) {
+               return self::renderStatic(
+                       array(
+                               'file' => $file,
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return bool
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $file = $arguments['file'];
+
                $result = FALSE;
                if (file_exists($file) && is_file($file)) {
                        $result = TRUE;
index f37ac53..28ef9dd 100644 (file)
@@ -14,6 +14,11 @@ namespace TYPO3\CMS\Install\ViewHelpers\File;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+
+
 /**
  * Get width or height from image file
  *
@@ -28,7 +33,7 @@ namespace TYPO3\CMS\Install\ViewHelpers\File;
  *
  * @internal
  */
-class ImageDimensionViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class ImageDimensionViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Get width / height from image file
@@ -38,6 +43,24 @@ class ImageDimensionViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\Abstract
         * @return int width or height
         */
        public function render($dimension = 'width') {
+               return self::renderStatic(
+                       array(
+                               'dimension' => $dimension,
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $dimension = $arguments['dimension'];
                if ($dimension !== 'width' && $dimension !== 'height') {
                        throw new \TYPO3\CMS\Install\ViewHelpers\Exception(
                                'Dimension must be either \'width\' or \'height\'',
index 434fb97..477ae83 100644 (file)
@@ -14,6 +14,10 @@ namespace TYPO3\CMS\Install\ViewHelpers\File;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+
 /**
  * Get file path relative to PATH_site from absolute path
  *
@@ -28,7 +32,7 @@ namespace TYPO3\CMS\Install\ViewHelpers\File;
  *
  * @internal
  */
-class RelativePathViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class RelativePathViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Get relative path
@@ -36,7 +40,22 @@ class RelativePathViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractVi
         * @return string Relative path
         */
        public function render() {
-               $absolutePath = $this->renderChildren();
+               return self::renderStatic(
+                       array(),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $absolutePath = $renderChildrenClosure();
                return \TYPO3\CMS\Core\Utility\PathUtility::stripPathSitePrefix($absolutePath);
        }
 
index fcb0865..b5aba8a 100644 (file)
@@ -15,6 +15,9 @@ namespace TYPO3\CMS\Install\ViewHelpers\File;
  */
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 
 /**
  * Get file size from file
@@ -30,7 +33,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  *
  * @internal
  */
-class SizeViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class SizeViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Get size from file
@@ -40,7 +43,26 @@ class SizeViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
         * @return int File size
         */
        public function render($format = TRUE) {
-               $absolutePathToFile = $this->renderChildren();
+               return self::renderStatic(
+                       array(
+                               'format' => $format,
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $format = $arguments['format'];
+
+               $absolutePathToFile = $renderChildrenClosure();
                if (!is_file($absolutePathToFile)) {
                        throw new \TYPO3\CMS\Install\ViewHelpers\Exception(
                                'File not found',
index b15f3d6..de58134 100644 (file)
@@ -14,6 +14,11 @@ namespace TYPO3\CMS\Install\ViewHelpers\Format;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+
+
 /**
  * Simplified crop view helper that does not need a frontend environment
  *
@@ -36,7 +41,7 @@ namespace TYPO3\CMS\Install\ViewHelpers\Format;
  *
  * @internal
  */
-class CropViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class CropViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Render the cropped text
@@ -46,13 +51,31 @@ class CropViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
         * @return string cropped text
         */
        public function render($maxCharacters) {
+               return self::renderStatic(
+                       array(
+                               'maxCharacters' => $maxCharacters,
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $maxCharacters = $arguments['maxCharacters'];
                if (empty($maxCharacters) || $maxCharacters < 1) {
                        throw new \TYPO3\CMS\Install\ViewHelpers\Exception(
                                'maxCharacters must be a positive integer',
                                1371410113
                        );
                }
-               $stringToTruncate = $this->renderChildren();
+               $stringToTruncate = $renderChildrenClosure();
                return substr($stringToTruncate, 0, $maxCharacters);
        }
 
index 0abd5b4..ccbf1ed 100644 (file)
@@ -14,12 +14,16 @@ namespace TYPO3\CMS\Install\ViewHelpers\Format;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+
 /**
  * Display image magick commands
  *
  * @internal
  */
-class ImageMagickCommandsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class ImageMagickCommandsViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Display image magick commands
@@ -28,6 +32,25 @@ class ImageMagickCommandsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\Abs
         * @return string Formatted commands
         */
        public function render(array $commands = array()) {
+               return self::renderStatic(
+                       array(
+                               'commands' => $commands,
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $commands = $arguments['commands'];
+
                $result = array();
                foreach ($commands as $commandGroup) {
                        $result[] = 'Command: ' . $commandGroup[1];
index e2a1a81..b675d2f 100644 (file)
@@ -14,17 +14,21 @@ namespace TYPO3\CMS\Install\ViewHelpers\Format;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+
 /**
  * Transform PHP error code to readable text
  *
  * @internal
  */
-class PhpErrorCodeViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class PhpErrorCodeViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * @var array
         */
-       protected $levelNames = array(
+       static protected $levelNames = array(
                E_ERROR => 'E_ERROR',
                E_WARNING => 'E_WARNING',
                E_PARSE => 'E_PARSE',
@@ -49,12 +53,30 @@ class PhpErrorCodeViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractVi
         * @return string
         */
        public function render($phpErrorCode) {
+               return self::renderStatic(
+                       array(
+                               'phpErrorCode' => $phpErrorCode,
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $phpErrorCode = $arguments['phpErrorCode'];
                $levels = array();
                if (($phpErrorCode & E_ALL) == E_ALL) {
                        $levels[] = 'E_ALL';
                        $phpErrorCode &= ~E_ALL;
                }
-               foreach ($this->levelNames as $level => $name) {
+               foreach (self::$levelNames as $level => $name) {
                        if (($phpErrorCode & $level) == $level) {
                                $levels[] = $name;
                        }
index 414bcd1..d32e2d1 100644 (file)
@@ -14,13 +14,17 @@ namespace TYPO3\CMS\Install\ViewHelpers;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+
 /**
  * Utility class for phpinfo()
  *
  * @author Patrick Broens <patrick@patrickbroens.nl>
  * @internal
  */
-class PhpInfoViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
+class PhpInfoViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
@@ -36,9 +40,24 @@ class PhpInfoViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHel
         * @return string
         */
        public function render() {
-               return $this->removeAllHtmlOutsideBody(
-                       $this->changeHtmlToHtml5(
-                               $this->getPhpInfo()
+               return self::renderStatic(
+                       array(),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        *
+        * @return string
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               return self::removeAllHtmlOutsideBody(
+                       self::changeHtmlToHtml5(
+                               self::getPhpInfo()
                        )
                );
        }
@@ -48,7 +67,7 @@ class PhpInfoViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHel
         *
         * @return string HTML page with the configuration options
         */
-       public function getPhpInfo() {
+       static protected function getPhpInfo() {
                ob_start();
                phpinfo();
 
@@ -61,7 +80,7 @@ class PhpInfoViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHel
         * @param string $html Complete HTML markup for page
         * @return string Content of the body tag
         */
-       protected function removeAllHtmlOutsideBody($html) {
+       static protected function removeAllHtmlOutsideBody($html) {
                // Delete anything outside of the body tag and the body tag itself
                $html = preg_replace('/^.*?<body.*?>/is', '', $html);
                $html = preg_replace('/<\/body>.*?$/is', '', $html);
@@ -75,7 +94,7 @@ class PhpInfoViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHel
         * @param string $html HTML markup to be cleaned
         * @return string
         */
-       protected function changeHtmlToHtml5($html) {
+       static protected function changeHtmlToHtml5($html) {
                // Delete obsolete attributes
                $html = preg_replace('#\s(cellpadding|border|width)="[^"]+"#', '', $html);
 
index 18f4b0b..f12a599 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Install\Tests\Unit\ViewHelpers\Format;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
+
 /**
  * Test case
  */
@@ -29,6 +31,9 @@ class PhpErrorCodeViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        protected function setUp() {
                $this->viewHelper = $this->getMock(\TYPO3\CMS\Install\ViewHelpers\Format\PhpErrorCodeViewHelper::class, array('dummy'));
+               /** @var RenderingContext $renderingContext */
+               $renderingContext = $this->getMock(RenderingContext::class);
+               $this->viewHelper->setRenderingContext($renderingContext);
        }
 
        /**