From 79a8ca43d1bceb0d3419f52c370acfe5b3033d2a Mon Sep 17 00:00:00 2001 From: anonymous Date: Sun, 28 Aug 2022 14:04:22 +0200 Subject: [PATCH] maps detected paths to openapi spec --- src/main/clj/gen/core.clj | 25 ++-------- src/main/clj/scan/lang/php/laravel5.clj | 84 ++++++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 42 deletions(-) diff --git a/src/main/clj/gen/core.clj b/src/main/clj/gen/core.clj index 698b1e6..6b9db6a 100644 --- a/src/main/clj/gen/core.clj +++ b/src/main/clj/gen/core.clj @@ -7,8 +7,7 @@ ^{} run (fn [args] - (scan.lang.php.laravel5/routes (first args)) - #_(println + (println (yaml/generate-string {"openapi" "3.0.0" :info @@ -17,31 +16,17 @@ :license {:name "MIT"}} :servers - [{:url "thing"}] + [ + {:url "thing"}] :paths - {"/pets" - {:get - {:parameters - [{:name "limit" - :in "query" - :description "aaaaaaaa" - :required false - :schema - {:type "integer" - :format "int32"}}]}} - "/lists" - {:get - {:responses - {"200" - {:description "ok"}}}}} - :spec {:lorem "ipsum" :thing true}} + (scan.lang.php.laravel5/routes (first args))} :dumper-options {:indent 2 :indicator-indent 1 :flow-style :block})))) (comment - ) +) (defn -main [& argv] (prn (run argv))) \ No newline at end of file diff --git a/src/main/clj/scan/lang/php/laravel5.clj b/src/main/clj/scan/lang/php/laravel5.clj index bf43eea..29afce8 100644 --- a/src/main/clj/scan/lang/php/laravel5.clj +++ b/src/main/clj/scan/lang/php/laravel5.clj @@ -4,6 +4,12 @@ (def ^{} + omit-comments + (fn [source] + (str/replace source #"\/\*.+\*\/" ""))) + +(def + ^{} load-method-signature! (fn [route] (cond (.exists (io/file (:ctl route))) @@ -21,27 +27,61 @@ (def ^{} + to-openapi + (fn [routes] + (reduce + (fn [acc el] + (assoc-in + acc + [(:path el) + (:verb el)] + {:description (:act el) + :parameters + (into + [] + (map + (fn [e] + (if (< 0 (count e)) + (let [[type name] (str/split e #" ")] + {:name (re-find #"\w" name) + :in "query" + :required true ;; depends on param optionality + :schema + {:type type}}) + [])) + (:signature el)))})) + {} + routes))) + +(def + ^{} routes (fn [project-path] - (map (fn [route] - (assoc route - :signature - (load-method-signature! route))) - (let [route-file (str project-path "/routes/web.php")] - (filter - (fn [e] - (not (nil? e))) - (map - (fn [hit] - (let [[_ verb raw-params] hit - params (map (fn [e] (str/trim (str/escape e {\' ""}))) (str/split raw-params #",")) - [route ctl] params] - (if ctl - {:verb verb - :path route - :ctl (str project-path "/app/Http/Controllers/" (str/replace (str/replace ctl #"\@(.*)" ".php") "\\" "/")) - :act (str/replace ctl #"(.*)\@" "")} - nil))) - (re-seq - #"router->(.*)\((.*)\)" - (slurp route-file)))))))) \ No newline at end of file + (to-openapi + (map (fn [route] + (assoc route + :signature + (load-method-signature! route))) + (let [route-file (str project-path "/routes/web.php")] + (filter + (fn [e] + (not (nil? e))) + (map + (fn [hit] + (let [[_ verb raw-params] hit + params (map (fn [e] (str/trim (str/escape e {\' ""}))) (str/split raw-params #",")) + [route ctl] params] + (if ctl + {:verb verb + :path route + :ctl (str project-path "/app/Http/Controllers/" (str/replace (str/replace ctl #"\@(.*)" ".php") "\\" "/")) + :act (str/replace ctl #"(.*)\@" "")} + nil))) + (re-seq + #"router->(.*)\((.*)\)" + (omit-comments (slurp route-file)))))))))) + +(comment + + + ) \ No newline at end of file