Swift 2.2获取所有实现协议的类

时间:2016-01-29 21:18:44

标签: swift swift2 open-source

我无法弄清楚如何获取实现协议的类,实例化它们并使用它们,而不了解它们。像泥一样清楚?

示例:

protocol ApiController : RequestHandler {
    var routePrefix: String { get set }
    var resources: [ApiResource] { get set }
}

extension ApiController {    
    func handleRequest(request: WebRequest, response: WebResponse) {

        var apiResourceResponse: ApiResourceResponse = ApiResourceResponse(withHttpStatus: HttpStatus.BadRequest, httpBody: "404: Not Found")

        for resource in resources {
            var foundAllParameters = true;
            if(resource.route != request.requestURI()) {
                for(param, _) in request.urlVariables {
                    if(!resource.route.contains("{\(param)}")) {
                        foundAllParameters = false;
                    }
                }
            }
            if(foundAllParameters) {
                let resourceFunc = resource.function as! (ApiRequest, ApiResponse) -> ApiResourceResponse
                apiResourceResponse = resourceFunc(ApiRequest(withWebRequest: request), ApiResponse(withWebResponse: response))
                break;
            }
        }

        response.setStatus(apiResourceResponse.httpStatus.rawValue, message: "\(apiResourceResponse.httpStatus)")
        response.appendBodyString(apiResourceResponse.httpBody);
        response.requestCompletedCallback();
    }
}

class ListingsController : ApiController {
    var routePrefix = "listings"
    var resources = [ApiResource]()

    init() {
        resources.append(ApiResource(withHttpVerb: "GET", function: getListings, route: "/"))
        resources.append(ApiResource(withHttpVerb: "GET", function: getListing, route: "/{id}"))
        resources.append(ApiResource(withHttpVerb: "GET", function: getListingBaz, route: "/{id}/baz/{bazid}"))
    }

    func getListings(withWebRequest request: ApiRequest, response: ApiResponse) -> ApiResourceResponse {
        return ApiResourceResponse(withHttpStatus: HttpStatus.Ok, httpBody: "Showing All Listings")
    }

    func getListing(withWebRequest request: ApiRequest, response: ApiResponse) -> ApiResourceResponse {
        return ApiResourceResponse(withHttpStatus: HttpStatus.Ok, httpBody: "Listings with Id: \(request.webRequest.urlVariables["id"]!)")
    }

    func getListingBaz(withWebRequest request: ApiRequest, response: ApiResponse) -> ApiResourceResponse {
        return ApiResourceResponse(withHttpStatus: HttpStatus.Ok, httpBody: "Listings with Id: \(request.webRequest.urlVariables["id"]!), BazId: \(request.webRequest.urlVariables["bazid"]!)")
    }
}

我想要做的是当应用程序加载时,调用以下函数,我必须注册我的路由。当我自己手动创建ListingsController,迭代路由并注册它们时,这非常有效:

public func PerfectServerModuleInit() {

    // Install the built-in routing handler.
    // Using this system is optional and you could install your own system if desired.
    Routing.Handler.registerGlobally()


    //register controllers, would be nice to have reflection here...
    let listingController = ListingsController()
    for resource in listingController.resources {
        Routing.Routes[resource.httpVerb, "\(listingController.routePrefix)\(resource.route)"] = { _ in return listingController}
    }
}

我想要做的是找到ListingsController,启动它的新实例,并基于ApiController协议使用它。在这样做时,我可以创建一个新的ApiController,除了实现协议之外不需要做任何事情,它会自动注册其路由,而不必我进入该功能并手动完成。我正在使用PerfectSwift Examples - URL Routing。我对此的处理可能不正确,我愿意接受建议。我不想做的是改变PerfectServer的魔力。我只是想扩展它并构建一个其他人可能使用的框架。

0 个答案:

没有答案