Skip to content

Commit

Permalink
First go at the XML config system
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofgreen committed Apr 3, 2011
1 parent f47995c commit 14e0f28
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 30 deletions.
11 changes: 11 additions & 0 deletions sampleConfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<config>
<ReadSVN URL="repositoryAddress">
<Author UserName="un"/>
</ReadSVN>
<ReadGitHub UserName="un" Repository="r" Branch="master">
<Author Email="e"/>
</ReadGitHub>
<WriteHourOfDayData GraphFile="HourOfDay.png" CSVFile="HourOfDay.csv"/>
<WriteDayOfWeekData GraphFile="DayOfWeek.png" CSVFile="DayOfWeek.csv"/>
<WriteICalData File="DayOfWeek.ical" />
</config>
67 changes: 56 additions & 11 deletions src/CreepyCoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,71 @@
require dirname(__FILE__).DIRECTORY_SEPARATOR.'BaseWriteClass.php';
require dirname(__FILE__).DIRECTORY_SEPARATOR.'CoderAction.php';

require dirname(__FILE__).DIRECTORY_SEPARATOR.'ReadSVN.php';


$opts = getopt('c:');

$configXML = file_get_contents($opts['c']);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($configXML);

$dataManager = new DataManager();

$dataManager->addReader(new ReadSVN(array('repo'=>'https://elastik.svn.sourceforge.net/svnroot/elastik/trunk','authors'=>array('jarofgreen'))));
#################################### set input

$configList = $xmlDoc->getElementsByTagName('ReadSVN');
$configListLength = $configList->length;
if ($configListLength > 0) {
require dirname(__FILE__).DIRECTORY_SEPARATOR.'ReadSVN.php';
for($pos=0; $pos<$configListLength; $pos++) {
$dataManager->addReader(new ReadSVN($configList->item($pos)));
}
}

$dataManager->process();

print_r($dataManager->getData());
$configList = $xmlDoc->getElementsByTagName('ReadGitHub');
$configListLength = $configList->length;
if ($configListLength > 0) {
require dirname(__FILE__).DIRECTORY_SEPARATOR.'ReadGitHub.php';
for($pos=0; $pos<$configListLength; $pos++) {
$dataManager->addReader(new ReadGitHub($configList->item($pos)));
}
}


#################################### process input

$dataManager->process();
#print_r($dataManager->getData());
#print(count($dataManager->getData()));

print(count($dataManager->getData()));

require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteHourOfDayData.php';
$dataManager->writeData(new WriteHourOfDayData(array('file'=>'hourOfDay.csv')));
#################################### output

require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteDayOfWeekData.php';
$dataManager->writeData(new WriteDayOfWeekData(array('file'=>'dayOfWeek.csv')));
$configList = $xmlDoc->getElementsByTagName('WriteHourOfDayData');
$configListLength = $configList->length;
if ($configListLength > 0) {
require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteHourOfDayData.php';
for($pos=0; $pos<$configListLength; $pos++) {
$dataManager->writeData(new WriteHourOfDayData($configList->item($pos)));
}
}

require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteICalData.php';
$dataManager->writeData(new WriteICalData(array('file'=>'out.ical')));
$configList = $xmlDoc->getElementsByTagName('WriteDayOfWeekData');
$configListLength = $configList->length;
if ($configListLength > 0) {
require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteDayOfWeekData.php';
for($pos=0; $pos<$configListLength; $pos++) {
$dataManager->writeData(new WriteDayOfWeekData($configList->item($pos)));
}
}

$configList = $xmlDoc->getElementsByTagName('WriteICalData');
$configListLength = $configList->length;
if ($configListLength > 0) {
require dirname(__FILE__).DIRECTORY_SEPARATOR.'WriteICalData.php';
for($pos=0; $pos<$configListLength; $pos++) {
$dataManager->writeData(new WriteICalData($configList->item($pos)));
}
}

28 changes: 24 additions & 4 deletions src/ReadGitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,37 @@

