Created
January 24, 2012 16:32
-
-
Save BjornW/1670987 to your computer and use it in GitHub Desktop.
A first attempt at writing a Phing Task
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"; | |
class RandomStringTask extends Task { | |
/** | |
* The length of the random string | |
*/ | |
private $length = 32; | |
/** | |
* The ASCII range start character number for the characters | |
* used in the string | |
*/ | |
private $ascii_start = 33; | |
/** | |
* The ASCII range end character number for the characters | |
* used in the string | |
*/ | |
private $ascii_stop = 126; | |
/** | |
* Set the return value to this property | |
*/ | |
private $propertyName = null; | |
/** | |
* Exclude these comma seperated ascii numbers by default | |
* by default exclude: double quote, single quote and apastroph | |
*/ | |
private $exclude = '34, 39, 96'; | |
/** | |
* The setter for the attribute "length" | |
*/ | |
public function setLength($length) { | |
$this->length = (int) $length; | |
} | |
/** | |
* The setter for the attribute "ascii_start" | |
*/ | |
public function setAscii_Start($ascii_start) { | |
$this->ascii_start = (int) $ascii_start; | |
} | |
/** | |
* The setter for the attribute "ascii_stop" | |
*/ | |
public function setAscii_Stop($ascii_stop) { | |
$this->ascii_stop = (int) $ascii_stop; | |
} | |
/** | |
* The getter for the attribute "ascii_start" | |
*/ | |
public function getAscii_Start($ascii_start) { | |
return $this->ascii_start; | |
} | |
/** | |
* The getter for the attribute "ascii_stop" | |
*/ | |
public function getAscii_Stop($ascii_stop) { | |
return $this->ascii_stop; | |
} | |
/** | |
* The setter for the attribute "value" | |
*/ | |
public function setPropertyName($propertyName) { | |
$this->propertyName = $propertyName; | |
} | |
/** | |
* The getter for the attribute "value" | |
*/ | |
public function getPropertyName() { | |
return $this->propertyName; | |
} | |
/** | |
* The init method: Do init steps. | |
*/ | |
public function init() { | |
// nothing to do here | |
} | |
/** | |
* The main entry point method. | |
*/ | |
public function main() { | |
if ( $this->propertyName === null) { | |
throw new BuildException("You must specify a value for propertyName attribute."); | |
} | |
// prevent non printable characters from selecting | |
if( $this->ascii_start < (int) 33) { | |
throw new BuildException("The ascii_start property value needs a value bigger than 33"); | |
} | |
if( $this->ascii_stop > (int) 126) { | |
throw new BuildException("The ascii_stop property value needs a value smaller than 126"); | |
} | |
$this->project->setUserProperty( $this->getPropertyName(), $this->randomString()); | |
} | |
/** | |
* Retrieves and processes the ascii numbers which should not be used | |
*/ | |
private function getExcludedAsciiNumbers() { | |
$excluded = explode(',', $this->exclude); | |
if( is_array($excluded) ) { | |
array_walk( $excluded, function(&$value, $key) { $value = trim($value); $value = (int)$value; } ); | |
} | |
return $excluded; | |
} | |
/** | |
* Generate a string of a given length with random characters in the ASCII | |
* range given See for instance http://www.asciitable.com/ | |
*/ | |
private function randomString() { | |
$str = ''; | |
$excluded = $this->getExcludedAsciiNumbers(); | |
for( $i = 0; $i <= $this->length; $i++) { | |
$ascii_nr = $this->randomNumber(); | |
$str .= chr( $ascii_nr ); | |
} | |
return $str; | |
} | |
/** | |
* Generates a random number between the | |
* $ascii_start and $ascii_top values and prevents using | |
* an excluded number | |
*/ | |
function randomNumber() { | |
$excluded = $this->getExcludedAsciiNumbers(); | |
$ascii_nr = mt_rand( $this->ascii_start, $this->ascii_stop ); | |
if( in_array($ascii_nr, $excluded) ) { | |
$this->randomNumber(); | |
} else { | |
return $ascii_nr; | |
} | |
} | |
} | |
?> |
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
<?xml version="1.0" ?> | |
<project name="test-random-string-task" basedir="." default="test.randomstring"> | |
<taskdef name="randomstring" classname="RandomStringTask" /> | |
<target name="test.randomstring"> | |
<randomstring length="32" propertyName="test.rndstr" ascii_start="33" ascii_stop="100" /> | |
<echo message="The Random String: ${test.rndstr}" /> | |
</target> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment