Ejemplo n.º 1
0
 public function trigger($event, array $args = [], $method = null)
 {
     $eventName = $this->getEventName($event);
     $handlers = array_merge(isset($this->events[$eventName]) ? $this->events[$eventName]->getEventHandlers() : [], isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : []);
     $this->triggers[$eventName] = isset($this->triggers[$eventName]) ? $this->triggers[$eventName] + 1 : 1;
     if (!$handlers) {
         return null;
     }
     /**
      * Handlers are not chained anymore.
      * They are interrupted only if handler returns false.
      */
     foreach ($handlers as $handler) {
         if (is_string($handler)) {
             $handler = Reflect::create($handler, $args);
         }
         if (is_callable($handler)) {
             Reflect::call($handler, $args);
         } else {
             if (is_object($handler)) {
                 Reflect::method($handler, 'handle', $args);
             }
         }
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * @T00D00 - this needs to be refactored without nesting ...
  * @return null
  */
 public function runChains()
 {
     if (!$this->chains) {
         return null;
     }
     $next = $this->firstChain ?: function () {
         return $this;
     };
     foreach (array_reverse($this->chains) as $chain) {
         $next = function () use($chain, $next) {
             if (is_string($chain)) {
                 $chain = Reflect::create($chain);
             }
             if (is_callable($chain)) {
                 $result = $chain(array_merge($this->args, ['next' => $next]));
             } else {
                 $result = Reflect::method($chain, $this->runMethod, array_merge($this->args, ['next' => $next]));
             }
             return $result;
         };
     }
     //startMeasure('Chain: ' . $this->runMethod . '()');
     $result = $next();
     //stopMeasure('Chain: ' . $this->runMethod . '()');
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * @param $method
  * @param $args
  *
  * @return $this
  */
 public function __call($method, $args)
 {
     if (method_exists($this->getQuery(), $method)) {
         /**
          * First overload Query.
          */
         message(get_class($this) . '->__call(' . $method . ') on query ' . get_class($this->getQuery()));
         Reflect::method($this->getQuery(), $method, $args);
     } elseif (method_exists($this->getRightEntity(), $method)) {
         /**
          * Then right entity.
          */
         message(get_class($this) . '->__call(' . $method . ') on right entity ' . get_class($this->getRightEntity()));
         Reflect::method($this->getRightEntity(), $method, $args);
     } elseif (method_exists($this, 'getMiddleEntity') && method_exists($this->getMiddleEntity(), $method)) {
         /**
          * Then middle entity.
          */
         message(get_class($this) . '->__call(' . $method . ') on middle entity ' . get_class($this->getMiddleEntity()));
         Reflect::method($this->getMiddleEntity(), $method, $args);
     } else {
         message(get_class($this) . '->__call(' . $method . ') with right entity ' . get_class($this->getRightEntity()));
         $this->callWith($method, $args, $this->getRightEntity());
     }
     return $this;
 }
Ejemplo n.º 4
0
 /**
  * Register options
  */
 public function register()
 {
     if ($this->registered) {
         return $this;
     }
     $hadStack = context()->exists(Stack::class);
     if (!$hadStack) {
         context()->bind(Stack::class, new Stack());
     }
     $this->registerAutoloaders($this->autoload());
     $this->registerClassMaps($this->classMaps());
     $this->registerApps($this->apps());
     $this->registerProviders($this->providers());
     $this->registerRoutes($this->routes());
     $this->registerListeners($this->listeners());
     $this->registerMiddlewares($this->middlewares());
     $this->registerAfterwares($this->afterwares());
     $this->registerPaths($this->paths());
     $this->registerViewObjects($this->viewObjects());
     $this->registerConsoles($this->consoles());
     $this->registerAssets($this->assets());
     if (method_exists($this, 'registered')) {
         Reflect::method($this, 'registered');
     }
     $this->registered = true;
     /**
      * Some actions needs to be executed in reverse direction, for example config initialization.
      */
     if (!$hadStack) {
         $stack = context()->get(Stack::class);
         $stack->execute();
     }
     return $this;
 }
Ejemplo n.º 5
0
 /**
  * @return string
  * @throws Exception
  */
 public function getHtml()
 {
     if ($this->class && $this->method) {
         $prefix = strtolower(request()->getMethod());
         $args = array_merge($this->args, ['action' => $this]);
         $controller = Reflect::create($this->class, $args);
         $method = ($prefix ? $prefix . ucfirst($this->method) : $this->method) . 'Action';
         if (isset($args['settings'])) {
             /**
              * We need to resolve dependencies. ;-)
              */
             $args['settings']->each(function (Setting $setting) use(&$args) {
                 $setting->pivot->resolve($args);
             });
         }
         $result = null;
         $e = null;
         try {
             $result = Reflect::method($controller, $method, $args);
         } catch (Throwable $e) {
             if (prod()) {
                 return null;
             }
             throw $e;
         }
         if (is_array($result)) {
             return $result;
         } else {
             return '<!-- ' . $this->class . '::' . $method . ' start -->' . ($e ? 'Exception: ' . exception($e) : $result) . '<!-- ' . $this->class . '::' . $method . ' end -->';
         }
     }
 }
Ejemplo n.º 6
0
 /**
  * @param $method
  * @param $args
  *
  * @return $this|Relation
  * @throws Exception
  */
 public function callWith($method, $args, $object, $returnRelation = false)
 {
     if (is_string($object)) {
         $object = Reflect::create($object);
     }
     /**
      * Check if $method prefix is listed in autocallPrefixes.
      */
     foreach ($this->autocallPrefixes as $prefix) {
         if (substr($method, 0, strlen($prefix)) === $prefix && strtoupper(substr($method, strlen($prefix), 1)) == substr($method, strlen($prefix), 1)) {
             /**
              * We are calling relation function without arguments: $entity->something();.
              */
             $relationMethod = lcfirst(substr($method, strlen($prefix)));
             $relation = Reflect::method($object, $relationMethod, $args);
             // $relation = $object->{$relationMethod}();
             if (isset($args[0]) && ($args[0] instanceof Closure || is_callable($args[0]))) {
                 /**
                  * If callback was added, we run it.
                  * Now, this is a problem if we're making join because query was already merged.
                  * So, we'll call this magically and provide both - relation and query.
                  *
                  * @T00D00
                  */
                 if ($prefix == 'join') {
                     $rightEntity = $relation->getRightEntity();
                     $oldEntityQuery = $rightEntity->getQuery();
                     $rightEntity->setQuery($relation->getQuery());
                     Reflect::call($args[0], [$relation, $relation->getQuery()]);
                     $rightEntity->setQuery($oldEntityQuery);
                 } else {
                     Reflect::call($args[0], [$relation, $relation->getQuery()]);
                 }
             }
             /**
              * We'll call $entity->with($relation), and return Relation;
              */
             $return = $object->{$prefix}($relation);
             /**
              * Then we return relation.
              */
             return $returnRelation ? $relation : $return;
         }
     }
     if (!method_exists($object, $method)) {
         if (prod()) {
             return null;
         }
         throw new Exception('Method ' . $method . ' does not exist in ' . get_class($object));
     }
     /**
      * Autoprefixes failed, return relation, probably?
      */
     $relation = Reflect::method($object, $method, $args);
     if (isset($args[0]) && ($args[0] instanceof Closure || is_callable($args[0]))) {
         Reflect::call($args[0], [$relation, $relation->getQuery()]);
     }
     return $relation;
 }
Ejemplo n.º 7
0
 public function make($controller, $method, $params = [], $byRequest = false)
 {
     /**
      * Create controller.
      */
     $controller = Reflect::create($controller, $params);
     /**
      * Call action.
      */
     $view = Reflect::method($controller, (!$byRequest ? $method : (request()->isPost() ? 'post' : 'get') . ucfirst($method)) . 'Action', $params);
     return (string) $view;
 }
Ejemplo n.º 8
0
 public function execute()
 {
     $viewHttp = $this->request->isPost() ? 'post' . ucfirst($this->view) : 'get' . ucfirst($this->view);
     $result = null;
     $data = $this->getResolved();
     if (!method_exists($this->controller, $viewHttp . "Action")) {
         throw new Exception('Method ' . $viewHttp . 'Action() does not exist in ' . get_class($this->controller));
     }
     /**
      * Call main route action.
      */
     $result = Reflect::method($this->controller, $viewHttp . "Action", array_merge($this->data, $data));
     return $result;
 }
Ejemplo n.º 9
0
 /**
  * @return $this
  */
 public function initExtensions()
 {
     foreach (get_class_methods($this) as $method) {
         if (substr($method, 0, 4) == 'init' && substr($method, -9) == 'Extension') {
             $this->{$method}();
         } else {
             if (substr($method, 0, 6) == 'inject' && substr($method, -12) == 'Dependencies') {
                 Reflect::method($this, $method);
             }
         }
     }
     return $this;
 }
Ejemplo n.º 10
0
 public function init()
 {
     list($namespace, $method) = explode('::', $this->config);
     Reflect::method($namespace, $method);
 }
Ejemplo n.º 11
0
 public function execute(callable $next)
 {
     if (isConsole()) {
         return $next();
     }
     $router = router()->get();
     $routeName = $router['name'];
     foreach (config('pckg.auth.gates', []) as $gate) {
         $auth = auth($gate['provider']);
         /**
          * Check status rules.
          */
         if ($gate['status']) {
             if ($gate['status'] == 'logged-out' && $auth->isLoggedIn()) {
                 continue;
             } elseif ($gate['status'] == 'logged-in' && !$auth->isLoggedIn()) {
                 continue;
             }
         }
         /**
          * Check user group rules.
          */
         if ($gate['userGroup']) {
             if (!in_array($auth->getGroupId(), $gate['userGroup'])) {
                 continue;
             }
         }
         /**
          * Check if route is excluded in rule.
          */
         if (isset($gate['exclude'])) {
             if (in_array($routeName, $gate['exclude'])) {
                 continue;
             }
             foreach ($gate['exclude'] as $route) {
                 if (preg_match('#' . $route . '#', $routeName)) {
                     continue;
                 }
             }
         }
         /**
          * Check if route is included in rule.
          */
         if (isset($gate['include']) && !in_array($routeName, $gate['include'])) {
             $found = false;
             foreach ($gate['include'] as $route) {
                 if (preg_match('#' . $route . '#', $routeName)) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 continue;
             }
         }
         /**
          * Check for callback.
          */
         if (isset($gate['callback'])) {
             if (Reflect::method($gate['callback']['class'], $gate['callback']['method'])) {
                 continue;
             }
         }
         redirect(url($gate['redirect']));
     }
     return $next();
 }
Ejemplo n.º 12
0
 public function execute(InputInterface $input, OutputInterface $output)
 {
     Reflect::method($this, 'handle');
 }