PageRenderTime 50ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/TYPO3/FLOW3/Package/MetaData/XmlWriter.php

https://github.com/christianjul/FLOW3-Composer
PHP | 139 lines | 81 code | 22 blank | 36 comment | 17 complexity | 47af6cc7ce36c698488a7443476449dd MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Package\MetaData;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Annotations as FLOW3;
  13. /**
  14. * A package meta XML writer implementation based on the Package.xml format
  15. *
  16. * @FLOW3\Scope("singleton")
  17. */
  18. class XmlWriter {
  19. /**
  20. * Write metadata for the given package into a Package.xml file.
  21. *
  22. * @param \TYPO3\FLOW3\Package\PackageInterface $package The package - also contains information about where to write the Package meta file
  23. * @param \TYPO3\FLOW3\Package\MetaDataInterface $meta The MetaData object containing the information to write
  24. * @return boolean If writing the XML file was successful returns TRUE, otherwise FALSE
  25. */
  26. static public function writePackageMetaData(\TYPO3\FLOW3\Package\PackageInterface $package, \TYPO3\FLOW3\Package\MetaDataInterface $meta) {
  27. $xml = new \XMLWriter();
  28. if ($xml->openURI($package->getMetaPath() . 'Package.xml') === FALSE) return FALSE;
  29. $xml->setIndent(true);
  30. $xml->setIndentString(chr(9));
  31. $xml->startDocument('1.0', 'utf-8', 'yes');
  32. $xml->startElement('package');
  33. $xml->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
  34. $xml->writeAttribute('xmlns', 'http://typo3.org/ns/2008/flow3/package');
  35. $xml->writeAttribute('version', '1.0');
  36. $xml->writeElement('key', $meta->getPackageKey());
  37. $xml->writeElement('title', $meta->getTitle());
  38. $xml->writeElement('description', $meta->getDescription());
  39. $xml->writeElement('version', $meta->getVersion());
  40. if (count($meta->getCategories())) {
  41. $xml->startElement('categories');
  42. foreach ($meta->getCategories() as $category) {
  43. $xml->writeElement('category', $category);
  44. }
  45. $xml->endElement();
  46. }
  47. if (count($meta->getParties())) {
  48. $xml->startElement('parties');
  49. foreach ($meta->getParties() as $party) {
  50. self::writeParty($xml, $party);
  51. }
  52. $xml->endElement();
  53. }
  54. if (count($meta->getConstraints())) {
  55. $xml->startElement('constraints');
  56. foreach ($meta->getConstraintTypes() as $constraintType) {
  57. $constraints = $meta->getConstraintsByType($constraintType);
  58. if (count($constraints)) {
  59. $xml->startElement($constraintType);
  60. foreach ($constraints as $constraint) {
  61. self::writeConstraint($xml, $constraint);
  62. }
  63. $xml->endElement();
  64. }
  65. }
  66. $xml->endElement();
  67. }
  68. $xml->endElement();
  69. return TRUE;
  70. }
  71. /**
  72. * Write party metadata to the XMLWriter.
  73. *
  74. *
  75. * @param \XMLWriter $xml The XMLWriter to write to
  76. * @param \TYPO3\FLOW3\Package\MetaData\AbstractParty $party The party to write
  77. * @return void
  78. */
  79. static protected function writeParty(\XMLWriter $xml, \TYPO3\FLOW3\Package\MetaData\AbstractParty $party) {
  80. $xml->startElement($party->getPartyType());
  81. if (strlen($party->getRole())) $xml->writeAttribute('role', $party->getRole());
  82. if (strlen($party->getName())) $xml->writeElement('name', $party->getName());
  83. if (strlen($party->getEmail())) $xml->writeElement('email', $party->getEmail());
  84. if (strlen($party->getWebsite())) $xml->writeElement('website', $party->getWebsite());
  85. switch ($party->getPartyType()) {
  86. case 'person':
  87. if (strlen($party->getCompany())) $xml->writeElement('company', $party->getCompany());
  88. if (strlen($party->getRepositoryUserName())) $xml->writeElement('repositoryUserName', $party->getRepositoryUserName());
  89. break;
  90. case 'company':
  91. break;
  92. }
  93. $xml->endElement();
  94. }
  95. /**
  96. * Write the constraint to a XMLWriter instance.
  97. *
  98. * @param \XMLWriter $xml The XMLWriter to write to
  99. * @param \TYPO3\FLOW3\Package\MetaData\AbstractConstraint $constraint The constraint to write
  100. * @return void
  101. */
  102. static protected function writeConstraint(\XMLWriter $xml, \TYPO3\FLOW3\Package\MetaData\AbstractConstraint $constraint) {
  103. $xml->startElement($constraint->getConstraintScope());
  104. if (strlen($constraint->getMinVersion())) $xml->writeAttribute('minVersion', $constraint->getMinVersion());
  105. if (strlen($constraint->getMaxVersion())) $xml->writeAttribute('maxVersion', $constraint->getMaxVersion());
  106. switch ($constraint->getConstraintScope()) {
  107. case \TYPO3\FLOW3\Package\MetaData::CONSTRAINT_SCOPE_SYSTEM :
  108. if (strlen($constraint->getType())) $xml->writeAttribute('type', $constraint->getType());
  109. break;
  110. case \TYPO3\FLOW3\Package\MetaData::CONSTRAINT_SCOPE_PACKAGE :
  111. break;
  112. }
  113. if (strlen($constraint->getValue())) $xml->text($constraint->getValue());
  114. $xml->endElement();
  115. }
  116. }
  117. ?>