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
  • Deprecated
  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:      * @return Nette\Application\Request|NULL
 48:      */
 49:     public function match(Nette\Http\IRequest $httpRequest)
 50:     {
 51:         if (empty($_SERVER['argv']) || !is_array($_SERVER['argv'])) {
 52:             return NULL;
 53:         }
 54: 
 55:         $names = array(self::PRESENTER_KEY);
 56:         $params = $this->defaults;
 57:         $args = $_SERVER['argv'];
 58:         array_shift($args);
 59:         $args[] = '--';
 60: 
 61:         foreach ($args as $arg) {
 62:             $opt = preg_replace('#/|-+#A', '', $arg);
 63:             if ($opt === $arg) {
 64:                 if (isset($flag) || $flag = array_shift($names)) {
 65:                     $params[$flag] = $arg;
 66:                 } else {
 67:                     $params[] = $arg;
 68:                 }
 69:                 $flag = NULL;
 70:                 continue;
 71:             }
 72: 
 73:             if (isset($flag)) {
 74:                 $params[$flag] = TRUE;
 75:                 $flag = NULL;
 76:             }
 77: 
 78:             if ($opt !== '') {
 79:                 $pair = explode('=', $opt, 2);
 80:                 if (isset($pair[1])) {
 81:                     $params[$pair[0]] = $pair[1];
 82:                 } else {
 83:                     $flag = $pair[0];
 84:                 }
 85:             }
 86:         }
 87: 
 88:         if (!isset($params[self::PRESENTER_KEY])) {
 89:             throw new Nette\InvalidStateException('Missing presenter & action in route definition.');
 90:         }
 91:         $presenter = $params[self::PRESENTER_KEY];
 92:         if ($a = strrpos($presenter, ':')) {
 93:             $params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
 94:             $presenter = substr($presenter, 0, $a);
 95:         }
 96: 
 97:         return new Application\Request(
 98:             $presenter,
 99:             'CLI',
100:             $params
101:         );
102:     }
103: 
104: 
105: 
106:     /**
107:      * This router is only unidirectional.
108:      * @return NULL
109:      */
110:     public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl)
111:     {
112:         return NULL;
113:     }
114: 
115: 
116: 
117:     /**
118:      * Returns default values.
119:      * @return array
120:      */
121:     public function getDefaults()
122:     {
123:         return $this->defaults;
124:     }
125: 
126: }
127: 
Nette Framework 2.0.10 API API documentation generated by ApiGen 2.8.0