Scalr\Api\Rest\ApiApplication::getRouteHandler PHP Метод

getRouteHandler() публичный Метод

Gets the callback for the handler of the specified Route
public getRouteHandler ( string $name ) : callable
$name string The description of the handler _:
Результат callable Returns the callable handler
    public function getRouteHandler($name)
    {
        $m = [];
        if (preg_match('/^(\\w+)_(\\w+):(\\w+)$/i', $name, $m)) {
            $version = (string) $this->settings[static::SETTING_API_VERSION];
            $controller = 'Scalr\\Api\\Service\\' . $m[1] . (empty($version) ? '' : '\\V' . $version) . '\\Controller\\' . $m[2];
            $method = $m[3];
        } else {
            throw new \InvalidArgumentException(sprintf("Invalid controller handler '%s'", $name));
        }
        return [$this->getContainer()->api->controller($controller), $method];
    }

Usage Example

Пример #1
0
 /**
  * @test
  */
 public function testGetHandler()
 {
     //Application instance is needed to setup container
     $app = new ApiApplication([ApiApplication::SETTING_API_VERSION => '1beta0']);
     $route = $this->getRouteFixture();
     $mdHandleApiVersion = [$app, 'handleApiVersion'];
     $route->addMiddleware($mdHandleApiVersion);
     $handler1 = function ($id) {
     };
     $handler2 = function ($id) {
         return 1;
     };
     $this->assertInternalType('callable', $route->getHandler());
     $route->addDefaults(['controller' => $handler1]);
     $this->assertSame($handler1, $route->getHandler());
     $route->addDefaults($handler2);
     $this->assertSame($handler2, $route->getHandler());
     //AbstractController based handler
     $route->setDefaults(['controller' => $app->getRouteHandler('Admin_Users:get')]);
     $this->assertInternalType('callable', $route->getHandler());
     $this->assertInstanceOf('Scalr\\Api\\Service\\Admin\\V1beta0\\Controller\\Users', $route->getHandler()[0]);
     $this->assertEquals('get', $route->getHandler()[1]);
 }