@@ -11,7 +11,7 @@ defmodule Phoenix.Router.Route do
1111
1212 * `:verb` - the HTTP verb as an atom
1313 * `:line` - the line the route was defined
14- * `:kind` - the kind of route, one of `:match`, `:forward`
14+ * `:kind` - the kind of route, either `:match` or `:forward`
1515 * `:path` - the normalized path as string
1616 * `:hosts` - the list of request hosts or host prefixes
1717 * `:plug` - the plug module
@@ -25,9 +25,22 @@ defmodule Phoenix.Router.Route do
2525 * `:warn_on_verify?` - whether or not to warn on route verification
2626 """
2727
28- defstruct [ :verb , :line , :kind , :path , :hosts , :plug , :plug_opts ,
29- :helper , :private , :pipe_through , :assigns , :metadata ,
30- :trailing_slash? , :warn_on_verify? ]
28+ defstruct [
29+ :verb ,
30+ :line ,
31+ :kind ,
32+ :path ,
33+ :hosts ,
34+ :plug ,
35+ :plug_opts ,
36+ :helper ,
37+ :private ,
38+ :pipe_through ,
39+ :assigns ,
40+ :metadata ,
41+ :trailing_slash? ,
42+ :warn_on_verify?
43+ ]
3144
3245 @ type t :: % Route { }
3346
@@ -47,29 +60,71 @@ defmodule Phoenix.Router.Route do
4760 Receives the verb, path, plug, options and helper
4861 and returns a `Phoenix.Router.Route` struct.
4962 """
50- @ spec build ( non_neg_integer , :match | :forward , atom , String . t , String . t | nil , atom , atom , atom | nil , list ( atom ) , map , map , map , boolean , boolean ) :: t
51- def build ( line , kind , verb , path , hosts , plug , plug_opts , helper , pipe_through , private , assigns , metadata , trailing_slash? , warn_on_verify? )
63+ @ spec build (
64+ non_neg_integer ,
65+ :match | :forward ,
66+ atom ,
67+ String . t ( ) ,
68+ String . t ( ) | nil ,
69+ atom ,
70+ atom ,
71+ atom | nil ,
72+ list ( atom ) ,
73+ map ,
74+ map ,
75+ map ,
76+ boolean ,
77+ boolean
78+ ) :: t
79+ def build (
80+ line ,
81+ kind ,
82+ verb ,
83+ path ,
84+ hosts ,
85+ plug ,
86+ plug_opts ,
87+ helper ,
88+ pipe_through ,
89+ private ,
90+ assigns ,
91+ metadata ,
92+ trailing_slash? ,
93+ warn_on_verify?
94+ )
5295 when is_atom ( verb ) and is_list ( hosts ) and
53- is_atom ( plug ) and ( is_binary ( helper ) or is_nil ( helper ) ) and
54- is_list ( pipe_through ) and is_map ( private ) and is_map ( assigns ) and
55- is_map ( metadata ) and kind in [ :match , :forward ] and
56- is_boolean ( trailing_slash? ) do
57- % Route { kind: kind , verb: verb , path: path , hosts: hosts , private: private ,
58- plug: plug , plug_opts: plug_opts , helper: helper ,
59- pipe_through: pipe_through , assigns: assigns , line: line , metadata: metadata ,
60- trailing_slash?: trailing_slash? , warn_on_verify?: warn_on_verify? }
96+ is_atom ( plug ) and ( is_binary ( helper ) or is_nil ( helper ) ) and
97+ is_list ( pipe_through ) and is_map ( private ) and is_map ( assigns ) and
98+ is_map ( metadata ) and kind in [ :match , :forward ] and
99+ is_boolean ( trailing_slash? ) do
100+ % Route {
101+ kind: kind ,
102+ verb: verb ,
103+ path: path ,
104+ hosts: hosts ,
105+ private: private ,
106+ plug: plug ,
107+ plug_opts: plug_opts ,
108+ helper: helper ,
109+ pipe_through: pipe_through ,
110+ assigns: assigns ,
111+ line: line ,
112+ metadata: metadata ,
113+ trailing_slash?: trailing_slash? ,
114+ warn_on_verify?: warn_on_verify?
115+ }
61116 end
62117
63118 @ doc """
64119 Builds the compiled expressions used by the route.
65120 """
66- def exprs ( route , forwards ) do
121+ def exprs ( route ) do
67122 { path , binding } = build_path_and_binding ( route )
68123
69124 % {
70125 path: path ,
71126 binding: binding ,
72- dispatch: build_dispatch ( route , forwards ) ,
127+ dispatch: build_dispatch ( route ) ,
73128 hosts: build_host_match ( route . hosts ) ,
74129 path_params: build_path_params ( binding ) ,
75130 prepare: build_prepare ( route ) ,
@@ -78,6 +133,7 @@ defmodule Phoenix.Router.Route do
78133 end
79134
80135 def build_host_match ( [ ] ) , do: [ Plug.Router.Utils . build_host_match ( nil ) ]
136+
81137 def build_host_match ( [ _ | _ ] = hosts ) do
82138 for host <- hosts , do: Plug.Router.Utils . build_host_match ( host )
83139 end
@@ -91,7 +147,7 @@ defmodule Phoenix.Router.Route do
91147 { _params , segments } =
92148 case route . kind do
93149 :forward -> Plug.Router.Utils . build_path_match ( path <> "/*_forward_path_info" )
94- :match -> Plug.Router.Utils . build_path_match ( path )
150+ :match -> Plug.Router.Utils . build_path_match ( path )
95151 end
96152
97153 rewrite_segments ( segments )
@@ -101,7 +157,8 @@ defmodule Phoenix.Router.Route do
101157 defp rewrite_segments ( segments ) do
102158 { segments , { binding , _counter } } =
103159 Macro . prewalk ( segments , { [ ] , 0 } , fn
104- { name , _meta , nil } , { binding , counter } when is_atom ( name ) and name != :_forward_path_info ->
160+ { name , _meta , nil } , { binding , counter }
161+ when is_atom ( name ) and name != :_forward_path_info ->
105162 var = Macro . var ( :"arg#{ counter } " , __MODULE__ )
106163 { var , { [ { Atom . to_string ( name ) , var } | binding ] , counter + 1 } }
107164
@@ -140,27 +197,32 @@ defmodule Phoenix.Router.Route do
140197 { [ { key , var } ] , [ { key , merge } ] }
141198 end
142199
143- defp build_dispatch ( % Route { kind: :match , plug: plug , plug_opts: plug_opts } , _forwards ) do
200+ defp build_dispatch ( % Route { kind: :match , plug: plug , plug_opts: plug_opts } ) do
144201 quote do
145202 { unquote ( plug ) , unquote ( Macro . escape ( plug_opts ) ) }
146203 end
147204 end
148205
149- defp build_dispatch ( % Route { kind: :forward , plug: plug , plug_opts: plug_opts } , forwards ) do
150- segments = Map . fetch! ( forwards , plug )
151-
206+ defp build_dispatch ( % Route {
207+ kind: :forward ,
208+ plug: plug ,
209+ plug_opts: plug_opts ,
210+ metadata: metadata
211+ } ) do
152212 quote do
153213 {
154214 Phoenix.Router.Route ,
155- { unquote ( segments ) , unquote ( plug ) , unquote ( Macro . escape ( plug_opts ) ) }
215+ { unquote ( metadata . forward ) , unquote ( plug ) , unquote ( Macro . escape ( plug_opts ) ) }
156216 }
157217 end
158218 end
159219
160220 defp build_params ( ) do
161221 params = Macro . var ( :params , :conn )
162222 path_params = Macro . var ( :path_params , :conn )
163- merge_params = quote ( do: Phoenix.Router.Route . merge_params ( unquote ( params ) , unquote ( path_params ) ) )
223+
224+ merge_params =
225+ quote ( do: Phoenix.Router.Route . merge_params ( unquote ( params ) , unquote ( path_params ) ) )
164226
165227 {
166228 [ { :params , params } ] ,
0 commit comments