Skip to content

Commit

Permalink
apply format
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Apr 17, 2024
1 parent 57e69d3 commit 0e144f9
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 160 deletions.
34 changes: 16 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 69 additions & 69 deletions src/router.phel
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
(:use \Symfony\Component\Routing\Exception\ResourceNotFoundException))

(definterface Router
(match-by-path [this path] "Matches a route given a path. Returns nil if path doesn't match.")
(match-by-name [this route-name] "Matches a route given a route name. Returns nil if route can't be found.")
(generate [this route-name parameter] "Generate a url for a route"))
(match-by-path [this path] "Matches a route given a path. Returns nil if path doesn't match.")
(match-by-name [this route-name] "Matches a route given a route name. Returns nil if route can't be found.")
(generate [this route-name parameter] "Generate a url for a route"))

(defn- build-route-collection [normalized-routes]
(for [route :in normalized-routes
Expand All @@ -29,39 +29,39 @@
coll))

(defstruct SymfonyRouter [route-collection]
Router
(match-by-path [this path]
(try
(let [context (php/new RequestContext)
matcher (php/new UrlMatcher route-collection context)
parameters (php/-> matcher (match path))
params-map (for [[k v] :pairs parameters
:when (not (= k "_route"))
:reduce [acc {}]]
(put acc (keyword k) v))
route-name (php/aget parameters "_route")
route (php/-> route-collection (get route-name))]
{:template (php/-> route (getOption "template"))
:data (php/-> route (getOption "data"))
:path path
:path-params params-map})
(catch NoConfigurationException e nil)
(catch ResourceNotFoundException e nil)
(catch MethodNotAllowedException e nil)))
(match-by-name [this route-name]
(let [route (php/-> route-collection (get (full-name route-name)))]
(when route
{:template (php/-> route (getOption "template"))
:data (php/-> route (getOption "data"))})))
(generate [this route-name parameters]
(let [context (php/new RequestContext)
generator (php/new UrlGenerator route-collection context)
arr (->>
parameters
(map-indexed (fn [k v] [(name k) v]))
flatten
(apply php-associative-array))]
(php/-> generator (generate (full-name route-name) arr)))))
Router
(match-by-path [this path]
(try
(let [context (php/new RequestContext)
matcher (php/new UrlMatcher route-collection context)
parameters (php/-> matcher (match path))
params-map (for [[k v] :pairs parameters
:when (not (= k "_route"))
:reduce [acc {}]]
(put acc (keyword k) v))
route-name (php/aget parameters "_route")
route (php/-> route-collection (get route-name))]
{:template (php/-> route (getOption "template"))
:data (php/-> route (getOption "data"))
:path path
:path-params params-map})
(catch NoConfigurationException e nil)
(catch ResourceNotFoundException e nil)
(catch MethodNotAllowedException e nil)))
(match-by-name [this route-name]
(let [route (php/-> route-collection (get (full-name route-name)))]
(when route
{:template (php/-> route (getOption "template"))
:data (php/-> route (getOption "data"))})))
(generate [this route-name parameters]
(let [context (php/new RequestContext)
generator (php/new UrlGenerator route-collection context)
arr (->>
parameters
(map-indexed (fn [k v] [(name k) v]))
flatten
(apply php-associative-array))]
(php/-> generator (generate (full-name route-name) arr)))))

(defn router
"Builds a router"
Expand All @@ -73,39 +73,39 @@
(-> raw-routes (flatten/flatten-routes path data) build-route-collection))))

(defstruct CompiledSymfonyRouter [compiled-matcher-routes compiled-generator-routes indexed-routes]
Router
(match-by-path [this path]
(try
(let [context (php/new RequestContext)
matcher (php/new CompiledUrlMatcher compiled-matcher-routes context)
parameters (php/-> matcher (match path))
params-map (for [[k v] :pairs parameters
:when (not (= k "_route"))
:reduce [acc {}]]
(put acc (keyword k) v))
route-name (php/aget parameters "_route")
[template data] (get indexed-routes route-name)]
{:template template
:data data
:path path
:path-params params-map})
(catch NoConfigurationException e nil)
(catch ResourceNotFoundException e nil)
(catch MethodNotAllowedException e nil)))
(match-by-name [this route-name]
(let [route (get indexed-routes (full-name route-name))]
(when route
{:template (first route)
:data (second route)})))
(generate [this route-name parameters]
(let [context (php/new RequestContext)
generator (php/new CompiledUrlGenerator compiled-generator-routes context)
arr (->>
parameters
(map-indexed (fn [k v] [(name k) v]))
flatten
(apply php-associative-array))]
(php/-> generator (generate (full-name route-name) arr)))))
Router
(match-by-path [this path]
(try
(let [context (php/new RequestContext)
matcher (php/new CompiledUrlMatcher compiled-matcher-routes context)
parameters (php/-> matcher (match path))
params-map (for [[k v] :pairs parameters
:when (not (= k "_route"))
:reduce [acc {}]]
(put acc (keyword k) v))
route-name (php/aget parameters "_route")
[template data] (get indexed-routes route-name)]
{:template template
:data data
:path path
:path-params params-map})
(catch NoConfigurationException e nil)
(catch ResourceNotFoundException e nil)
(catch MethodNotAllowedException e nil)))
(match-by-name [this route-name]
(let [route (get indexed-routes (full-name route-name))]
(when route
{:template (first route)
:data (second route)})))
(generate [this route-name parameters]
(let [context (php/new RequestContext)
generator (php/new CompiledUrlGenerator compiled-generator-routes context)
arr (->>
parameters
(map-indexed (fn [k v] [(name k) v]))
flatten
(apply php-associative-array))]
(php/-> generator (generate (full-name route-name) arr)))))

(defmacro compiled-router
"Builds a compiled router"
Expand Down
6 changes: 3 additions & 3 deletions src/router/flatten.phel
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

(defn- walk-many [routes prefix-path prev-data]
(apply concat []
(for [r :in routes]
(walk-one r prefix-path prev-data))))
(for [r :in routes]
(walk-one r prefix-path prev-data))))

(defn- walk-one [routes prefix-path prev-data]
(if (vector? (first routes))
Expand All @@ -18,7 +18,7 @@
[maybe-arg (rest args)])
next-data (deep-merge prev-data data)
child-routes (walk-many (keep identity childs) (str prefix-path path) next-data)]
(if (empty? childs) [[(str prefix-path path) next-data]] child-routes)))))
(if (empty? childs) [[(str prefix-path path) next-data]] child-routes)))))

(defn flatten-routes
"Flattens nested routes to a vector of tuples [path data]."
Expand Down
Loading

0 comments on commit 0e144f9

Please sign in to comment.