All pastes #557017 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-06-10 20:59 UTC
rendered paste body
<?php

/**
 * Reads specs folder and prints infos about packages
 *
 * @author	   Nicolas Christener <christener@gmail.com>
 * @copyright  Nicolas Christener, 2007
 * @license    GPL
 * @version    0.1
 */

class specFileParser{
	private $specFileFolder;
	private $specFilesInfolder;
	private $specs;

	/**
	 * Class constructor
	 *
	 * @param string $specFileFolder
	 */
	public function __construct($folder){
		$this->specFileFolder = $folder;
		$this->specs = array();

		try {
			$this->getSpecFilesInFolder();
			$this->parseFiles();
			//$this->printSpecFileInfos();
		}catch (Exception $e){
			echo "Error: ".$e->getMessage();
		}

	}

	/**
	 * Reads a folder and save the xml files into a array
	 */
	private function getSpecFilesInFolder(){
		$specFilesInfolder = array();

		if ($handle = opendir($this->specFileFolder)) {
		    while (false !== ($file = readdir($handle))) {
		        $fileName = basename($file);
		        $stringParts = explode('.', $fileName);
				if(array_pop($stringParts) == 'xml'){
		        	//echo "$fileName\n";
		        	array_push($specFilesInfolder, $fileName);
				}
		    }
		    closedir($handle);
		    $this->specFilesInfolder = $specFilesInfolder;
		}else{
			throw new Exception(sprintf('Could not read folder', $this->specFileFolder));
		}
	}

	private function parseFiles(){
		foreach($this->specFilesInfolder as $file){
			$this->parseFile($file);
		}
	}

	private function parseFile($file){
		$xmlData = file_get_contents($this->specFileFolder.'/'.$file);
		try{
			$xmlReader = @new SimpleXMLElement($xmlData);
		}catch (Exception $e){
			echo "Could not parse: ".$file."\n";
			return;
		}

    	foreach($xmlReader->releases as $release){
			//var_dump($release);
			foreach ($release->release as $releaseData){
				$this->specs[$file] = array();
				$this->specs[$file]['packageName'] = $xmlReader['name'];
				$this->specs[$file]['fileName'] = $file;
				$this->specs[$file]['version'] = $releaseData['version'];
				$this->specs[$file]['revision'] = $releaseData['revision'];
				$this->specs[$file]['branch'] = $releaseData['branch'];
			}
    	}
	}

	/**
	 * Returns array with all infos
	 *
	 * @return array
	 */
	public function getSpecInfos(){
		return $this->specs;
	}

	/**
	 * Call this if you don't want to use smarty
	 */
	private function printSpecFileInfos(){
		// *damn* nicu.. what a crap... use smarty!
		echo '<table>';
		echo '<th>package</th>';
		echo '<th>version</th>';
		echo '<th>revision</th>';
		echo '<th>branch</th>';

		foreach($this->specs as $specInfos){
			echo '<tr>';
			echo sprintf('<td><a href="specs/%s">%s</a></td>', $specInfos['fileName'], $specInfos['packageName']);
			echo sprintf('<td>%s</td>', $specInfos['version']);
			echo sprintf('<td>%s</td>', $specInfos['revision']);
			echo sprintf('<td>%s</td>', $specInfos['branch']);
			echo '</tr>';
		}

		echo "</table>";
	}
}