Source for file CliRouter.php

Documentation is available at CliRouter.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Application
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * The unidirectional router for CLI. (experimental)
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Application
  20. 20:  */
  21. 21: class CliRouter extends Object implements IRouter
  22. 22: {
  23. 23:     const PRESENTER_KEY = 'action';
  24. 24:  
  25. 25:     /** @var array */
  26. 26:     private $defaults;
  27. 27:  
  28. 28:  
  29. 29:  
  30. 30:     /**
  31. 31:      * @param  array   default values
  32. 32:      */
  33. 33:     public function __construct($defaults array())
  34. 34:     {
  35. 35:         $this->defaults $defaults;
  36. 36:     }
  37. 37:  
  38. 38:  
  39. 39:  
  40. 40:     /**
  41. 41:      * Maps command line arguments to a PresenterRequest object.
  42. 42:      * @param  IHttpRequest 
  43. 43:      * @return PresenterRequest|NULL
  44. 44:      */
  45. 45:     public function match(IHttpRequest $httpRequest)
  46. 46:     {
  47. 47:         if (empty($_SERVER['argv']|| !is_array($_SERVER['argv'])) {
  48. 48:             return NULL;
  49. 49:         }
  50. 50:  
  51. 51:         $names array(self::PRESENTER_KEY);
  52. 52:         $params $this->defaults;
  53. 53:         $args $_SERVER['argv'];
  54. 54:         array_shift($args);
  55. 55:         $args['--';
  56. 56:  
  57. 57:         foreach ($args as $arg{
  58. 58:             $opt preg_replace('#/|-+#A'''$arg);
  59. 59:             if ($opt === $arg{
  60. 60:                 if (isset($flag|| $flag array_shift($names)) {
  61. 61:                     $params[$flag$arg;
  62. 62:                 else {
  63. 63:                     $params[$arg;
  64. 64:                 }
  65. 65:                 $flag NULL;
  66. 66:                 continue;
  67. 67:             }
  68. 68:  
  69. 69:             if (isset($flag)) {
  70. 70:                 $params[$flagTRUE;
  71. 71:                 $flag NULL;
  72. 72:             }
  73. 73:  
  74. 74:             if ($opt !== ''{
  75. 75:                 $pair explode('='$opt2);
  76. 76:                 if (isset($pair[1])) {
  77. 77:                     $params[$pair[0]] $pair[1];
  78. 78:                 else {
  79. 79:                     $flag $pair[0];
  80. 80:                 }
  81. 81:             }
  82. 82:         }
  83. 83:  
  84. 84:         if (!isset($params[self::PRESENTER_KEY])) {
  85. 85:             throw new InvalidStateException('Missing presenter & action in route definition.');
  86. 86:         }
  87. 87:         $presenter $params[self::PRESENTER_KEY];
  88. 88:         if ($a strrpos($presenter':')) {
  89. 89:             $params[self::PRESENTER_KEYsubstr($presenter$a 1);
  90. 90:             $presenter substr($presenter0$a);
  91. 91:         }
  92. 92:  
  93. 93:         return new PresenterRequest(
  94. 94:             $presenter,
  95. 95:             'CLI',
  96. 96:             $params
  97. 97:         );
  98. 98:     }
  99. 99:  
  100. 100:  
  101. 101:  
  102. 102:     /**
  103. 103:      * This router is only unidirectional.
  104. 104:      * @param  IHttpRequest 
  105. 105:      * @param  PresenterRequest 
  106. 106:      * @return NULL 
  107. 107:      */
  108. 108:     public function constructUrl(PresenterRequest $appRequestIHttpRequest $httpRequest)
  109. 109:     {
  110. 110:         return NULL;
  111. 111:     }
  112. 112:  
  113. 113:  
  114. 114:  
  115. 115:     /**
  116. 116:      * Returns default values.
  117. 117:      * @return array 
  118. 118:      */
  119. 119:     public function getDefaults()
  120. 120:     {
  121. 121:         return $this->defaults;
  122. 122:     }
  123. 123: