Overview

Packages

  • PHP
  • Router

Classes

  • Route
  • Router
  • RouterForIncludes
  • SimpleRoute
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Úprava routeru pro web skládaný pomocí include.
  4:  * 
  5:  * @author Viktorie Halasů
  6:  * @link http://projekty.vize.name/router/
  7:  * @version 1.3
  8:  * @package Router
  9:  */
 10: 
 11: class RouterForIncludes extends Router {
 12:     
 13:     
 14:     /** @var array Kontext pro includované soubory */
 15:     protected $context = array();
 16:     
 17:     
 18:     /**
 19:      * Přidá proměnnou do kontextu. 
 20:      * Je možné zadat buď asoc.pole jako jediný parametr, anebo (se dvěma param.) název + hodnotu.
 21:      * @param mixed $name Asoc. pole, anebo jméno, pod kterým má být hodnota v include přístupná.
 22:      * @param mixed $data Hodnota.
 23:      * @return RouterForIncludes (fluent interface)
 24:      * 
 25:      * @throws InvalidArgumentException Pokud jméno proměnné není řetězec
 26:      */
 27:     public function addToContext()  {
 28:         /* asoc. pole */
 29:         if (func_num_args() === 1)  {
 30:             $data = func_get_arg(0);
 31:             if (!is_array($data))   {
 32:                 throw new InvalidArgumentException("Router (include kontext): Pokud je volána jen s jedním argumentem, musí to být pole.");
 33:             }
 34:             foreach ($data as $key => $value)   {
 35:                 $this->addToContext($key, $value);
 36:             }
 37:         /* jméno + hodnota */  
 38:         } elseif (func_num_args() === 2) {
 39:             $name = func_get_arg(0);
 40:             $value = func_get_arg(1);
 41:             if (isset($this->context[$name]))   {
 42:                 trigger_error("Router: Proměnná '$name' už v kontextu existuje.", E_USER_NOTICE);
 43:             }
 44:             if (!is_scalar($name))  {
 45:                 throw new InvalidArgumentException("Router (include kontext): Název proměnné musí být řetězec.");
 46:             }
 47:             $this->context[$name] = $value;
 48:         }
 49:         return $this;
 50:     }
 51:     
 52:     
 53:     /**
 54:      * Vrátí pole se jmény includovaných souborů (a ověří, jestli existují)
 55:      * @param string $str 
 56:      * @return array
 57:      * 
 58:      * @throws RuntimeException Pokud soubor neexistuje
 59:      */
 60:     protected function callbackFromString($str) {
 61:         $cb = array();
 62:         foreach (explode(',', $str) as $file)   {
 63:             if (!file_exists($file))    {
 64:                 throw new RuntimeException("Router: Soubor '$file' neexistuje.");
 65:             }
 66:             $cb[] = $file;
 67:         }
 68:         return $cb;
 69:     }
 70:     
 71:     
 72:     /**
 73:      * Předá řízení (includuje požadované soubory).
 74:      */
 75:     public function delegate() {
 76:         try {
 77:             foreach ($this->callbackFromString($this->callback) as $filename)   {
 78:                 $this->localInclude($filename);
 79:             }
 80:         } catch (RuntimeException $e)   {
 81:             if (empty($this->errorCallback))    {
 82:                 throw $e;
 83:             }
 84:             foreach ($this->callbackFromString($this->errorCallback) as $filename)  {
 85:                 $this->localInclude($filename);
 86:             }
 87:         }
 88:     }
 89:     
 90:     
 91:     /**
 92:      * Includuje soubor v uzavřeném kontextu.
 93:      * @param string $filename Cesta k souboru
 94:      */
 95:     protected function localInclude($filename)  {
 96:         if (!empty($this->context)) {
 97:             extract($this->context);
 98:         }
 99:         $router = $this;
100:         
101:         include $filename;
102:     }
103:     
104: }
105: 
PHP Router ver.1.3, r02 API documentation generated by ApiGen 2.6.1