Created
December 19, 2013 07:34
-
-
Save extraordinaire/8035652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "phing/Task.php"; | |
/** | |
* Saves currently defined properties into a specified file | |
* | |
* @author Andrei Serdeliuc | |
* @extends Task | |
*/ | |
class ExportProperties extends Task | |
{ | |
/** | |
* Array of project properties | |
* | |
* (default value: null) | |
* | |
* @var array | |
* @access private | |
*/ | |
private $_properties = null; | |
/** | |
* Target file for saved properties | |
* | |
* (default value: null) | |
* | |
* @var string | |
* @access private | |
*/ | |
private $_targetFile = null; | |
/** | |
* Exclude properties starting with these prefixes | |
* | |
* @var array | |
* @access private | |
*/ | |
private $_disallowedPropertyPrefixes = array( | |
'host.', | |
'phing.', | |
'os.', | |
'php.', | |
'line.', | |
'env.', | |
'user.' | |
); | |
/** | |
* setter for _targetFile | |
* | |
* @access public | |
* @param string $file | |
* @return bool | |
*/ | |
public function setTargetFile($file) | |
{ | |
if(!is_dir(dirname($file))) { | |
throw new BuildException("Parent directory of target file doesn't exist"); | |
} | |
if(!is_writable(dirname($file)) && (file_exists($file) && !is_writable($file))) { | |
throw new BuildException("Target file isn't writable"); | |
} | |
$this->_targetFile = $file; | |
return true; | |
} | |
/** | |
* Sets the currently declared properties | |
* | |
* @access public | |
* @return void | |
*/ | |
public function init() | |
{ | |
$this->_properties = $this->getProject()->getProperties(); | |
} | |
public function main() | |
{ | |
if(is_array($this->_properties) && !empty($this->_properties) && null !== $this->_targetFile) { | |
$propertiesString = ''; | |
foreach($this->_properties as $propertyName => $propertyValue) { | |
if(!$this->isDisallowedPropery($propertyName)) { | |
$propertiesString .= $propertyName . "=" . $propertyValue . PHP_EOL; | |
} | |
} | |
if(!file_put_contents($this->_targetFile, $propertiesString)) { | |
throw new BuildException('Failed writing to ' . $this->_targetFile); | |
} | |
} | |
} | |
/** | |
* Checks if a property name is disallowed | |
* | |
* @access protected | |
* @param string $propertyName | |
* @return bool | |
*/ | |
protected function isDisallowedPropery($propertyName) | |
{ | |
foreach($this->_disallowedPropertyPrefixes as $property) { | |
if(substr($propertyName, 0, strlen($property)) == $property) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment