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