How to use the masonite.provider.ServiceProvider function in masonite

To help you get started, we’ve selected a few masonite examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MasoniteFramework / masonite / tests / core / test_service_provider.py View on Github external
from masonite.provider import ServiceProvider
from masonite.request import Request
from masonite.routes import Get
from masonite.testsuite.TestSuite import TestSuite, generate_wsgi


class ContainerTest(ServiceProvider):

    def boot(self, request: Request, get: Get):
        return request

    def testboot(self, request: Request, get: Get):
        return request


class ServiceProviderTest(ServiceProvider):

    def register(self):
        self.app.bind('Request', object)


class Mock1Command:
    pass


class Mock2Command:
    pass


ROUTE1 = Get().route('/url/here', None)
ROUTE2 = Get().route('/test/url', None)
github MasoniteFramework / masonite / tests / core / test_service_provider.py View on Github external
def setUp(self):
        self.app = TestSuite().create_container().container
        self.app.resolve_parameters = True
        self.provider = ServiceProvider()
        self.provider.load_app(self.app).register()
        self.load_provider = LoadProvider()
        self.load_provider.load_app(self.app).boot()
github MasoniteFramework / masonite / masonite / providers / RouteProvider.py View on Github external
"""A RouteProvider Service Provider."""

from masonite.helpers.routes import create_matchurl
from masonite.provider import ServiceProvider
from masonite.request import Request
from masonite.response import Response
from masonite.routes import Route


class RouteProvider(ServiceProvider):

    def register(self):
        pass

    def boot(self, router: Route, request: Request, response: Response):
        # All routes joined
        from config import application
        for route in self.app.make('WebRoutes'):

            """Make a better match for trailing slashes
            Sometimes a user will end with a trailing slash. Because the user might
            create routes like `/url/route` and `/url/route/` and how the regex
            is compiled down, we may need to adjust for urls that end or dont
            end with a trailing slash.
            """
github MasoniteFramework / masonite / app / providers / AppProvider.py View on Github external
''' A AppProvider Service Provider '''
from masonite.provider import ServiceProvider
from masonite.request import Request
from masonite.routes import Route
from config import storage
from routes import web, api

class AppProvider(ServiceProvider):

    wsgi = True

    def register(self):
        self.app.bind('WebRoutes', web.ROUTES)
        self.app.bind('ApiRoutes', api.ROUTES)
        self.app.bind('Response', None)
        self.app.bind('Storage', storage)

    def boot(self, Environ):
        self.app.bind('Request', Request(Environ))
        self.app.bind('Route', Route(Environ))
github MasoniteFramework / masonite / masonite / providers / StatusCodeProvider.py View on Github external
if application.DEBUG:
            return

        request = app.make('Request')

        request.status(500)
        if app.make('ViewClass').exists('errors/500'):
            rendered_view = app.make('View')('errors/500')
        else:
            rendered_view = app.make('View')(
                '/masonite/snippets/statuscode', {'code': '500 Internal Server Error'})

        request.app().make(Response).view(rendered_view)


class StatusCodeProvider(ServiceProvider):

    def register(self):
        self.app.bind('ServiceErrorExceptionHook', ServerErrorExceptionHook())

    def boot(self):
        request = self.app.make('Request')
        response = self.app.make(Response)
        if request.is_status(200):
            return

        if request.get_status() in (500, 405, 404):
            if request.header('Content-Type') == 'application/json':
                # Returns json response when we want the client to receive a json response
                json_response = {'error': {'status': request.get_status()}}
                response.view(json_response, status=request.get_status())
            else:
github MasoniteFramework / masonite / masonite / providers / SassProvider.py View on Github external
"""A SassProvider Service Provider."""

from masonite.provider import ServiceProvider
from masonite.storage import Storage


class SassProvider(ServiceProvider):

    wsgi = False

    def register(self):
        """Compile Sass.

        Compile Sass if the libsass module is installed. Once installed, all
        Sass files are compiled when the server is ran. This will only run
        once when the server is first started.
        """
        Storage().compile_sass()

    def boot(self):
        pass
github MasoniteFramework / masonite / masonite / providers / CacheProvider.py View on Github external
"""A Cache Service Provider."""

from masonite import Cache
from masonite.drivers import CacheDiskDriver, CacheRedisDriver
from masonite.managers import CacheManager
from masonite.provider import ServiceProvider


class CacheProvider(ServiceProvider):

    wsgi = False

    def register(self):
        from config import cache
        self.app.bind('CacheConfig', cache)
        self.app.bind('CacheDiskDriver', CacheDiskDriver)
        self.app.bind('CacheRedisDriver', CacheRedisDriver)
        self.app.bind('CacheManager', CacheManager(self.app))

    def boot(self, cache: CacheManager):
        self.app.bind('Cache', cache.driver(self.app.make('CacheConfig').DRIVER))
        self.app.swap(Cache, cache.driver(self.app.make('CacheConfig').DRIVER))
github MasoniteFramework / masonite / app / providers / MiddlewareProvider.py View on Github external
""" A MiddlewareProvider Service Provider """
from masonite.provider import ServiceProvider
from config import middleware

class MiddlewareProvider(ServiceProvider):
    """ Adds Middleware To The Service Container """

    wsgi = False

    def register(self):
        """ Register Middleware Into The Service Container """
        self.app.bind('HttpMiddleware', middleware.HTTP_MIDDLEWARE)

    def boot(self):
        pass
github MasoniteFramework / masonite / masonite / providers / ApiProvider.py View on Github external
''' A ApiProvider Service Provider '''
from masonite.provider import ServiceProvider

class ApiProvider(ServiceProvider):
    
    def register(self):
        pass

    def boot(self, Response, ApiRoutes, Route, Request):
        router = Route
        if Response == 'Route not found. Error 404':

            '''
            |--------------------------------------------------------------------------
            | Pull in the API Routes from the Service Container
            |--------------------------------------------------------------------------
            |
            | The Service Container has loaded all Api routes into the container so
            | let's loop through and check for any matches.
            |
github MasoniteFramework / masonite / masonite / providers / ViewProvider.py View on Github external
"""A View Service Provider."""

from masonite.provider import ServiceProvider
from masonite.view import View


class ViewProvider(ServiceProvider):

    wsgi = False

    def register(self):
        view = View(self.app)
        self.app.bind('ViewClass', view)
        self.app.bind('View', view.render)

    def boot(self):
        self.publishes_migrations([
            'storage/append_from.txt'
        ])