Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

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