A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.1 KiB

  1. <?php
  2. namespace SparkLib\Application\Environment;
  3. use \SparkLib\Application\Environment;
  4. use \SparkLib\Application\Request\Get;
  5. use \SparkLib\Application\Request\Post;
  6. use \SparkLib\Application\Request\Head;
  7. use \SparkLib\Application\Request\Delete;
  8. /**
  9. * An application environment for the command line. Mangles ARGV,
  10. * STDIN, etc., into objects that can be treated by Application more
  11. * or less the same way it treats data from HTTP requests.
  12. *
  13. * See comments on Environment\HTTP for more detailed rationale.
  14. *
  15. * Arguments work like so:
  16. *
  17. * $ executable
  18. * $ executable controller
  19. * $ executable controller action
  20. * $ executable controller action param1=val param2=val ...
  21. *
  22. * Which should correspond almost exactly to:
  23. *
  24. * /index.php
  25. * /index.php/controller
  26. * /index.php/controller/action
  27. * /index.php/controller/action?param1=val
  28. *
  29. */
  30. class CLI extends Environment {
  31. protected $_keyvalpat = '/
  32. (?<key> .*?)
  33. =
  34. (?<val> .*)
  35. /x';
  36. protected $_flagpat = '/
  37. ^-+
  38. (?<flag> [a-z]+)
  39. /xi';
  40. protected $_flags = [];
  41. public function __construct ()
  42. {
  43. global $argv;
  44. $script = array_shift($argv);
  45. $request = [];
  46. $path_arr = [];
  47. $argc = 1;
  48. foreach ($argv as $arg) {
  49. $matches = [];
  50. if (preg_match($this->_keyvalpat, $arg, $matches)) {
  51. // handle pulling key/values into the "request"
  52. $request[ $matches['key'] ] = $matches['val'];
  53. } else if (preg_match($this->_flagpat, $arg, $matches)) {
  54. // set some bits for --flag style flags
  55. $this->_flags[ $matches['flag'] ] = true;
  56. } else if (count($path_arr) >= 2) {
  57. $request['arg' . ($argc++)] = $arg;
  58. } else {
  59. // tack other strings on to path
  60. $path_arr[] = $arg;
  61. }
  62. }
  63. // we need a leading slash and a slash-separated path
  64. // for all the routing crap:
  65. $this->_path = implode('/', $path_arr);
  66. // if we have a path, but it doesn't start with a leading slash,
  67. // add one:
  68. if (strlen($this->_path)) {
  69. if ($this->_path[0] !== '/') {
  70. $this->_path = '/' . $this->_path;
  71. }
  72. }
  73. // set up a Request object
  74. switch ($this->method()) {
  75. case 'GET' : $this->_req = new Get($request); break;
  76. case 'POST' : $this->_req = new Post($request); break;
  77. case 'DELETE' : $this->_req = new Delete($request); break;
  78. case 'HEAD' : $this->_req = new Head($request); break;
  79. default : $this->_req = new Get($request);
  80. }
  81. }
  82. public function method ()
  83. {
  84. // silly, liable to break:
  85. if (isset($this->_flags['get'])) return 'GET';
  86. if (isset($this->_flags['post'])) return 'POST';
  87. if (isset($this->_flags['delete'])) return 'DELETE';
  88. if (isset($this->_flags['head'])) return 'HEAD';
  89. // silly default:
  90. return 'GET';
  91. }
  92. /**
  93. * For now, just fake a session.
  94. */
  95. public function startSession ()
  96. {
  97. $GLOBALS['_SESSION'] = [];
  98. }
  99. /**
  100. * End the current session.
  101. */
  102. public function endSession ()
  103. {
  104. $GLOBALS['_SESSION'] = array();
  105. }
  106. public function header ($header)
  107. {
  108. return;
  109. }
  110. }