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