From 14e0f287a3840e2a0dae7e0d98e86af1ac3e9e59 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 3 Apr 2011 22:27:57 +0100 Subject: [PATCH] First go at the XML config system --- sampleConfig.xml | 11 +++++++ src/CreepyCoder.php | 67 +++++++++++++++++++++++++++++++------- src/ReadGitHub.php | 28 +++++++++++++--- src/ReadSVN.php | 23 ++++++++++--- src/WriteDayOfWeekData.php | 20 +++++++++--- src/WriteHourOfDayData.php | 24 ++++++++++---- src/WriteICalData.php | 11 ++++++- 7 files changed, 154 insertions(+), 30 deletions(-) create mode 100644 sampleConfig.xml diff --git a/sampleConfig.xml b/sampleConfig.xml new file mode 100644 index 0000000..4b7902f --- /dev/null +++ b/sampleConfig.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/src/CreepyCoder.php b/src/CreepyCoder.php index 7a15872..a65790b 100644 --- a/src/CreepyCoder.php +++ b/src/CreepyCoder.php @@ -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))); + } +} diff --git a/src/ReadGitHub.php b/src/ReadGitHub.php index a115100..7e7ac9a 100644 --- a/src/ReadGitHub.php +++ b/src/ReadGitHub.php @@ -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; @@ -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); diff --git a/src/ReadSVN.php b/src/ReadSVN.php index 4a678b5..ef6c69f 100644 --- a/src/ReadSVN.php +++ b/src/ReadSVN.php @@ -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)); @@ -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); diff --git a/src/WriteDayOfWeekData.php b/src/WriteDayOfWeekData.php index a1a7e66..cfccd6f 100644 --- a/src/WriteDayOfWeekData.php +++ b/src/WriteDayOfWeekData.php @@ -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(); @@ -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) { @@ -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); @@ -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"); } diff --git a/src/WriteHourOfDayData.php b/src/WriteHourOfDayData.php index 32072cf..05fc632 100644 --- a/src/WriteHourOfDayData.php +++ b/src/WriteHourOfDayData.php @@ -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(); @@ -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) { @@ -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"); } diff --git a/src/WriteICalData.php b/src/WriteICalData.php index bbc4985..e58e08a 100644 --- a/src/WriteICalData.php +++ b/src/WriteICalData.php @@ -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"); }