Skip to content
Snippets Groups Projects
Commit 1db19b2c authored by damjan's avatar damjan
Browse files

[FEATURE] Export adapted for types, properties and pseudo types

parent f7793674
No related branches found
No related tags found
No related merge requests found
......@@ -5,8 +5,9 @@ namespace SGalinski\TypoScriptBackendBundle\Service;
use Doctrine\Bundle\DoctrineBundle\Registry;
use DOMDocument;
use Monolog\Logger;
use SGalinski\TypoScriptBackendBundle\Entity\Attribute;
use SGalinski\TypoScriptBackendBundle\Entity\AttributeRepository;
use SGalinski\TypoScriptBackendBundle\Entity\Property;
use SGalinski\TypoScriptBackendBundle\Entity\Type;
use SGalinski\TypoScriptBackendBundle\Entity\TypeRepository;
use SGalinski\TypoScriptBackendBundle\Utility\CdataSupportingSimpleXMLElement;
/**
......@@ -43,7 +44,7 @@ class TypoScriptReferenceExportService {
* @param int $typo3Group
* @return void
*/
public function export($fileAddress, $typo3Version = NULL, $typo3Group = Attribute::NORMAL_GROUP) {
public function export($fileAddress, $typo3Version = NULL, $typo3Group = Type::NORMAL_GROUP) {
$tsrefXmlString = $this->getTsrefString($typo3Version, $typo3Group);
file_put_contents($fileAddress, $tsrefXmlString);
......@@ -57,12 +58,15 @@ class TypoScriptReferenceExportService {
* @param integer $typo3Group - Attribute::NORMAL_GROUP | Attribute::PAGE_GROUP | Attribute::USER_GROUP
* @return string
*/
public function getTsrefString($typo3Version = NULL, $typo3Group = Attribute::NORMAL_GROUP) {
/** @var AttributeRepository $attributeRepository */
$attributeRepository = $this->doctrine->getRepository('TypoScriptBackendBundle:Attribute');
$typeAttributes = $attributeRepository->findByVersionInRange(TRUE, $typo3Group, $typo3Version);
public function getTsrefString($typo3Version = NULL, $typo3Group = Type::NORMAL_GROUP) {
/** @var TypeRepository $typeRepository */
$typeRepository = $this->doctrine->getRepository('TypoScriptBackendBundle:Type');
$types = $typeRepository->findByVersionInRange($typo3Group, $typo3Version);
$types = $this->makePseudoTypesFromSubProperties($types);
/** @var CdataSupportingSimpleXMLElement $tsrefXml */
$tsrefXml = $this->phpToXml($typeAttributes);
$tsrefXml = $this->phpToXml($types);
$tsrefXmlString = $this->formatXml($tsrefXml);
return $tsrefXmlString;
}
......@@ -70,77 +74,69 @@ class TypoScriptReferenceExportService {
/**
* Makes XML (SimpleXMLElement) from php array.
*
* @param array $typeAttributes
* @param array $types
* @return CdataSupportingSimpleXMLElement
*/
protected function phpToXml(array $typeAttributes) {
protected function phpToXml(array $types) {
$tsrefXml = new CdataSupportingSimpleXMLElement(
'<?xml version="1.0" encoding="UTF-8"?>
<tsRef></tsRef>'
);
// Some checks
if (!is_array($typeAttributes)) {
if (!is_array($types)) {
$message = 'Not an array. Array of attributes expected.';
$this->logger->addError($message);
throw new \RuntimeException ($message);
}
if (empty($typeAttributes)) {
if (empty($types)) {
$this->logger->addWarning('Type array is empty.');
return $tsrefXml;
}
//Write XML
/** @var Attribute $typeAttribute */
foreach ($typeAttributes as $typeAttribute) {
if (!$typeAttribute->getIsType()) {
$this->logger->addWarning('Non-type (' . $typeAttribute->getName() . ') found in the type array.');
continue;
}
/** @var Type $type */
foreach ($types as $type) {
/** @var CdataSupportingSimpleXMLElement $typeTag */
$typeTag = $tsrefXml->addChild('type');
$typeTag->addAttribute('id', $typeAttribute->getName());
if ($typeAttribute->getParent() != NULL) {
$typeTag->addAttribute('extends', $typeAttribute->getParent()->getName());
$typeTag->addAttribute('id', $type->getName());
if ($type->getExtends() !== NULL) {
$typeTag->addAttribute('extends', $type->getExtends()->getName());
}
if ($typeAttribute->getMinVersion() != NULL) {
$typeTag->addAttribute('minVersion', $typeAttribute->getMinVersion());
if ($type->getMinVersion() !== NULL) {
$typeTag->addAttribute('minVersion', $type->getMinVersion());
}
if ($typeAttribute->getMaxVersion() != NULL) {
$typeTag->addAttribute('maxVersion', $typeAttribute->getMaxVersion());
if ($type->getMaxVersion() !== NULL) {
$typeTag->addAttribute('maxVersion', $type->getMaxVersion());
}
if ($typeAttribute->getUrlName() != NULL) {
$typeTag->addAttribute('urlName', $typeAttribute->getUrlName());
if ($type->getUrlName() !== NULL) {
$typeTag->addAttribute('urlName', $type->getUrlName());
}
if ($typeAttribute->getDescription() != NULL) {
$typeTag->addChildCData('description', $typeAttribute->getDescription());
if ($type->getDescription() !== NULL) {
$typeTag->addChildCData('description', $type->getDescription());
}
if ($typeAttribute->getChildren() != NULL) {
/** @var Attribute $propertyAttribute */
foreach ($typeAttribute->getChildren() as $propertyAttribute) {
if ($propertyAttribute->getIsType()) {
continue;
}
if ($type->getChildren() !== NULL) {
/** @var Property $property */
foreach ($type->getChildren() as $property) {
/** @var CdataSupportingSimpleXMLElement $propertyTag */
$propertyTag = $typeTag->addChild('property');
$propertyTag->addAttribute('name', $propertyAttribute->getName());
$propertyTag->addAttribute('type', $propertyAttribute->getType()->getName());
if ($propertyAttribute->getMinVersion() != NULL) {
$propertyTag->addAttribute('minVersion', $propertyAttribute->getMinVersion());
$propertyTag->addAttribute('name', $property->getName());
$propertyTag->addAttribute('type', $property->getType()->getName());
if ($property->getMinVersion() !== NULL) {
$propertyTag->addAttribute('minVersion', $property->getMinVersion());
}
if ($propertyAttribute->getMaxVersion() != NULL) {
$propertyTag->addAttribute('maxVersion', $propertyAttribute->getMaxVersion());
if ($property->getMaxVersion() !== NULL) {
$propertyTag->addAttribute('maxVersion', $property->getMaxVersion());
}
if ($propertyAttribute->getUrlName() != NULL) {
$propertyTag->addAttribute('urlName', $propertyAttribute->getUrlName());
if ($property->getUrlName() !== NULL) {
$propertyTag->addAttribute('urlName', $property->getUrlName());
}
if ($propertyAttribute->getDescription() != NULL) {
$propertyTag->addChildCData('description', $propertyAttribute->getDescription());
if ($property->getDescription() !== NULL) {
$propertyTag->addChildCData('description', $property->getDescription());
}
if ($propertyAttribute->getDefault() != NULL) {
$propertyTag->addChildCData('default', $propertyAttribute->getDefault());
if ($property->getDefault() !== NULL) {
$propertyTag->addChildCData('default', $property->getDefault());
}
}
}
......@@ -163,4 +159,83 @@ class TypoScriptReferenceExportService {
$formattedXml = $dom->saveXML();
return $formattedXml;
}
/**
* Makes pseudo types from sub properties and adds them to type list which is returned.
* Name format: <type name>_<property name>[_<sub property name>*]
* Example:
* <type id="CARRAY+TDParams_TDParams_firstSubProperty!" extends="CARRAY+TDParams_TDParams" minVersion="4.3"
* urlName="CARRAY_TDParams_TDParams_firstSubProperty_">
*
* @param array $types
* @return array
*/
private function makePseudoTypesFromSubProperties(array $types) {
/** @var Type $type */
foreach ($types as $key => $type) {
if ($type->getDeleted()) {
continue;
}
/** @var Property $property */
foreach ($type->getChildren() as $property) {
if ($property->getDeleted()) {
continue;
}
$pseudoTypes = $this->handleSubProperties($property, $type);
if (!empty($pseudoTypes)) {
array_splice($types, $key + 1, 0, $pseudoTypes);
}
}
}
return $types;
}
/**
* Returns array of pseudo types made from properties which have sub properties.
*
* @param Property $parentProperty
* @param Type $parentType
* @param array $pseudoTypesArray
* @return array
*/
protected function handleSubProperties(Property $parentProperty, Type $parentType, $pseudoTypesArray = []) {
$children = $parentProperty->getChildren();
foreach ($children as $key => $property) {
if ($property->getDeleted()) {
unset($children[$key]);
}
}
if ($children === NULL || sizeof($children) <= 0) {
return $pseudoTypesArray;
}
$pseudoType = $this->createPseudoType($parentProperty, $parentType, $children);
$pseudoTypesArray[] = $pseudoType;
foreach ($children as $property) {
$pseudoTypesArray = $this->handleSubProperties($property, $pseudoType, $pseudoTypesArray);
}
return $pseudoTypesArray;
}
/**
* Creates and initialises a pseudo type.
*
* @param Property $parentProperty
* @param Type $parentType
* @param $children
* @return Type
*/
protected function createPseudoType(Property $parentProperty, Type $parentType, $children) {
$pseudoType = new Type();
$pseudoType->setName($parentType->getName() . '_' . $parentProperty->getName());
$pseudoType->setMinVersion($parentProperty->getMinVersion());
$pseudoType->setMaxVersion($parentProperty->getMaxVersion());
$pseudoType->setTypo3Group($parentProperty->getTypo3Group());
$pseudoType->setDescription($parentProperty->getDescription());
$pseudoType->setChildren($children);
$pseudoType->setExtends($parentType);
return $pseudoType;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment