Guest User

Untitled

a guest
May 22nd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This file is part of the Arachne
  5.  *
  6.  * Copyright (c) Jáchym Toušek ([email protected])
  7.  *
  8.  * For the full copyright and license information, please view the file license.md that was distributed with this source code.
  9.  */
  10.  
  11. namespace Arachne\EntityLoader\Routing;
  12.  
  13. use Arachne\EntityLoader\Application\Envelope;
  14. use Closure;
  15. use Nette\Application\Routers\Route as BaseRoute;
  16. use Nette\Callback;
  17. use Nette\InvalidArgumentException;
  18.  
  19. /**
  20.  * @author Jáchym Toušek <[email protected]>
  21.  */
  22. class Route extends BaseRoute
  23. {
  24.  
  25.     /**
  26.      * {@inheritdoc}
  27.      */
  28.     public function __construct($mask, $metadata = [], $flags = 0)
  29.     {
  30.         // Copied from Nette\Application\Routers\Route::__construct()
  31.         if (is_string($metadata)) {
  32.             $pos = strrpos($metadata, ':');
  33.             if (!$pos) {
  34.                 throw new InvalidArgumentException("Second argument must be array or string in format Presenter:action, '$metadata' given.");
  35.             }
  36.             $metadata = [
  37.                 self::PRESENTER_KEY => substr($metadata, 0, $pos),
  38.                 'action' => $pos === strlen($metadata) - 1 ? null : substr($metadata, $pos + 1),
  39.             ];
  40.         } elseif ($metadata instanceof Closure || $metadata instanceof Callback) {
  41.             $metadata = [
  42.                 self::PRESENTER_KEY => 'Nette:Micro',
  43.                 'callback' => $metadata,
  44.             ];
  45.         }
  46.  
  47.         // Filter for handling Envelopes correctly
  48.         $filter = function (array $parameters) use ($metadata) {
  49.             foreach ($parameters as $key => & $value) {
  50.                 if ($value instanceof Envelope && !isset($metadata[$key][self::FILTER_OUT])) {
  51.                     $value = $value->getIdentifier();
  52.                 }
  53.             }
  54.             return $parameters;
  55.         };
  56.  
  57.         // Hack to invoke the filter after original global filter
  58.         if (isset($metadata[null][self::FILTER_OUT])) {
  59.             $original = $metadata[null][self::FILTER_OUT];
  60.             $metadata[null][self::FILTER_OUT] = function (array $parameters) use ($original, $filter) {
  61.                 $parameters = call_user_func($original, $parameters);
  62.                 if (!is_array($parameters)) {
  63.                     return $parameters;
  64.                 }
  65.                 return call_user_func($filter, $parameters);
  66.             };
  67.         } else {
  68.             $metadata[null][self::FILTER_OUT] = $filter;
  69.         }
  70.  
  71.         parent::__construct($mask, $metadata, $flags);
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment