836c9ce3b7df106f185b9489a1ef08baf69479ab
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / Service / AbstractGenerator.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Service;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 /**
15 * Common base class for XML generators.
16 */
17 abstract class AbstractGenerator {
18
19 /**
20 * The reflection class for AbstractViewHelper. Is needed quite often, that's why we use a pre-initialized one.
21 *
22 * @var \TYPO3\CMS\Extbase\Reflection\ClassReflection
23 */
24 protected $abstractViewHelperReflectionClass;
25
26 /**
27 * The doc comment parser.
28 *
29 * @var \TYPO3\CMS\Extbase\Reflection\DocCommentParser
30 * @inject
31 */
32 protected $docCommentParser;
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
36 * @inject
37 */
38 protected $reflectionService;
39
40 /**
41 * Constructor. Sets $this->abstractViewHelperReflectionClass
42 *
43 */
44 public function __construct() {
45 \TYPO3\CMS\Fluid\Fluid::$debugMode = TRUE; // We want ViewHelper argument documentation
46 $this->abstractViewHelperReflectionClass = new \TYPO3\CMS\Extbase\Reflection\ClassReflection('TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper');
47 }
48
49 /**
50 * Get all class names inside this namespace and return them as array.
51 *
52 * @param string $namespace
53 * @return array Array of all class names inside a given namespace.
54 */
55 protected function getClassNamesInNamespace($namespace) {
56 $affectedViewHelperClassNames = array();
57
58 $allViewHelperClassNames = $this->reflectionService->getAllSubClassNamesForClass('TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper');
59 foreach ($allViewHelperClassNames as $viewHelperClassName) {
60 if ($this->reflectionService->isClassAbstract($viewHelperClassName)) {
61 continue;
62 }
63 if (strncmp($namespace, $viewHelperClassName, strlen($namespace)) === 0) {
64 $affectedViewHelperClassNames[] = $viewHelperClassName;
65 }
66 }
67 sort($affectedViewHelperClassNames);
68 return $affectedViewHelperClassNames;
69 }
70
71 /**
72 * Get a tag name for a given ViewHelper class.
73 * Example: For the View Helper TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper, and the
74 * namespace prefix TYPO3\CMS\Fluid\ViewHelpers\, this method returns "form.select".
75 *
76 * @param string $className Class name
77 * @param string $namespace Base namespace to use
78 * @return string Tag name
79 */
80 protected function getTagNameForClass($className, $namespace) {
81 /// Strip namespace from the beginning and "ViewHelper" from the end of the class name
82 $strippedClassName = substr($className, strlen($namespace), -10);
83 $classNameParts = explode(\TYPO3\CMS\Fluid\Fluid::NAMESPACE_SEPARATOR, $strippedClassName);
84 return implode(
85 '.',
86 array_map(
87 function($element) {
88 return lcfirst($element);
89 },
90 $classNameParts
91 )
92 );
93 }
94
95 /**
96 * Add a child node to $parentXmlNode, and wrap the contents inside a CDATA section.
97 *
98 * @param \SimpleXMLElement $parentXmlNode Parent XML Node to add the child to
99 * @param string $childNodeName Name of the child node
100 * @param string $childNodeValue Value of the child node. Will be placed inside CDATA.
101 * @return \SimpleXMLElement the new element
102 */
103 protected function addChildWithCData(\SimpleXMLElement $parentXmlNode, $childNodeName, $childNodeValue) {
104 $parentDomNode = dom_import_simplexml($parentXmlNode);
105 $domDocument = new \DOMDocument();
106
107 $childNode = $domDocument->appendChild($domDocument->createElement($childNodeName));
108 $childNode->appendChild($domDocument->createCDATASection($childNodeValue));
109 $childNodeTarget = $parentDomNode->ownerDocument->importNode($childNode, true);
110 $parentDomNode->appendChild($childNodeTarget);
111 return simplexml_import_dom($childNodeTarget);
112 }
113
114 }