Packages

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