refine.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Oil;
  13. /**
  14. * Oil\Refine Class
  15. *
  16. * @package Fuel
  17. * @subpackage Oil
  18. * @category Core
  19. */
  20. class Refine
  21. {
  22. public static function run($task, $args)
  23. {
  24. $task = strtolower($task);
  25. // Make sure something is set
  26. if (empty($task) or $task === 'help')
  27. {
  28. static::help();
  29. return;
  30. }
  31. $module = false;
  32. list($module, $task) = array_pad(explode('::', $task), 2, null);
  33. if ($task === null)
  34. {
  35. $task = $module;
  36. $module = false;
  37. }
  38. if ($module)
  39. {
  40. try
  41. {
  42. \Module::load($module);
  43. $path = \Module::exists($module);
  44. \Finder::instance()->add_path($path);
  45. }
  46. catch (\FuelException $e)
  47. {
  48. throw new Exception(sprintf('Module "%s" does not exist.', $module));
  49. }
  50. }
  51. // Just call and run() or did they have a specific method in mind?
  52. list($task, $method) = array_pad(explode(':', $task), 2, 'run');
  53. // Find the task
  54. if ( ! $file = \Finder::search('tasks', $task))
  55. {
  56. $files = \Finder::instance()->list_files('tasks');
  57. $possibilities = array();
  58. foreach($files as $file)
  59. {
  60. $possible_task = pathinfo($file, \PATHINFO_FILENAME);
  61. $difference = levenshtein($possible_task, $task);
  62. $possibilities[$difference] = $possible_task;
  63. }
  64. ksort($possibilities);
  65. if ($possibilities and current($possibilities) <= 5)
  66. {
  67. throw new Exception(sprintf('Task "%s" does not exist. Did you mean "%s"?', $task, current($possibilities)));
  68. }
  69. else
  70. {
  71. throw new Exception(sprintf('Task "%s" does not exist.', $task));
  72. }
  73. return;
  74. }
  75. require_once $file;
  76. $task = '\\Fuel\\Tasks\\'.ucfirst($task);
  77. $new_task = new $task;
  78. // The help option hs been called, so call help instead
  79. if (\Cli::option('help') && is_callable(array($new_task, 'help')))
  80. {
  81. $method = 'help';
  82. }
  83. if ($return = call_user_func_array(array($new_task, $method), $args))
  84. {
  85. \Cli::write($return);
  86. }
  87. }
  88. public static function help()
  89. {
  90. // Build a list of possible tasks for the help output
  91. $tasks = self::_discover_tasks();
  92. if (count($tasks) > 0)
  93. {
  94. $output_available_tasks = "";
  95. foreach ($tasks as $task => $options)
  96. {
  97. foreach ($options as $option)
  98. {
  99. $option = ($option == "run") ? "" : ":$option";
  100. $output_available_tasks .= " php oil refine $task$option\n";
  101. }
  102. }
  103. }
  104. else
  105. {
  106. $output_available_tasks = " (none found)";
  107. }
  108. $output = <<<HELP
  109. Usage:
  110. php oil [r|refine] <taskname>
  111. Description:
  112. Tasks are classes that can be run through the the command line or set up as a cron job.
  113. Available tasks:
  114. $output_available_tasks
  115. Documentation:
  116. http://docs.fuelphp.com/packages/oil/refine.html
  117. HELP;
  118. \Cli::write($output);
  119. }
  120. /**
  121. * Find all of the task classes in the system and use reflection to discover the
  122. * commands we can call.
  123. *
  124. * @return array $taskname => array($taskmethods)
  125. **/
  126. protected static function _discover_tasks()
  127. {
  128. $result = array();
  129. $files = \Finder::instance()->list_files('tasks');
  130. if (count($files) > 0)
  131. {
  132. foreach ($files as $file)
  133. {
  134. $task_name = str_replace('.php', '', basename($file));
  135. $class_name = '\\Fuel\\Tasks\\'.$task_name;
  136. require $file;
  137. $reflect = new \ReflectionClass($class_name);
  138. // Ensure we only pull out the public methods
  139. $methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
  140. $result[$task_name] = array();
  141. if (count($methods) > 0)
  142. {
  143. foreach ($methods as $method)
  144. {
  145. strpos($method->name, '_') !== 0 and $result[$task_name][] = $method->name;
  146. }
  147. }
  148. }
  149. }
  150. return $result;
  151. }
  152. }