PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/api/class.Addon.php

https://bitbucket.org/xertoz/forge
PHP | 162 lines | 93 code | 14 blank | 55 comment | 1 complexity | 0f2df1936bca6bb076b7e09a29b17d02 MD5 | raw file
  1. <?php
  2. /**
  3. * class.Addon.php
  4. * Copyright 2009-2012 Mattias Lindholm
  5. *
  6. * This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
  7. * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or send a letter
  8. * to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
  9. */
  10. namespace forge;
  11. /**
  12. * Modules and components must be derived from this class
  13. */
  14. abstract class Addon {
  15. use Versioning;
  16. /**
  17. * Define permissions issued
  18. * @var array
  19. */
  20. static protected $permissions = array();
  21. /**
  22. * Is this a component?
  23. * @return bool
  24. */
  25. static final public function isComponent() {
  26. return in_array('forge\Component',class_parents(self::getName(true)));
  27. }
  28. /**
  29. * Is this a module?
  30. * @return bool
  31. */
  32. static final public function isModule() {
  33. return in_array('forge\Module',class_parents(self::getName(true)));
  34. }
  35. /**
  36. * Is the given component loaded?
  37. * @param $component string Component's system name
  38. * @return boolean
  39. */
  40. static final public function existsComponent($component) {
  41. return class_exists('forge\\components\\'.$component);
  42. }
  43. /**
  44. * Is the given module loaded?
  45. * @param $module string Module's system name
  46. * @return boolean
  47. */
  48. static final public function existsModule($module) {
  49. return class_exists('forge\\modules\\'.$module);
  50. }
  51. /**
  52. * Get all loaded components
  53. * @param $long boolean Return the long (namespaced) class name?
  54. * @return array
  55. */
  56. static final public function getComponents($long=false) {
  57. $components = array();
  58. foreach (glob(FORGE_PATH.'/components/*') as $raw) {
  59. $name = substr($raw, strlen(FORGE_PATH.'/components/'));
  60. $components[] = $long ? 'forge\\components\\'.$name : $name;
  61. }
  62. return $components;
  63. }
  64. /**
  65. * Get all loaded addons
  66. * @param $long boolean Return the long (namespaced) class names?
  67. * @return array
  68. */
  69. static final public function getAddons($long=false) {
  70. $components = self::getComponents($long);
  71. $modules = self::getModules($long);
  72. $addons = array_merge($components, $modules);
  73. sort($addons);
  74. return $addons;
  75. }
  76. /**
  77. * Get all loaded modules
  78. * @param $long boolean Return the long (namespaced) class name?
  79. * @return array
  80. */
  81. static final public function getModules($long=false) {
  82. $modules = array();
  83. foreach (glob('modules/*') as $raw) {
  84. $name = substr($raw, strlen('modules/'));
  85. $modules[] = $long ? 'forge\\modules\\'.$name : $name;
  86. }
  87. return $modules;
  88. }
  89. /**
  90. * Get name
  91. * @param Get full class name
  92. * @return string
  93. */
  94. static final public function getName($long=false) {
  95. $ref = explode('\\',get_called_class());
  96. return $long ? get_called_class() : array_pop($ref);
  97. }
  98. /**
  99. * Get list of tables
  100. * @param $long bool Full name of the classes?
  101. * @return array
  102. * @throws Exception
  103. */
  104. static public function getTables($long=true) {
  105. $tables = array();
  106. $files = glob(FORGE_PATH.'/'.($path = (substr(str_replace('\\', '/', get_called_class()), strlen('forge/')).'/db/')).'*');
  107. foreach ($files as $file) {
  108. preg_match('$'.$path.'class.(\w+).php$', $file, $m);
  109. if (count($m))
  110. $tables[] = ($long ? 'forge\\'.str_replace('/', '\\', $path) : null).$m[1];
  111. }
  112. return $tables;
  113. }
  114. /**
  115. * Get list of classes available in namespace
  116. * @param string Subspace (NULL gives all)
  117. * @return array
  118. */
  119. static public function getNamespace($subspace=null) {
  120. $classes = array();
  121. $path = str_replace('\\', '/', substr(get_called_class(), strlen('forge\\'))).'/'.str_replace('\\', '/', $subspace).'/';
  122. if (($files = glob($path.'class.*.php')) !== false)
  123. foreach ($files as $file) {
  124. preg_match_all('#'.$path.'class.(\w+).php#', $file, $matches);
  125. $classes[] = get_called_class().'\\'.(strlen($subspace) ? $subspace.'\\' : null).array_pop($matches[1]);
  126. }
  127. return $classes;
  128. }
  129. /**
  130. * Get permissions issued
  131. * @return array
  132. */
  133. static public function getPermissions() {
  134. $class = get_called_class();
  135. return $class::$permissions;
  136. }
  137. }