/src/class/core/SimpleModule.class.php

https://bitbucket.org/stk2k/charcoalphp2.1 · PHP · 348 lines · 166 code · 65 blank · 117 comment · 35 complexity · 6344e6a104c31222fb963558e353dadf MD5 · raw file

  1. <?php
  2. /**
  3. * 基本的なモジュール実装
  4. *
  5. * PHP version 5
  6. *
  7. * @package class.core
  8. * @author CharcoalPHP Development Team
  9. * @copyright 2008 stk2k, sazysoft
  10. */
  11. class Charcoal_SimpleModule extends Charcoal_CharcoalComponent implements Charcoal_IModule
  12. {
  13. /** @var Charcoal_Vector */
  14. private $tasks;
  15. /** @var Charcoal_Vector */
  16. private $events;
  17. /** @var Charcoal_Vector */
  18. private $required_modules;
  19. /*
  20. * コンストラクタ
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. }
  26. /*
  27. * Initialize instance
  28. *
  29. * @param array $config configuration data
  30. */
  31. public function configure( $config )
  32. {
  33. parent::configure( $config );
  34. $config = new Charcoal_HashMap($config);
  35. $this->tasks = $config->getArray( 'tasks', array() );
  36. $this->events = $config->getArray( 'events', array() );
  37. $this->required_modules = $config->getArray( 'required_modules', array() );
  38. }
  39. /*
  40. * get required module names
  41. */
  42. public function getRequiredModules()
  43. {
  44. return $this->required_modules;
  45. }
  46. /**
  47. * load a task
  48. *
  49. * @param Charcoal_ObjectPath $obj_path
  50. * @param Charcoal_String|string $path
  51. * @param Charcoal_ITaskManager $task_manager
  52. *
  53. * @return Charcoal_ObjectPath
  54. */
  55. private function loadTask( $obj_path, $path, $task_manager )
  56. {
  57. $event_stream = $this->getSandbox()->getCoreHookEventStream();
  58. // file base name
  59. $base_name = basename( $path );
  60. // retrieve class name from file name
  61. $pos = strpos( $base_name, '.class.php' );
  62. $class_name = substr( $base_name, 0, $pos );
  63. // include source file
  64. Charcoal_Framework::loadSourceFile( $path );
  65. $event_stream->push( 'simple_module.loaded_module', $path, true );
  66. // create new instance
  67. $klass = new Charcoal_Class( $class_name );
  68. $task = $klass->newInstance();
  69. // check if th object implements Charcoal_Task interface
  70. if ( !($task instanceof Charcoal_Task) ){
  71. // Invoke Exception
  72. _throw( new Charcoal_InterfaceImplementException( $task, 'Charcoal_Task' ) );
  73. }
  74. log_info( 'system, event, debug', "created task[$task] in module[$obj_path]");
  75. // build object path for the task
  76. $obj_name = $task->getObjectName();
  77. $task_path = s($obj_name . '@' . $obj_path->getVirtualPath());
  78. // set task property
  79. $task->setObjectPath( $task_path );
  80. $task->setTypeName( 'task' );
  81. $task->setSandbox( $this->getSandbox() );
  82. // load object config
  83. $config = Charcoal_ConfigLoader::loadConfig( $this->getSandbox()->getRegistry(), $task_path, 'task' );
  84. // configure task
  85. $task->configure( $config );
  86. // log_info( 'system, event, debug', "task[$task] configured.");
  87. // regiser task
  88. $task_manager->registerTask( $task_path, $task );
  89. log_info( 'system, event, debug', "task[$class_name] registered as: [$task_path]");
  90. return $task->getObjectPath();
  91. }
  92. /**
  93. * load an event
  94. *
  95. * @param Charcoal_ObjectPath $obj_path
  96. * @param string|Charcoal_String $path
  97. *
  98. * @return Charcoal_ObjectPath
  99. */
  100. private function loadEvent( $obj_path, $path )
  101. {
  102. // file base name
  103. //$base_name = basename( $path );
  104. // retrieve class name from file name
  105. //$pos = strpos( $base_name, '.class.php' );
  106. //$class_name = substr( $base_name, 0, $pos );
  107. // include source file
  108. Charcoal_Framework::loadSourceFile( $path );
  109. /*
  110. // create new instance
  111. $klass = new Charcoal_Class( $class_name );
  112. $event = $klass->newInstance();
  113. log_info( 'system, event, debug', "module", "created event[$event] in module[$obj_path]");
  114. // build object path for the event
  115. $obj_name = $event->getObjectName();
  116. $event_path = $obj_name . '@' . $obj_path->getVirtualPath();
  117. // $event_path = new Charcoal_ObjectPath( $event_path );
  118. // log_info( 'system, event, debug', "module", "event[$event] path: [$event_path]");
  119. // set task property
  120. $event->setObjectPath( $event_path );
  121. $event->setTypeName( 'event' );
  122. // load object config
  123. $config = Charcoal_ConfigLoader::loadConfig( $this->getSandbox(), $event_path, 'event' );
  124. $config = new Charcoal_Config( $this->getSandbox()->getEnvironment(), $config );
  125. // configure event
  126. $event->configure( $config );
  127. // log_info( 'system, event, debug', "module", "event[$event] configured.");
  128. */
  129. return $obj_path;
  130. }
  131. /**
  132. * load tasks in module directory
  133. *
  134. * @param Charcoal_ITaskManager $task_manager
  135. *
  136. * @return Charcoal_ITask[] loaded tasks
  137. */
  138. public function loadTasks( $task_manager )
  139. {
  140. $loadedtasks = NULL;
  141. $root_path = $this->getObjectPath();
  142. //==================================
  143. // load task source code
  144. //==================================
  145. $real_path = $root_path->getRealPath();
  146. $webapp_path = Charcoal_ResourceLocator::getApplicationPath( 'module' . $real_path );
  147. $project_path = Charcoal_ResourceLocator::getProjectPath( 'module' . $real_path );
  148. $framework_path = Charcoal_ResourceLocator::getFrameworkPath( 'module' . $real_path );
  149. $task_class_suffix = 'Task.class.php';
  150. $task = NULL;
  151. if ( is_dir($webapp_path) && $dh = opendir($webapp_path) )
  152. {
  153. log_info( 'system, event, debug', "module", "webapp_path is existing.");
  154. while( ($file = readdir($dh)) !== FALSE )
  155. {
  156. if ( $file === '.' || $file === '..' ) continue;
  157. if ( strpos($file,$task_class_suffix) !== FALSE ){
  158. $loadedtasks[] = $this->loadTask( $root_path, "$webapp_path/$file", $task_manager );
  159. }
  160. }
  161. closedir($dh);
  162. }
  163. if ( is_dir($project_path) && $dh = opendir($project_path) )
  164. {
  165. log_info( 'system, event, debug', "module", "project_path is existing.");
  166. while( ($file = readdir($dh)) !== FALSE )
  167. {
  168. if ( $file === '.' || $file === '..' ) continue;
  169. if ( strpos($file,$task_class_suffix) !== FALSE ){
  170. $loadedtasks[] = $this->loadTask( $root_path, "$project_path/$file", $task_manager );
  171. }
  172. }
  173. closedir($dh);
  174. }
  175. if ( is_dir($framework_path) && $dh = opendir($framework_path) )
  176. {
  177. log_info( 'system, event, debug', "module", "framework_path is existing.");
  178. while( ($file = readdir($dh)) !== FALSE )
  179. {
  180. if ( $file === '.' || $file === '..' ) continue;
  181. if ( strpos($file,$task_class_suffix) !== FALSE ){
  182. $loadedtasks[] = $this->loadTask( $root_path, "$framework_path/$file", $task_manager );
  183. }
  184. }
  185. closedir($dh);
  186. }
  187. //==================================
  188. // create and register tasks
  189. //==================================
  190. // load tasks from config file
  191. if ( $this->tasks ){
  192. foreach( $this->tasks as $task ){
  193. if ( empty($task) ){
  194. continue;
  195. }
  196. $task_path = $task . '@' . $root_path->getVirtualPath();
  197. /** @var Charcoal_Itask $task */
  198. $task = $this->getSandbox()->createObject( $task_path, 'task' );
  199. $task_manager->registerTask( $task_path, $task );
  200. $loadedtasks[] = $task;
  201. }
  202. }
  203. return $loadedtasks;
  204. }
  205. /**
  206. * load events in module directory
  207. *
  208. * @param Charcoal_ITaskManager $task_manager
  209. *
  210. * @return Charcoal_IEvent[] loaded events
  211. */
  212. public function loadEvents( $task_manager )
  213. {
  214. $loadedevents = NULL;
  215. $root_path = $this->getObjectPath();
  216. //==================================
  217. // load event source code
  218. //==================================
  219. $real_path = $root_path->getRealPath();
  220. $webapp_path = Charcoal_ResourceLocator::getApplicationPath( 'module' . $real_path );
  221. $project_path = Charcoal_ResourceLocator::getProjectPath( 'module' . $real_path );
  222. $framework_path = Charcoal_ResourceLocator::getFrameworkPath( 'module' . $real_path );
  223. $event_class_suffix = 'Event.class.php';
  224. if ( is_dir($webapp_path) && $dh = opendir($webapp_path) )
  225. {
  226. while( ($file = readdir($dh)) !== FALSE )
  227. {
  228. if ( $file === '.' || $file === '..' ) continue;
  229. if ( strpos($file,$event_class_suffix) !== FALSE ){
  230. $loadedevents[] = $this->loadEvent( $root_path, "$webapp_path/$file" );
  231. }
  232. }
  233. closedir($dh);
  234. }
  235. if ( is_dir($project_path) && $dh = opendir($project_path) )
  236. {
  237. while( ($file = readdir($dh)) !== FALSE )
  238. {
  239. if ( $file === '.' || $file === '..' ) continue;
  240. if ( strpos($file,$event_class_suffix) !== FALSE ){
  241. $loadedevents[] = $this->loadEvent( $root_path, "$project_path/$file" );
  242. }
  243. }
  244. closedir($dh);
  245. }
  246. if ( is_dir($framework_path) && $dh = opendir($framework_path) )
  247. {
  248. while( ($file = readdir($dh)) !== FALSE )
  249. {
  250. if ( $file === '.' || $file === '..' ) continue;
  251. if ( strpos($file,$event_class_suffix) !== FALSE ){
  252. $loadedevents[] = $this->loadEvent( $root_path, "$framework_path/$file" );
  253. }
  254. }
  255. closedir($dh);
  256. }
  257. //==================================
  258. // create and register events
  259. //==================================
  260. // load events from config file
  261. // log_info( "system,debug", "module", "loading events in module file:" . $this->events );
  262. if ( $this->events ){
  263. foreach( $this->events as $event ){
  264. if ( empty($event) ){
  265. continue;
  266. }
  267. $event_path = $event . '@' . $root_path->getVirtualPath();
  268. $event = $this->getSandbox()->createEvent( $event_path );
  269. // log_info( "system,debug", "module", "created event[" . get_class($event) . "/$event_path] in module[$root_path]");
  270. $loadedevents[] = $event;
  271. }
  272. }
  273. // log_info( "system,debug", "module", "loaded events: " . print_r($loadedevents,true) );
  274. return $loadedevents;
  275. }
  276. }