PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/ZoopModule.php

http://zoop.googlecode.com/
PHP | 120 lines | 77 code | 18 blank | 25 comment | 6 complexity | 8d669ec6695dffd4611d744ae75a2544 MD5 | raw file
  1<?php
  2abstract class ZoopModule
  3{
  4	private $name, $path, $lib;
  5	private $depends = array(), $includes = array(), $classes = array();
  6	protected $hasConfig = false;
  7	
  8	final function __construct($path, $lib)
  9	{
 10		//	assign in the paramamters
 11		$this->path = $path;
 12		$this->lib = $lib;
 13		$this->name = strtolower(str_replace('Module', '', get_class($this)));
 14		
 15		//	second stage (module specific) construction
 16		$this->init();
 17		
 18		$classname = get_class($this);
 19		//	load any dependant modules
 20		if($this->getDepends())
 21			foreach($this->getDepends() as $thisDepends)
 22				$this->lib->loadMod($thisDepends);
 23		
 24		//	include any normal files that need to be included
 25		if($this->getIncludes())
 26		{
 27			foreach($this->getIncludes() as $thisInclude)
 28			{
 29				if($thisInclude[0] == '/')
 30					require($thisInclude);
 31				else
 32					require($this->path . '/' . $thisInclude);
 33			}
 34		}
 35		
 36		//	register any class files
 37		if($classes = $this->getClasses())
 38			foreach($classes as $thisClass)
 39				ZoopLoader::addClass($thisClass, $this->path . '/' . $thisClass . '.php');
 40		
 41		if($this->hasConfig)
 42			$this->loadConfig();
 43		
 44		//	handle configuration
 45		$this->configure();		
 46	}
 47	
 48	protected function init() {}
 49	protected function configure() {}
 50	
 51	/**
 52	 * Figures out the name of the module by removing the word "Module" from
 53	 * the class name and returning the result
 54	 *
 55	 * @return string
 56	 */
 57	function createName()
 58	{
 59		return strtolower(str_replace('Module', '', get_class($this)));
 60	}
 61	
 62	function getConfigPath()
 63	{
 64		return $this->name;
 65	}
 66	
 67	private function loadConfig()
 68	{
 69		Config::suggest($this->path . '/' . 'config.yaml', 'zoop.' . $this->getConfigPath());
 70	}
 71	
 72	/**
 73	 * Returns the configuration options using the Config class.
 74	 * Returns config options from "zoop.<modulename>.<path>"
 75	 * Path is optional and may be omitted.
 76	 *
 77	 * @param string $path
 78	 * @return array of configuration options
 79	 */
 80	function getConfig($path = '')
 81	{
 82		$config = Config::get('zoop.' . $this->getConfigPath() . $path);
 83		return $config;
 84	}
 85	
 86	/**
 87	 * stuff about this function
 88	 *
 89	 * @return array(list of files to include) or false;
 90	 */
 91	protected function addClass($className)
 92	{
 93		$this->classes[] = $className;
 94	}
 95	
 96	protected function getClasses()
 97	{
 98		return $this->classes;
 99	}
100	
101	protected function addInclude($include)
102	{
103		$this->includes[] = $include;
104	}
105	
106	protected function getIncludes()
107	{
108		return $this->includes;
109	}
110	
111	protected function depend($module)
112	{
113		$this->depends[] = $module;
114	}
115	
116	private function getDepends()
117	{
118		return $this->depends;
119	}
120}