ExportPropertiesTask.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /*
  3. * $Id: ExportPropertiesTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once "phing/Task.php";
  22. /**
  23. * Saves currently defined properties into a specified file
  24. *
  25. * @author Andrei Serdeliuc
  26. * @extends Task
  27. * @version $Id: ExportPropertiesTask.php 905 2010-10-05 16:28:03Z mrook $
  28. * @package phing.tasks.ext
  29. */
  30. class ExportPropertiesTask extends Task
  31. {
  32. /**
  33. * Array of project properties
  34. *
  35. * (default value: null)
  36. *
  37. * @var array
  38. * @access private
  39. */
  40. private $_properties = null;
  41. /**
  42. * Target file for saved properties
  43. *
  44. * (default value: null)
  45. *
  46. * @var string
  47. * @access private
  48. */
  49. private $_targetFile = null;
  50. /**
  51. * Exclude properties starting with these prefixes
  52. *
  53. * @var array
  54. * @access private
  55. */
  56. private $_disallowedPropertyPrefixes = array(
  57. 'host.',
  58. 'phing.',
  59. 'os.',
  60. 'php.',
  61. 'line.',
  62. 'env.',
  63. 'user.'
  64. );
  65. /**
  66. * setter for _targetFile
  67. *
  68. * @access public
  69. * @param string $file
  70. * @return bool
  71. */
  72. public function setTargetFile($file)
  73. {
  74. if(!is_dir(dirname($file))) {
  75. throw new BuildException("Parent directory of target file doesn't exist");
  76. }
  77. if(!is_writable(dirname($file)) && (file_exists($file) && !is_writable($file))) {
  78. throw new BuildException("Target file isn't writable");
  79. }
  80. $this->_targetFile = $file;
  81. return true;
  82. }
  83. /**
  84. * setter for _disallowedPropertyPrefixes
  85. *
  86. * @access public
  87. * @param string $file
  88. * @return bool
  89. */
  90. public function setDisallowedPropertyPrefixes($prefixes)
  91. {
  92. $this->_disallowedPropertyPrefixes = explode(",", $prefixes);
  93. return true;
  94. }
  95. public function main()
  96. {
  97. // Sets the currently declared properties
  98. $this->_properties = $this->getProject()->getProperties();
  99. if(is_array($this->_properties) && !empty($this->_properties) && null !== $this->_targetFile) {
  100. $propertiesString = '';
  101. foreach($this->_properties as $propertyName => $propertyValue) {
  102. if(!$this->isDisallowedPropery($propertyName)) {
  103. $propertiesString .= $propertyName . "=" . $propertyValue . PHP_EOL;
  104. }
  105. }
  106. if(!file_put_contents($this->_targetFile, $propertiesString)) {
  107. throw new BuildException('Failed writing to ' . $this->_targetFile);
  108. }
  109. }
  110. }
  111. /**
  112. * Checks if a property name is disallowed
  113. *
  114. * @access protected
  115. * @param string $propertyName
  116. * @return bool
  117. */
  118. protected function isDisallowedPropery($propertyName)
  119. {
  120. foreach($this->_disallowedPropertyPrefixes as $property) {
  121. if(substr($propertyName, 0, strlen($property)) == $property) {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. }
  128. ?>