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

Classes

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