PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Application/Routers/CliRouter.php

http://github.com/nette/nette
PHP | 129 lines | 69 code | 26 blank | 34 comment | 12 complexity | 063222a6b4b9c01dab6deb50f0ef9e8a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (http://nette.org)
  4. *
  5. * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6. *
  7. * For the full copyright and license information, please view
  8. * the file license.txt that was distributed with this source code.
  9. */
  10. namespace Nette\Application\Routers;
  11. use Nette,
  12. Nette\Application;
  13. /**
  14. * The unidirectional router for CLI. (experimental)
  15. *
  16. * @author David Grudl
  17. *
  18. * @property-read array $defaults
  19. */
  20. class CliRouter extends Nette\Object implements Application\IRouter
  21. {
  22. const PRESENTER_KEY = 'action';
  23. /** @var array */
  24. private $defaults;
  25. /**
  26. * @param array default values
  27. */
  28. public function __construct($defaults = array())
  29. {
  30. $this->defaults = $defaults;
  31. }
  32. /**
  33. * Maps command line arguments to a Request object.
  34. * @param Nette\Http\IRequest
  35. * @return Nette\Application\Request|NULL
  36. */
  37. public function match(Nette\Http\IRequest $httpRequest)
  38. {
  39. if (empty($_SERVER['argv']) || !is_array($_SERVER['argv'])) {
  40. return NULL;
  41. }
  42. $names = array(self::PRESENTER_KEY);
  43. $params = $this->defaults;
  44. $args = $_SERVER['argv'];
  45. array_shift($args);
  46. $args[] = '--';
  47. foreach ($args as $arg) {
  48. $opt = preg_replace('#/|-+#A', '', $arg);
  49. if ($opt === $arg) {
  50. if (isset($flag) || $flag = array_shift($names)) {
  51. $params[$flag] = $arg;
  52. } else {
  53. $params[] = $arg;
  54. }
  55. $flag = NULL;
  56. continue;
  57. }
  58. if (isset($flag)) {
  59. $params[$flag] = TRUE;
  60. $flag = NULL;
  61. }
  62. if ($opt !== '') {
  63. $pair = explode('=', $opt, 2);
  64. if (isset($pair[1])) {
  65. $params[$pair[0]] = $pair[1];
  66. } else {
  67. $flag = $pair[0];
  68. }
  69. }
  70. }
  71. if (!isset($params[self::PRESENTER_KEY])) {
  72. throw new Nette\InvalidStateException('Missing presenter & action in route definition.');
  73. }
  74. $presenter = $params[self::PRESENTER_KEY];
  75. if ($a = strrpos($presenter, ':')) {
  76. $params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
  77. $presenter = substr($presenter, 0, $a);
  78. }
  79. return new Application\Request(
  80. $presenter,
  81. 'CLI',
  82. $params
  83. );
  84. }
  85. /**
  86. * This router is only unidirectional.
  87. * @param Nette\Application\Request
  88. * @param Nette\Http\Url
  89. * @return NULL
  90. */
  91. public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl)
  92. {
  93. return NULL;
  94. }
  95. /**
  96. * Returns default values.
  97. * @return array
  98. */
  99. public function getDefaults()
  100. {
  101. return $this->defaults;
  102. }
  103. }