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
  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:      * @param  IHttpRequest
 45:      * @return PresenterRequest|NULL
 46:      */
 47:     public function match(IHttpRequest $httpRequest)
 48:     {
 49:         if (empty($_SERVER['argv']) || !is_array($_SERVER['argv'])) {
 50:             return NULL;
 51:         }
 52: 
 53:         $names = array(self::PRESENTER_KEY);
 54:         $params = $this->defaults;
 55:         $args = $_SERVER['argv'];
 56:         array_shift($args);
 57:         $args[] = '--';
 58: 
 59:         foreach ($args as $arg) {
 60:             $opt = preg_replace('#/|-+#A', '', $arg);
 61:             if ($opt === $arg) {
 62:                 if (isset($flag) || $flag = array_shift($names)) {
 63:                     $params[$flag] = $arg;
 64:                 } else {
 65:                     $params[] = $arg;
 66:                 }
 67:                 $flag = NULL;
 68:                 continue;
 69:             }
 70: 
 71:             if (isset($flag)) {
 72:                 $params[$flag] = TRUE;
 73:                 $flag = NULL;
 74:             }
 75: 
 76:             if ($opt !== '') {
 77:                 $pair = explode('=', $opt, 2);
 78:                 if (isset($pair[1])) {
 79:                     $params[$pair[0]] = $pair[1];
 80:                 } else {
 81:                     $flag = $pair[0];
 82:                 }
 83:             }
 84:         }
 85: 
 86:         if (!isset($params[self::PRESENTER_KEY])) {
 87:             throw new InvalidStateException('Missing presenter & action in route definition.');
 88:         }
 89:         $presenter = $params[self::PRESENTER_KEY];
 90:         if ($a = strrpos($presenter, ':')) {
 91:             $params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
 92:             $presenter = substr($presenter, 0, $a);
 93:         }
 94: 
 95:         return new PresenterRequest(
 96:             $presenter,
 97:             'CLI',
 98:             $params
 99:         );
100:     }
101: 
102: 
103: 
104:     /**
105:      * This router is only unidirectional.
106:      * @param  PresenterRequest
107:      * @param  Url
108:      * @return NULL
109:      */
110:     public function constructUrl(PresenterRequest $appRequest, 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.1 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0