Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • 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, 2011 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: class CliRouter extends Nette\Object implements Application\IRouter
 25: {
 26:     const PRESENTER_KEY = 'action';
 27: 
 28:     /** @var array */
 29:     private $defaults;
 30: 
 31: 
 32: 
 33:     /**
 34:      * @param  array   default values
 35:      */
 36:     public function __construct($defaults = array())
 37:     {
 38:         $this->defaults = $defaults;
 39:     }
 40: 
 41: 
 42: 
 43:     /**
 44:      * Maps command line arguments to a Request object.
 45:      * @param  Nette\Http\IRequest
 46:      * @return Nette\Application\Request|NULL
 47:      */
 48:     public function match(Nette\Http\IRequest $httpRequest)
 49:     {
 50:         if (empty($_SERVER['argv']) || !is_array($_SERVER['argv'])) {
 51:             return NULL;
 52:         }
 53: 
 54:         $names = array(self::PRESENTER_KEY);
 55:         $params = $this->defaults;
 56:         $args = $_SERVER['argv'];
 57:         array_shift($args);
 58:         $args[] = '--';
 59: 
 60:         foreach ($args as $arg) {
 61:             $opt = preg_replace('#/|-+#A', '', $arg);
 62:             if ($opt === $arg) {
 63:                 if (isset($flag) || $flag = array_shift($names)) {
 64:                     $params[$flag] = $arg;
 65:                 } else {
 66:                     $params[] = $arg;
 67:                 }
 68:                 $flag = NULL;
 69:                 continue;
 70:             }
 71: 
 72:             if (isset($flag)) {
 73:                 $params[$flag] = TRUE;
 74:                 $flag = NULL;
 75:             }
 76: 
 77:             if ($opt !== '') {
 78:                 $pair = explode('=', $opt, 2);
 79:                 if (isset($pair[1])) {
 80:                     $params[$pair[0]] = $pair[1];
 81:                 } else {
 82:                     $flag = $pair[0];
 83:                 }
 84:             }
 85:         }
 86: 
 87:         if (!isset($params[self::PRESENTER_KEY])) {
 88:             throw new Nette\InvalidStateException('Missing presenter & action in route definition.');
 89:         }
 90:         $presenter = $params[self::PRESENTER_KEY];
 91:         if ($a = strrpos($presenter, ':')) {
 92:             $params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
 93:             $presenter = substr($presenter, 0, $a);
 94:         }
 95: 
 96:         return new Application\Request(
 97:             $presenter,
 98:             'CLI',
 99:             $params
100:         );
101:     }
102: 
103: 
104: 
105:     /**
106:      * This router is only unidirectional.
107:      * @param  Nette\Application\Request
108:      * @param  Nette\Http\Url
109:      * @return NULL
110:      */
111:     public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl)
112:     {
113:         return NULL;
114:     }
115: 
116: 
117: 
118:     /**
119:      * Returns default values.
120:      * @return array
121:      */
122:     public function getDefaults()
123:     {
124:         return $this->defaults;
125:     }
126: 
127: }
128: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0