Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

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