Exemplo n.º 1
0
    def run_middleware(self, type_of_middleware):
        """Run route middleware.

        Arguments:
            type_of_middleware {string} -- Type of middleware to be ran (before|after)

        Raises:
            RouteMiddlewareNotFound -- Thrown when the middleware could not be found.
        """
        # Get the list of middleware to run for a route.
        for arg in self.list_middleware:
            if ':' in arg:
                middleware_to_run, arguments = arg.split(':')
                # Splits "name:value1,value2" into ['value1', 'value2']
                arguments = arguments.split(',')
            else:
                middleware_to_run = arg
                arguments = []

            middleware_to_run = self.request.app().make('RouteMiddleware')[middleware_to_run]
            if not isinstance(middleware_to_run, list):
                middleware_to_run = [middleware_to_run]

            try:
                for middleware in middleware_to_run:
                    located_middleware = self.request.app().resolve(middleware)
                    if hasattr(located_middleware, type_of_middleware):
                        getattr(located_middleware, type_of_middleware)(*arguments)
            except KeyError:
                raise RouteMiddlewareNotFound(
                    "Could not find the '{0}' route middleware".format(arg))
Exemplo n.º 2
0
    def run_middleware(self, type_of_middleware):
        """Run route middleware

        Arguments:
            type_of_middleware {string} -- Type of middleware to be ran (before|after)

        Raises:
            RouteMiddlewareNotFound -- Thrown when the middleware could not be found.
        """

        # Get the list of middleware to run for a route.
        for arg in self.list_middleware:
            middleware_to_run = self.request.app().make('RouteMiddleware')[arg]
            if isinstance(middleware_to_run, str):
                middleware_to_run = [middleware_to_run]

            # Locate the middleware based on the string specified
            try:
                for middleware in middleware_to_run:
                    located_middleware = self.request.app().resolve(
                        locate(middleware))

                    # If the middleware has the specific type of middleware
                    # (before or after) then execute that
                    if hasattr(located_middleware, type_of_middleware):
                        getattr(located_middleware, type_of_middleware)()

            except KeyError:
                raise RouteMiddlewareNotFound(
                    "Could not find the '{0}' route middleware".format(arg))
Exemplo n.º 3
0
    def run_middleware(self, type_of_middleware):
        """ type_of_middleware should be a string that contains either 'before' or 'after' """

        # Get the list of middleware to run for a route.
        for arg in self.list_middleware:

            # Locate the middleware based on the string specified
            try:
                located_middleware = self.request.app().resolve(
                    locate(middleware.ROUTE_MIDDLEWARE[arg]))
            except KeyError:
                raise RouteMiddlewareNotFound(
                    "Could not find the '{0}' route middleware".format(arg))

            # If the middleware has the specific type of middleware
            # (before or after) then execute that
            if hasattr(located_middleware, type_of_middleware):
                getattr(located_middleware, type_of_middleware)()