Cli.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. use Pimf\Util\String;
  10. /**
  11. * A full featured package for managing command-line options and arguments,
  12. * it allows the developer to easily build complex command line interfaces.
  13. *
  14. * @package Pimf
  15. * @author Gjero Krsteski <[email protected]>
  16. */
  17. final class Cli
  18. {
  19. /**
  20. * Prints out a list of CLI commands from the system,
  21. * which is defined at the controllers with the "CliAction()" suffix at the method-name.
  22. *
  23. * @param string $appClr Path to application controller repository
  24. * @param string $coreClr Path to core controller repository
  25. * @param string $root Path to home directory
  26. */
  27. public static function absorb($appClr = null, $coreClr = null, $root = null)
  28. {
  29. echo PHP_EOL . 'PIMF v' . \Pimf\Application::VERSION . ' PHP Command Line Interface by Gjero Krsteski' . PHP_EOL;
  30. echo '+------------------------------------------------------+' . PHP_EOL;
  31. self::reflect(self::collect($appClr, $coreClr, $root));
  32. }
  33. /**
  34. * @param array $classes
  35. */
  36. public static function reflect(array $classes)
  37. {
  38. array_map(
  39. function ($class) {
  40. $reflection = new \ReflectionClass($class);
  41. if ($reflection->isSubclassOf('\Pimf\Controller\Base')) {
  42. $methods = $reflection->getMethods();
  43. $controller = explode('_', $class);
  44. echo 'controller: ' . strtolower(end($controller)) . '' . PHP_EOL;
  45. array_map(
  46. function ($method) {
  47. if (false !== $command = strstr($method->getName(), 'CliAction', true)) {
  48. echo PHP_EOL . ' action: ' . $command . ' ' . PHP_EOL;
  49. }
  50. }, $methods
  51. );
  52. echo PHP_EOL . '+------------------------------------------------------+' . PHP_EOL;
  53. }
  54. }, $classes
  55. );
  56. }
  57. /**
  58. * @param string $appClr
  59. * @param string $coreClr
  60. * @param string $root
  61. *
  62. * @return array
  63. */
  64. public static function collect($appClr = null, $coreClr = null, $root = null)
  65. {
  66. $classes = array();
  67. $conf = Registry::get('conf');
  68. $dis = DIRECTORY_SEPARATOR;
  69. if (!$root && !$coreClr && !$appClr) {
  70. // compute the PIMF framework path restriction.
  71. $root = dirname(dirname(dirname(dirname(__FILE__))));
  72. $coreClr = str_replace('/', $dis, $root . '/pimf-framework/core/Pimf/Controller/');
  73. $appClr = str_replace('/', $dis, $root . '/app/' . $conf['app']['name'] . '/Controller/');
  74. }
  75. foreach (array($appClr, $coreClr) as $dir) {
  76. $iterator
  77. = new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)), '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
  78. foreach (iterator_to_array($iterator, false) as $file) {
  79. $file = str_replace("\\", '/', current($file));
  80. $file = str_replace('/', $dis, $file);
  81. $name = str_replace(
  82. array($root . $dis . 'pimf-framework' . $dis . 'core' . $dis, $root . $dis . 'app' . $dis), '', $file
  83. );
  84. $name = str_replace($dis, '\\', $name);
  85. $name = str_replace('.php', '', $name);
  86. $classes[] = '\\' . $name;
  87. }
  88. }
  89. return $classes;
  90. }
  91. /**
  92. * @param array $commands
  93. *
  94. * @return array
  95. */
  96. public static function parse(array $commands)
  97. {
  98. $cli = array();
  99. parse_str(implode('&', array_slice($commands, 1)), $cli);
  100. $command = current(array_keys((array)$cli, ''));
  101. if (String::contains($command, ':')) {
  102. list($controller, $action) = explode(':', $command);
  103. $cli['controller'] = $controller;
  104. $cli['action'] = $action;
  105. }
  106. return $cli;
  107. }
  108. }