Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. <?php
  2.  
  3. class Middleware
  4. {
  5.     public static function check($middleware){
  6.         if($middleware===null) return true;
  7.  
  8.         if(is_array($middleware)){
  9.             foreach($middleware as $key){
  10.                 if(!Middleware::procedure($key)) return false;
  11.             }
  12.             return true;
  13.         }
  14.  
  15.         return Middleware::procedure($middleware) ? true : false;
  16.     }
  17.  
  18.     private static function procedure($key){
  19.         list($controller, $function, $args) = Middleware::decode($key);
  20.        
  21.         if(!class_exists($controller)) throw new BadMethodCallException('Bad middleware class call');
  22.         $middleware = new $controller();
  23.         if(!method_exists ($middleware, $function)) throw new BadMethodCallException('Bad middleware method call');
  24.         if($args!==null) return $middleware->$function($args);
  25.         return $middleware->$function();
  26.     }
  27.  
  28.     private static function decode($key){
  29.         $controller = ucfirst(strtolower(strstr($key, '@', true)))."Middleware";
  30.  
  31.         $function = strstr($key, '@');
  32.         $function = ltrim($function, '@');
  33.         $function = strstr($function, ':') ? strstr($function, ':', true) : $function;
  34.  
  35.         $args = null;
  36.         if(strstr($key, ':')){
  37.             $args = strstr($key, ':');
  38.             $args = ltrim($args, ':');
  39.         }
  40.  
  41.         return array($controller, $function, $args);
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement