|
| 1 | +package laravel |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/doITmagic/rag-code-mcp/internal/logger" |
| 11 | +) |
| 12 | + |
| 13 | +// Compiled regex patterns for Blade directives |
| 14 | +var ( |
| 15 | + reExtends = regexp.MustCompile(`@extends\(\s*['"](.+?)['"]\s*\)`) |
| 16 | + reSection = regexp.MustCompile(`@section\(\s*['"](.+?)['"]\s*(?:,.*?)?\)`) |
| 17 | + reYield = regexp.MustCompile(`@yield\(\s*['"](.+?)['"]\s*\)`) |
| 18 | + reInclude = regexp.MustCompile(`@include\(\s*['"](.+?)['"]\s*\)`) |
| 19 | + reComponent = regexp.MustCompile(`@component\(\s*['"](.+?)['"]\s*\)`) |
| 20 | + reEach = regexp.MustCompile(`@each\(\s*['"](.+?)['"]\s*\)`) |
| 21 | + rePushStack = regexp.MustCompile(`@(?:push|stack)\(\s*['"](.+?)['"]\s*\)`) |
| 22 | + reProps = regexp.MustCompile(`@props\(\s*\[(.*?)\]\s*\)`) |
| 23 | +) |
| 24 | + |
| 25 | +// BladeAnalyzer parses Blade template files and extracts directives. |
| 26 | +type BladeAnalyzer struct{} |
| 27 | + |
| 28 | +// NewBladeAnalyzer creates a new BladeAnalyzer. |
| 29 | +func NewBladeAnalyzer() *BladeAnalyzer { |
| 30 | + return &BladeAnalyzer{} |
| 31 | +} |
| 32 | + |
| 33 | +// Analyze parses the given Blade template files, extracting directives. |
| 34 | +// Files that cannot be read are logged and skipped (no error returned). |
| 35 | +func (ba *BladeAnalyzer) Analyze(filePaths []string) []BladeTemplate { |
| 36 | + var templates []BladeTemplate |
| 37 | + |
| 38 | + for _, fp := range filePaths { |
| 39 | + tpl, err := ba.analyzeFile(fp) |
| 40 | + if err != nil { |
| 41 | + logger.Instance.Debug("[BLADE] skip %s: %v", filepath.Base(fp), err) |
| 42 | + continue |
| 43 | + } |
| 44 | + templates = append(templates, tpl) |
| 45 | + } |
| 46 | + |
| 47 | + return templates |
| 48 | +} |
| 49 | + |
| 50 | +// analyzeFile parses a single Blade file. |
| 51 | +func (ba *BladeAnalyzer) analyzeFile(filePath string) (BladeTemplate, error) { |
| 52 | + f, err := os.Open(filePath) |
| 53 | + if err != nil { |
| 54 | + return BladeTemplate{}, err |
| 55 | + } |
| 56 | + defer f.Close() |
| 57 | + |
| 58 | + tpl := BladeTemplate{ |
| 59 | + Name: bladeViewName(filePath), |
| 60 | + FilePath: filePath, |
| 61 | + } |
| 62 | + |
| 63 | + scanner := bufio.NewScanner(f) |
| 64 | + scanner.Buffer(make([]byte, 64*1024), 1024*1024) // Allow lines up to 1MB |
| 65 | + lineNum := 0 |
| 66 | + for scanner.Scan() { |
| 67 | + lineNum++ |
| 68 | + line := scanner.Text() |
| 69 | + |
| 70 | + // @extends |
| 71 | + if m := reExtends.FindStringSubmatch(line); len(m) > 1 { |
| 72 | + tpl.Extends = m[1] |
| 73 | + } |
| 74 | + |
| 75 | + // @section |
| 76 | + if m := reSection.FindStringSubmatch(line); len(m) > 1 { |
| 77 | + tpl.Sections = append(tpl.Sections, BladeSection{ |
| 78 | + Name: m[1], |
| 79 | + Type: "section", |
| 80 | + StartLine: lineNum, |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + // @yield |
| 85 | + if m := reYield.FindStringSubmatch(line); len(m) > 1 { |
| 86 | + tpl.Sections = append(tpl.Sections, BladeSection{ |
| 87 | + Name: m[1], |
| 88 | + Type: "yield", |
| 89 | + StartLine: lineNum, |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + // @include |
| 94 | + if m := reInclude.FindStringSubmatch(line); len(m) > 1 { |
| 95 | + tpl.Includes = append(tpl.Includes, BladeInclude{ |
| 96 | + ViewName: m[1], |
| 97 | + Type: "include", |
| 98 | + Line: lineNum, |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + // @component |
| 103 | + if m := reComponent.FindStringSubmatch(line); len(m) > 1 { |
| 104 | + tpl.Includes = append(tpl.Includes, BladeInclude{ |
| 105 | + ViewName: m[1], |
| 106 | + Type: "component", |
| 107 | + Line: lineNum, |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + // @each |
| 112 | + if m := reEach.FindStringSubmatch(line); len(m) > 1 { |
| 113 | + tpl.Includes = append(tpl.Includes, BladeInclude{ |
| 114 | + ViewName: m[1], |
| 115 | + Type: "each", |
| 116 | + Line: lineNum, |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + // @push / @stack |
| 121 | + if m := rePushStack.FindStringSubmatch(line); len(m) > 1 { |
| 122 | + tpl.Stacks = appendUnique(tpl.Stacks, m[1]) |
| 123 | + } |
| 124 | + |
| 125 | + // @props |
| 126 | + if m := reProps.FindStringSubmatch(line); len(m) > 1 { |
| 127 | + props := parsePropsArray(m[1]) |
| 128 | + tpl.Props = append(tpl.Props, props...) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + tpl.TotalLines = lineNum |
| 133 | + |
| 134 | + return tpl, scanner.Err() |
| 135 | +} |
| 136 | + |
| 137 | +// bladeViewName converts a file path to Laravel dot notation. |
| 138 | +// Example: /project/resources/views/layouts/app.blade.php → layouts.app |
| 139 | +func bladeViewName(filePath string) string { |
| 140 | + // Normalize to forward slashes |
| 141 | + fp := filepath.ToSlash(filePath) |
| 142 | + |
| 143 | + // Try to find resources/views/ in the path |
| 144 | + marker := "resources/views/" |
| 145 | + idx := strings.LastIndex(fp, marker) |
| 146 | + if idx >= 0 { |
| 147 | + relative := fp[idx+len(marker):] |
| 148 | + // Remove .blade.php extension |
| 149 | + relative = strings.TrimSuffix(relative, ".blade.php") |
| 150 | + return strings.ReplaceAll(relative, "/", ".") |
| 151 | + } |
| 152 | + |
| 153 | + // Fallback: use basename without extension |
| 154 | + base := filepath.Base(filePath) |
| 155 | + return strings.TrimSuffix(base, ".blade.php") |
| 156 | +} |
| 157 | + |
| 158 | +// parsePropsArray extracts prop names from a @props([...]) content string. |
| 159 | +// Input: "'title', 'color'" → Output: ["title", "color"] |
| 160 | +func parsePropsArray(raw string) []string { |
| 161 | + var props []string |
| 162 | + parts := strings.Split(raw, ",") |
| 163 | + for _, p := range parts { |
| 164 | + p = strings.TrimSpace(p) |
| 165 | + p = strings.Trim(p, "'\"") |
| 166 | + if p != "" { |
| 167 | + props = append(props, p) |
| 168 | + } |
| 169 | + } |
| 170 | + return props |
| 171 | +} |
| 172 | + |
| 173 | +// appendUnique appends s to slice only if not already present. |
| 174 | +func appendUnique(slice []string, s string) []string { |
| 175 | + for _, existing := range slice { |
| 176 | + if existing == s { |
| 177 | + return slice |
| 178 | + } |
| 179 | + } |
| 180 | + return append(slice, s) |
| 181 | +} |
0 commit comments