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