Posted By

extraordinaire on 11/19/09


Tagged

phing


Versions (?)


Advertising

Submit Site


Phing task: export properties to file


Published in: PHP 



Website Promotion
DIRECTORY
is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


URL: http://extraordinaire.me/post/249721396/phing-how-to-export-defined-properties-to-file

Expand | Embed | Plain Text
  1. <?php
  2.  
  3. require_once "phing/Task.php";
  4.  
  5. /**
  6.  * Saves currently defined properties into a specified file
  7.  *
  8.  * @author Andrei Serdeliuc
  9.  * @extends Task
  10.  */
  11. class ExportProperties extends Task
  12. {
  13. /**
  14.   * Array of project properties
  15.   *
  16.   * (default value: null)
  17.   *
  18.   * @var array
  19.   * @access private
  20.   */
  21. private $_properties = null;
  22.  
  23. /**
  24.   * Target file for saved properties
  25.   *
  26.   * (default value: null)
  27.   *
  28.   * @var string
  29.   * @access private
  30.   */
  31. private $_targetFile = null;
  32.  
  33. /**
  34.   * Exclude properties starting with these prefixes
  35.   *
  36.   * @var array
  37.   * @access private
  38.   */
  39. private $_disallowedPropertyPrefixes = array(
  40. 'host.',
  41. 'phing.',
  42. 'os.',
  43. 'php.',
  44. 'line.',
  45. 'env.',
  46. 'user.'
  47. );
  48.  
  49. /**
  50.   * setter for _targetFile
  51.   *
  52.   * @access public
  53.   * @param string $file
  54.   * @return bool
  55.   */
  56. public function setTargetFile($file)
  57. {
  58. if(!is_dir(dirname($file))) {
  59. throw new BuildException("Parent directory of target file doesn't exist");
  60. }
  61.  
  62. if(!is_writable(dirname($file)) && (file_exists($file) && !is_writable($file))) {
  63. throw new BuildException("Target file isn't writable");
  64. }
  65.  
  66. $this->_targetFile = $file;
  67. return true;
  68. }
  69.  
  70. /**
  71.   * Sets the currently declared properties
  72.   *
  73.   * @access public
  74.   * @return void
  75.   */
  76. public function init()
  77. {
  78. $this->_properties = $this->getProject()->getProperties();
  79. }
  80.  
  81. public function main()
  82. {
  83. if(is_array($this->_properties) && !empty($this->_properties) && null !== $this->_targetFile) {
  84. $propertiesString = '';
  85. foreach($this->_properties as $propertyName => $propertyValue) {
  86. if(!$this->isDisallowedPropery($propertyName)) {
  87. $propertiesString .= $propertyName . "=" . $propertyValue . PHP_EOL;
  88. }
  89. }
  90.  
  91. if(!file_put_contents($this->_targetFile, $propertiesString)) {
  92. throw new BuildException('Failed writing to ' . $this->_targetFile);
  93. }
  94. }
  95. }
  96.  
  97. /**
  98.   * Checks if a property name is disallowed
  99.   *
  100.   * @access protected
  101.   * @param string $propertyName
  102.   * @return bool
  103.   */
  104. protected function isDisallowedPropery($propertyName)
  105. {
  106. foreach($this->_disallowedPropertyPrefixes as $property) {
  107. if(substr($propertyName, 0, strlen($property)) == $property) {
  108. return true;
  109. }
  110. }
  111.  
  112. return false;
  113. }
  114. }
  115.  
  116. ?>

Report this snippet 

You need to login to post a comment.

Download royalty free graphics