/framework/ZoopModule.php

http://zoop.googlecode.com/ · PHP · 120 lines · 77 code · 18 blank · 25 comment · 6 complexity · 8d669ec6695dffd4611d744ae75a2544 MD5 · raw file

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