Packages

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