class ReadGitHub extends BaseReadClass {

protected $userName;
protected $repo;
protected $branch;

protected $authors;

public function __construct($configData = array()) {
if (get_class($configData) == 'DOMElement') {
$this->userName = $configData->getAttribute('UserName');
$this->repo = $configData->getAttribute('Repository');
$this->branch = $configData->getAttribute('Branch');
$list = $configData->getElementsByTagName('Author');
if ($list->length > 0) {
$this->authors = array();
for($pos=0; $pos<$list->length ; $pos++) {
$this->authors[] = $list->item($pos)->getAttribute('Email');
}
}
}
parent::__construct($configData);
}

public function process() {


$this->readBranchFromRepository($this->configData['userName'],$this->configData['repository'],$this->configData['branch']);
$this->readBranchFromRepository($this->userName,$this->repo,$this->branch);

}

private function readBranchFromRepository($userName, $repository, $branch) {

$authors = isset($this->configData['authors']) && is_array($this->configData['authors']) ? $this->configData['authors'] : null;

$page = 1;
$gotResultsLastTime = false;

Expand All @@ -50,7 +70,7 @@ private function readBranchFromRepository($userName, $repository, $branch) {
if ($data->commits && count($data->commits) > 0) {
$gotResultsLastTime = true;
foreach($data->commits as $idx=>$commit) {
if (is_null($authors) || in_array($commit->author->email,$authors)) {
if (is_null($this->authors) || in_array($commit->author->email,$this->authors)) {
$data = new GitCommitAction();
$data->setDateTime(new DateTime($commit->authored_date));
$this->dataManager->addData($data);
Expand Down
23 changes: 19 additions & 4 deletions src/ReadSVN.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,27 @@ class SVNCommitAction extends CoderAction {

class ReadSVN extends BaseReadClass {

public function process() {
protected $repo;
protected $authors;

$authors = isset($this->configData['authors']) && is_array($this->configData['authors']) ? $this->configData['authors'] : null;
public function __construct($configData = array()) {
if (get_class($configData) == 'DOMElement') {
$this->repo = $configData->getAttribute('URL');
$list = $configData->getElementsByTagName('Author');
if ($list->length > 0) {
$this->authors = array();
for($pos=0; $pos<$list->length ; $pos++) {
$this->authors[] = $list->item($pos)->getAttribute('UserName');
}
}
}
parent::__construct($configData);
}

public function process() {

$out = array();
exec("svn log --xml ".$this->configData['repo'], $out);
exec("svn log --xml ".$this->repo, $out);

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML(implode('',$out));
Expand All @@ -37,7 +52,7 @@ public function process() {
$logEntryListLength = $logEntryList->length;
for($pos=0; $pos<$logEntryListLength; $pos++) {
$x = $logEntryList->item($pos);
if (is_null($authors) || in_array($x->getElementsByTagName('author')->item(0)->textContent,$authors)) {
if (is_null($this->authors) || in_array($x->getElementsByTagName('author')->item(0)->textContent,$this->authors)) {
$data = new SVNCommitAction();
$data->setDateTime(new DateTime($x->getElementsByTagName('date')->item(0)->textContent));
$this->dataManager->addData($data);
Expand Down
20 changes: 16 additions & 4 deletions src/WriteDayOfWeekData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@

class WriteDayOfWeekData extends BaseWriteClass {


protected $graph;
protected $csv;

public function __construct($configData = array()) {
if (get_class($configData) == 'DOMElement') {
$this->graph = $configData->getAttribute('GraphFile');
$this->csv = $configData->getAttribute('CSVFile');
}
parent::__construct($configData);
}

public function write() {

$data = array();
Expand All @@ -35,7 +47,7 @@ public function write() {
$data[$item->getDateTimeAs('D')]++;
}

if (isset($this->configData['graphfile']) && $this->configData['graphfile']) {
if ($this->graph) {
$maxCommits = max($data);
$dataArray = $labelArray = array();
foreach($data as $day=>$commits) {
Expand All @@ -47,7 +59,7 @@ public function write() {
print "URL: $url \n\n";

$ch = curl_init($url);
$fp = fopen($this->configData['graphfile'], "w");
$fp = fopen($this->graph, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
Expand All @@ -56,9 +68,9 @@ public function write() {

}

if (isset($this->configData['file']) && $this->configData['file']) {
if ($this->csv) {

if (!$handle = fopen($this->configData['file'], 'w')) {
if (!$handle = fopen($this->csv, 'w')) {
throw new Exception("Cannot open file");
}

Expand Down
24 changes: 18 additions & 6 deletions src/WriteHourOfDayData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@

class WriteHourOfDayData extends BaseWriteClass {



protected $graph;
protected $csv;

public function __construct($configData = array()) {
if (get_class($configData) == 'DOMElement') {
$this->graph = $configData->getAttribute('GraphFile');
$this->csv = $configData->getAttribute('CSVFile');
}
parent::__construct($configData);
}


public function write() {

$data = array();
Expand All @@ -28,7 +42,7 @@ public function write() {
$data[intval($item->getDateTimeAs('G'))]++;
}

if (isset($this->configData['graphfile']) && $this->configData['graphfile']) {
if ($this->graph) {
$maxCommits = max($data);
$dataArray = $labelArray = array();
foreach($data as $day=>$commits) {
Expand All @@ -40,19 +54,17 @@ public function write() {
print "URL: $url \n\n";

$ch = curl_init($url);
$fp = fopen($this->configData['graphfile'], "w");
$fp = fopen($this->graph, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);


}

if (isset($this->configData['file']) && $this->configData['file']) {
if ($this->csv) {

if (!$handle = fopen($this->configData['file'], 'w')) {
if (!$handle = fopen($this->csv, 'w')) {
throw new Exception("Cannot open file");
}

Expand Down
11 changes: 10 additions & 1 deletion src/WriteICalData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@

class WriteICalData extends BaseWriteClass {

protected $file;

public function __construct($configData = array()) {
if (get_class($configData) == 'DOMElement') {
$this->file = $configData->getAttribute('File');
}
parent::__construct($configData);
}

public function write() {

if (!$handle = fopen($this->configData['file'], 'w')) {
if (!$handle = fopen($this->file, 'w')) {
throw new Exception("Cannot open file");
}

Expand Down

0 comments on commit 14e0f28

Please sign in to comment.