{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts  #-}
module Yesod.Core.Class.Dispatch where

import qualified Network.Wai as W
import Yesod.Core.Types
import Yesod.Core.Content (ToTypedContent (..))
import Yesod.Core.Handler (sendWaiApplication)
import Yesod.Core.Class.Yesod

-- | This class is automatically instantiated when you use the template haskell
-- mkYesod function. You should never need to deal with it directly.
class Yesod site => YesodDispatch site where
    yesodDispatch :: YesodRunnerEnv site -> W.Application

class YesodSubDispatch sub master where
    yesodSubDispatch :: YesodSubRunnerEnv sub master -> W.Application

instance YesodSubDispatch WaiSubsite master where
    yesodSubDispatch :: YesodSubRunnerEnv WaiSubsite master -> Application
yesodSubDispatch YesodSubRunnerEnv {YesodRunnerEnv master
master -> WaiSubsite
Route WaiSubsite -> Route master
ParentRunner master
ysreParentEnv :: forall sub parent.
YesodSubRunnerEnv sub parent -> YesodRunnerEnv parent
ysreToParentRoute :: forall sub parent.
YesodSubRunnerEnv sub parent -> Route sub -> Route parent
ysreGetSub :: forall sub parent. YesodSubRunnerEnv sub parent -> parent -> sub
ysreParentRunner :: forall sub parent.
YesodSubRunnerEnv sub parent -> ParentRunner parent
ysreParentEnv :: YesodRunnerEnv master
ysreToParentRoute :: Route WaiSubsite -> Route master
ysreGetSub :: master -> WaiSubsite
ysreParentRunner :: ParentRunner master
..} = Application
app
      where
        WaiSubsite Application
app = master -> WaiSubsite
ysreGetSub forall a b. (a -> b) -> a -> b
$ forall site. YesodRunnerEnv site -> site
yreSite YesodRunnerEnv master
ysreParentEnv

instance YesodSubDispatch WaiSubsiteWithAuth master where
  yesodSubDispatch :: YesodSubRunnerEnv WaiSubsiteWithAuth master -> Application
yesodSubDispatch YesodSubRunnerEnv {YesodRunnerEnv master
master -> WaiSubsiteWithAuth
Route WaiSubsiteWithAuth -> Route master
ParentRunner master
ysreParentEnv :: YesodRunnerEnv master
ysreToParentRoute :: Route WaiSubsiteWithAuth -> Route master
ysreGetSub :: master -> WaiSubsiteWithAuth
ysreParentRunner :: ParentRunner master
ysreParentEnv :: forall sub parent.
YesodSubRunnerEnv sub parent -> YesodRunnerEnv parent
ysreToParentRoute :: forall sub parent.
YesodSubRunnerEnv sub parent -> Route sub -> Route parent
ysreGetSub :: forall sub parent. YesodSubRunnerEnv sub parent -> parent -> sub
ysreParentRunner :: forall sub parent.
YesodSubRunnerEnv sub parent -> ParentRunner parent
..} Request
req =
      ParentRunner master
ysreParentRunner HandlerFor master TypedContent
handlert YesodRunnerEnv master
ysreParentEnv (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Route WaiSubsiteWithAuth -> Route master
ysreToParentRoute Maybe (Route WaiSubsiteWithAuth)
route) Request
req
    where
      route :: Maybe (Route WaiSubsiteWithAuth)
route = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ [Text] -> [(Text, Text)] -> Route WaiSubsiteWithAuth
WaiSubsiteWithAuthRoute (Request -> [Text]
W.pathInfo Request
req) []
      WaiSubsiteWithAuth Application
set = master -> WaiSubsiteWithAuth
ysreGetSub forall a b. (a -> b) -> a -> b
$ forall site. YesodRunnerEnv site -> site
yreSite forall a b. (a -> b) -> a -> b
$ YesodRunnerEnv master
ysreParentEnv
      handlert :: HandlerFor master TypedContent
handlert = forall (m :: * -> *) b. MonadHandler m => Application -> m b
sendWaiApplication Application
set

subHelper
  :: ToTypedContent content
  => SubHandlerFor child master content
  -> YesodSubRunnerEnv child master
  -> Maybe (Route child)
  -> W.Application
subHelper :: forall content child master.
ToTypedContent content =>
SubHandlerFor child master content
-> YesodSubRunnerEnv child master
-> Maybe (Route child)
-> Application
subHelper (SubHandlerFor HandlerData child master -> IO content
f) YesodSubRunnerEnv {YesodRunnerEnv master
master -> child
Route child -> Route master
ParentRunner master
ysreParentEnv :: YesodRunnerEnv master
ysreToParentRoute :: Route child -> Route master
ysreGetSub :: master -> child
ysreParentRunner :: ParentRunner master
ysreParentEnv :: forall sub parent.
YesodSubRunnerEnv sub parent -> YesodRunnerEnv parent
ysreToParentRoute :: forall sub parent.
YesodSubRunnerEnv sub parent -> Route sub -> Route parent
ysreGetSub :: forall sub parent. YesodSubRunnerEnv sub parent -> parent -> sub
ysreParentRunner :: forall sub parent.
YesodSubRunnerEnv sub parent -> ParentRunner parent
..} Maybe (Route child)
mroute =
    ParentRunner master
ysreParentRunner HandlerFor master TypedContent
handler YesodRunnerEnv master
ysreParentEnv (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Route child -> Route master
ysreToParentRoute Maybe (Route child)
mroute)
  where
    handler :: HandlerFor master TypedContent
handler = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. ToTypedContent a => a -> TypedContent
toTypedContent forall a b. (a -> b) -> a -> b
$ forall site a. (HandlerData site site -> IO a) -> HandlerFor site a
HandlerFor forall a b. (a -> b) -> a -> b
$ \HandlerData master master
hd ->
      let rhe :: RunHandlerEnv master master
rhe = forall child site.
HandlerData child site -> RunHandlerEnv child site
handlerEnv HandlerData master master
hd
          rhe' :: RunHandlerEnv child master
rhe' = RunHandlerEnv master master
rhe
            { rheRoute :: Maybe (Route child)
rheRoute = Maybe (Route child)
mroute
            , rheChild :: child
rheChild = master -> child
ysreGetSub forall a b. (a -> b) -> a -> b
$ forall site. YesodRunnerEnv site -> site
yreSite YesodRunnerEnv master
ysreParentEnv
            , rheRouteToMaster :: Route child -> Route master
rheRouteToMaster = Route child -> Route master
ysreToParentRoute
            }
       in HandlerData child master -> IO content
f HandlerData master master
hd { handlerEnv :: RunHandlerEnv child master
handlerEnv = RunHandlerEnv child master
rhe' }