Advertisement
Tori

RouterForIncludes (v.1.3)

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