Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Aktuální verzi, popis a příklady použití najdete na adrese: http://projekty.vize.name/router/
- */
- /**
- * Úprava routeru pro web skládaný pomocí include.
- *
- * @author Viktorie Halasů
- * @link http://projekty.vize.name/router/
- * @version 1.3
- * @package Router
- */
- class RouterForIncludes extends Router {
- /** @var array Kontext pro includované soubory */
- protected $context = array();
- /**
- * Přidá proměnnou do kontextu.
- * Je možné zadat buď asoc.pole jako jediný parametr, anebo (se dvěma param.) název + hodnotu.
- * @param mixed $name Asoc. pole, anebo jméno, pod kterým má být hodnota v include přístupná.
- * @param mixed $data Hodnota.
- * @return RouterForIncludes (fluent interface)
- *
- * @throws InvalidArgumentException Pokud jméno proměnné není řetězec
- */
- public function addToContext() {
- /* asoc. pole */
- if (func_num_args() === 1) {
- $data = func_get_arg(0);
- if (!is_array($data)) {
- throw new InvalidArgumentException("Router (include kontext): Pokud je volána jen s jedním argumentem, musí to být pole.");
- }
- foreach ($data as $key => $value) {
- $this->addToContext($key, $value);
- }
- /* jméno + hodnota */
- } elseif (func_num_args() === 2) {
- $name = func_get_arg(0);
- $value = func_get_arg(1);
- if (isset($this->context[$name])) {
- trigger_error("Router: Proměnná '$name' už v kontextu existuje.", E_USER_NOTICE);
- }
- if (!is_scalar($name)) {
- throw new InvalidArgumentException("Router (include kontext): Název proměnné musí být řetězec.");
- }
- $this->context[$name] = $value;
- }
- return $this;
- }
- /**
- * Vrátí pole se jmény includovaných souborů (a ověří, jestli existují)
- * @param string $str
- * @return array
- *
- * @throws RuntimeException Pokud soubor neexistuje
- */
- protected function callbackFromString($str) {
- $cb = array();
- foreach (explode(',', $str) as $file) {
- if (!file_exists($file)) {
- throw new RuntimeException("Router: Soubor '$file' neexistuje.");
- }
- $cb[] = $file;
- }
- return $cb;
- }
- /**
- * Předá řízení (includuje požadované soubory).
- */
- public function delegate() {
- try {
- foreach ($this->callbackFromString($this->callback) as $filename) {
- $this->localInclude($filename);
- }
- } catch (RuntimeException $e) {
- if (empty($this->errorCallback)) {
- throw $e;
- }
- foreach ($this->callbackFromString($this->errorCallback) as $filename) {
- $this->localInclude($filename);
- }
- }
- }
- /**
- * Includuje soubor v uzavřeném kontextu.
- * @param string $filename Cesta k souboru
- */
- protected function localInclude($filename) {
- if (!empty($this->context)) {
- extract($this->context);
- }
- $router = $this;
- unset($this);
- include $filename;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement