[TASK] Move ViewHelper arguments to initializeArguments() in ext:install
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / ViewHelpers / File / SizeViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Install\ViewHelpers\File;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
20
21 /**
22 * Get file size from file
23 *
24 * = Examples =
25 *
26 * <code title="Defaults">
27 * <f:file.size>/var/www/typo3/instance/typo3temp/foo.jpg</f:file.size>
28 * </code>
29 * <output>
30 * 1,2k
31 * </output>
32 *
33 * @internal
34 */
35 class SizeViewHelper extends AbstractViewHelper
36 {
37 /**
38 * Output is escaped already. We must not escape children, to avoid double encoding.
39 *
40 * @var bool
41 */
42 protected $escapeChildren = false;
43
44 /**
45 * Initialize arguments
46 */
47 public function initializeArguments()
48 {
49 parent::initializeArguments();
50 $this->registerArgument('format', 'bool', 'If true, file size will be formatted', false, true);
51 }
52
53 /**
54 * Get size from file
55 *
56 * @throws \TYPO3\CMS\Install\ViewHelpers\Exception
57 * @return int File size
58 */
59 public function render()
60 {
61 return static::renderStatic(
62 $this->arguments,
63 $this->buildRenderChildrenClosure(),
64 $this->renderingContext
65 );
66 }
67
68 /**
69 * @param array $arguments
70 * @param \Closure $renderChildrenClosure
71 * @param RenderingContextInterface $renderingContext
72 * @throws \TYPO3\CMS\Install\ViewHelpers\Exception
73 *
74 * @return string
75 */
76 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
77 {
78 $format = $arguments['format'];
79
80 $absolutePathToFile = $renderChildrenClosure();
81 if (!is_file($absolutePathToFile)) {
82 throw new \TYPO3\CMS\Install\ViewHelpers\Exception(
83 'File not found',
84 1369563246
85 );
86 }
87 $size = filesize($absolutePathToFile);
88 if ($format) {
89 $size = GeneralUtility::formatSize($size);
90 }
91 return $size;
92 }
93 }