Location: PHPKode > scripts > BoUML File Parser > UML_Package.php
<?php
/**
 * The package by definition contains UML artifacts or other packages
 * @author jgonzalez
 *
 */

interface UML_Package_Interface {
	public function setSource(UML_Data_Source &$source);
	public function getSource();
	public function addPackage(UML_Package_Interface $package);
	public function getPackages();
}

class UML_Package implements UML_Package_Interface {
	private $config = NULL;
	private $source 	= NULL;
	private $packages 	= array();
	
	protected function __construct(UML_Parser_Configuration $configuration) {
		$this->config = $configuration;
	}
	
	public static function createFromSource(UML_Data_Source &$source, UML_Parser_Configuration &$configuration) {
		
		$package = new self($configuration);
		$package->setSource($source);
		$package->addPackage($package);
		
		return $package;
	}
	
	public function setSource(UML_Data_Source &$source) {
		return $this->source = $source;
	}
	
	public function getSource() {
		return $this->source;
	}
	
	public function addPackage(UML_Package_Interface $package) {
		$this->packages[] = $package;
		foreach ($package->getArtifactsByType($this->config->getArtifactNameForPackage()) as $sub_package) {
			
			$data_source_class = get_class($this->getSource());
			$source = new $data_source_class($sub_package->getId());
			$source->setConfiguration($this->config);
			
			$this->addPackage(self::createFromSource($source, $this->config));
		}
	}
	
	public function getPackages() {
		return $this->packages;
	}
	
	public function getArtifactsByType($artifact_type) {
		$artifacts = array();
		
		$artifact = $this->config->getArtifactObjectByType($artifact_type);
		
		if (is_null($artifact)) {
			throw new Exception("UML_Package->getArtifactsByType -> Tried to get artifact object for artifact type [$artifact_type] without success :(");
		} else {
			// go on!
		}
		$regexp   = $artifact->getRegExp();
		
		if (!is_null($regexp)) {
			$matches = array();
			
			$raw_source = $this->getSource()->getSource();
			preg_match_all($regexp, $raw_source, $matches);
			if(count($matches) > 0) {
				$matches = current($matches);
				foreach($matches as $artifact_source_code) {
					$artifacts[] = $this->config->getArtifactObjectByType($artifact_type, $artifact_source_code);
				}
			} else {
				// No packages found within file
			}
			
			
		} else {
			throw new Exception("getArtifactsByType:: Tried to get regexp for artifact [$artifact_type], but is not configured");
		}
		
		return $artifacts;
	}
}
Return current item: BoUML File Parser