/**
  * Implements the given action
  * 
  * @param  PhalconApp $app           Phalcon application object
  * @param  string  $actionClass   Class name for the action
  * @param  boolean $hasMiddleware True if the action class implements `MiddlewareInterface`
  */
 private function implementAction(PhalconApp $app, MicroCollection $collection, $actionClass, $hasMiddleware)
 {
     $method = $actionClass::getMethod();
     $path = $actionClass::getPath();
     $callback = function () use($app, $actionClass, $hasMiddleware) {
         $action = new $actionClass();
         if ($hasMiddleware) {
             foreach ($actionClass::middleware() as $middlewareClass) {
                 $middleware = new $middlewareClass();
                 if ($middleware instanceof MiddlewareInterface && !$middleware->call($app, $action)) {
                     return false;
                 }
             }
         }
         $args = func_get_args();
         return $action->run($app, $args);
     };
     if (!is_array($method)) {
         $method = [strtoupper($method)];
     }
     return $collection->map($path, $callback)->via($method);
 }
Example #2
0
File: App.php Project: ovide/phest
 /**
  * Adds a new resource to the app
  *
  * @param  string $controller The name of the controller class.
  */
 public function mountResource($controller)
 {
     if (!is_subclass_of($controller, Controller::class)) {
         throw new \RuntimeException($controller . ' is not a ' . Controller::class);
     }
     $rx = $controller::RX;
     $col = new Collection();
     $col->setHandler($controller, true);
     $col->setPrefix($controller::PATH);
     $col->map("[/]?{id:\$}", 'handle', $controller::PATH);
     $col->map("/{id:{$rx}}[/]?", 'handle', $controller::PATH);
     $this->mount($col);
 }