Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

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