auryn-php 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- auryn/ingestors/php/__init__.py +0 -0
- auryn/ingestors/php/file_parser.py +349 -0
- auryn/ingestors/php/git_sync.py +134 -0
- auryn/ingestors/php/grammar.py +19 -0
- auryn/ingestors/php/ids.py +74 -0
- auryn/ingestors/php/plugin.py +36 -0
- auryn/ingestors/php/raw_model.py +133 -0
- auryn/ingestors/php/resolution.py +519 -0
- auryn/ingestors/php/schema.py +58 -0
- auryn_php-0.1.0.dist-info/METADATA +11 -0
- auryn_php-0.1.0.dist-info/RECORD +14 -0
- auryn_php-0.1.0.dist-info/WHEEL +4 -0
- auryn_php-0.1.0.dist-info/entry_points.txt +2 -0
- auryn_php-0.1.0.dist-info/licenses/LICENSE +557 -0
|
File without changes
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"""parse_file(path) -> RawFile: the parallelizable phase-1 unit (pure function, no shared state
|
|
2
|
+
-- safe under a ProcessPoolExecutor; only picklable raw_model dataclasses are returned, no
|
|
3
|
+
tree-sitter node crosses the boundary). parse_source(source, path_label) holds the logic and takes
|
|
4
|
+
raw bytes, so a git blob can be parsed without touching disk.
|
|
5
|
+
|
|
6
|
+
Every node type / field name below was confirmed empirically against tree-sitter-php 0.24.1
|
|
7
|
+
(module `tree_sitter_php`, grammar function `language_php()` -- see grammar.py; there is NO bare
|
|
8
|
+
`language()`). Verified via a grammar probe, not assumed:
|
|
9
|
+
|
|
10
|
+
- top level (`program`): `php_tag`; `namespace_definition` (field `name`=namespace_name, optional
|
|
11
|
+
field `body`=compound_statement for the `namespace X { ... }` block form); `namespace_use_declaration`
|
|
12
|
+
(`namespace_use_clause` children, each a `name`/`qualified_name` + optional field `alias`); and the
|
|
13
|
+
declarations `class_declaration` / `interface_declaration` / `trait_declaration` /
|
|
14
|
+
`enum_declaration` / `function_definition`.
|
|
15
|
+
- type heritage: `base_clause` (extends -- one class, or a list of interfaces for an interface) and
|
|
16
|
+
`class_interface_clause` (implements) hold `name`/`qualified_name` children; enums carry an
|
|
17
|
+
optional backing `primitive_type` before the body.
|
|
18
|
+
- modifiers are individual NAMED children of the declaration: `abstract_modifier`, `final_modifier`,
|
|
19
|
+
`static_modifier`, `readonly_modifier`, `visibility_modifier` (text = public/private/protected).
|
|
20
|
+
- body (`declaration_list`, or `enum_declaration_list` for enums): `method_declaration`
|
|
21
|
+
(field `name`, field `parameters`=formal_parameters, field `return_type`, field `body`),
|
|
22
|
+
`property_declaration` (field `type` + repeated `property_element`, each field `name`=variable_name),
|
|
23
|
+
`const_declaration` (`const_element` children), `use_declaration` (trait use -> `name`/`qualified_name`),
|
|
24
|
+
and `enum_case` (field `name`, optional field `value`).
|
|
25
|
+
- params (`formal_parameters`): `simple_parameter`, `variadic_parameter` (`Type ...$x`), and
|
|
26
|
+
`property_promotion_parameter` (field `visibility` -- also becomes a Property); each has field
|
|
27
|
+
`type` and field `name` (a `variable_name`, `$`-prefixed).
|
|
28
|
+
- calls: `function_call_expression` (field `function`), `member_call_expression` (field `object`,
|
|
29
|
+
field `name`), `scoped_call_expression` (field `scope`, field `name`), `object_creation_expression`
|
|
30
|
+
(`new` + a `name`/`qualified_name`). `variable_name` = `$` + `name`; `member_access_expression`
|
|
31
|
+
has field `object` + field `name`; `relative_scope` = self/static/parent.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
from __future__ import annotations
|
|
35
|
+
|
|
36
|
+
from pathlib import Path
|
|
37
|
+
|
|
38
|
+
from auryn.core.docstrings import leading_comment
|
|
39
|
+
from auryn.ingestors.php.grammar import new_parser
|
|
40
|
+
from auryn.ingestors.php.raw_model import (
|
|
41
|
+
RawCall,
|
|
42
|
+
RawConstant,
|
|
43
|
+
RawEnumCase,
|
|
44
|
+
RawFile,
|
|
45
|
+
RawFunction,
|
|
46
|
+
RawMethod,
|
|
47
|
+
RawParameter,
|
|
48
|
+
RawProperty,
|
|
49
|
+
RawType,
|
|
50
|
+
RawUse,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
_TYPE_DECL_KINDS = {
|
|
54
|
+
"class_declaration": "Class",
|
|
55
|
+
"interface_declaration": "Interface",
|
|
56
|
+
"trait_declaration": "Trait",
|
|
57
|
+
"enum_declaration": "Enum",
|
|
58
|
+
}
|
|
59
|
+
_NAME_NODES = ("name", "qualified_name")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def parse_file(path: str) -> RawFile:
|
|
63
|
+
return parse_source(Path(path).read_bytes(), path)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def parse_source(source: bytes, path_label: str) -> RawFile:
|
|
67
|
+
tree = new_parser().parse(source)
|
|
68
|
+
rf = RawFile(path=path_label)
|
|
69
|
+
|
|
70
|
+
def txt(node) -> str:
|
|
71
|
+
return source[node.start_byte : node.end_byte].decode("utf-8", "replace") if node else ""
|
|
72
|
+
|
|
73
|
+
def lines(node) -> tuple[int, int]:
|
|
74
|
+
return node.start_point[0] + 1, node.end_point[0] + 1
|
|
75
|
+
|
|
76
|
+
def field(node, name):
|
|
77
|
+
return node.child_by_field_name(name) if node is not None else None
|
|
78
|
+
|
|
79
|
+
def type_text(node) -> str:
|
|
80
|
+
return " ".join(txt(node).split()) if node is not None else ""
|
|
81
|
+
|
|
82
|
+
def simple_name(node) -> str | None:
|
|
83
|
+
"""The bare simple name a name/type node denotes: `Base` for `Base`, `App\\Base` for a
|
|
84
|
+
`qualified_name` (last segment), the inner name for a `named_type`/`optional_type`,
|
|
85
|
+
self/static/parent for a `relative_scope`. None if it names nothing."""
|
|
86
|
+
if node is None:
|
|
87
|
+
return None
|
|
88
|
+
if node.type == "name":
|
|
89
|
+
return txt(node)
|
|
90
|
+
if node.type == "qualified_name":
|
|
91
|
+
last = None
|
|
92
|
+
for c in node.children:
|
|
93
|
+
if c.type == "name":
|
|
94
|
+
last = c
|
|
95
|
+
return txt(last) if last is not None else None
|
|
96
|
+
if node.type == "relative_scope": # self / static / parent
|
|
97
|
+
return txt(node).strip()
|
|
98
|
+
if node.type in ("named_type", "optional_type"):
|
|
99
|
+
for c in node.children:
|
|
100
|
+
if c.type in _NAME_NODES:
|
|
101
|
+
return simple_name(c)
|
|
102
|
+
# descend to the first name we can find
|
|
103
|
+
stack = list(node.children)
|
|
104
|
+
while stack:
|
|
105
|
+
n = stack.pop(0)
|
|
106
|
+
if n.type in _NAME_NODES:
|
|
107
|
+
return simple_name(n)
|
|
108
|
+
stack.extend(n.children)
|
|
109
|
+
return None
|
|
110
|
+
|
|
111
|
+
def var_name(variable_name_node) -> str:
|
|
112
|
+
"""A `variable_name` is `$` + `name`; return the identifier without the `$`."""
|
|
113
|
+
if variable_name_node is None:
|
|
114
|
+
return ""
|
|
115
|
+
return txt(variable_name_node).lstrip("$")
|
|
116
|
+
|
|
117
|
+
def visibility_of(node, default: str = "public") -> str:
|
|
118
|
+
for c in node.children:
|
|
119
|
+
if c.type == "visibility_modifier":
|
|
120
|
+
return txt(c).strip()
|
|
121
|
+
return default
|
|
122
|
+
|
|
123
|
+
def has_mod(node, modifier_type: str) -> bool:
|
|
124
|
+
return any(c.type == modifier_type for c in node.children)
|
|
125
|
+
|
|
126
|
+
# -- names lists (extends / implements / trait use) ------------------------------------
|
|
127
|
+
|
|
128
|
+
def name_list(node) -> list[str]:
|
|
129
|
+
if node is None:
|
|
130
|
+
return []
|
|
131
|
+
out = []
|
|
132
|
+
for c in node.children:
|
|
133
|
+
if c.type in _NAME_NODES:
|
|
134
|
+
nm = simple_name(c)
|
|
135
|
+
if nm:
|
|
136
|
+
out.append(nm)
|
|
137
|
+
return out
|
|
138
|
+
|
|
139
|
+
# -- parameters ------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
def parameters(formal_parameters_node) -> list[RawParameter]:
|
|
142
|
+
if formal_parameters_node is None:
|
|
143
|
+
return []
|
|
144
|
+
out: list[RawParameter] = []
|
|
145
|
+
pos = 0
|
|
146
|
+
for c in formal_parameters_node.children:
|
|
147
|
+
if c.type == "simple_parameter":
|
|
148
|
+
out.append(RawParameter(name=var_name(field(c, "name")), type=type_text(field(c, "type")), position=pos))
|
|
149
|
+
pos += 1
|
|
150
|
+
elif c.type == "variadic_parameter":
|
|
151
|
+
out.append(RawParameter(name=var_name(field(c, "name")), type=type_text(field(c, "type")), position=pos, is_variadic=True))
|
|
152
|
+
pos += 1
|
|
153
|
+
elif c.type == "property_promotion_parameter":
|
|
154
|
+
out.append(RawParameter(
|
|
155
|
+
name=var_name(field(c, "name")), type=type_text(field(c, "type")), position=pos,
|
|
156
|
+
is_promoted=True, visibility=(txt(field(c, "visibility")).strip() or "public"),
|
|
157
|
+
))
|
|
158
|
+
pos += 1
|
|
159
|
+
return out
|
|
160
|
+
|
|
161
|
+
# -- calls -----------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
def call_receiver(obj) -> str | None:
|
|
164
|
+
"""A simple name for the call's receiver, or None if too complex to name."""
|
|
165
|
+
if obj is None:
|
|
166
|
+
return None
|
|
167
|
+
if obj.type == "variable_name":
|
|
168
|
+
return var_name(obj) # "this", "store", ...
|
|
169
|
+
if obj.type == "member_access_expression": # $this->store -> the accessed member "store"
|
|
170
|
+
nm = field(obj, "name")
|
|
171
|
+
return txt(nm) if nm is not None and nm.type == "name" else None
|
|
172
|
+
return None
|
|
173
|
+
|
|
174
|
+
def collect_calls(body) -> list[RawCall]:
|
|
175
|
+
if body is None:
|
|
176
|
+
return []
|
|
177
|
+
out: list[RawCall] = []
|
|
178
|
+
seen: set = set()
|
|
179
|
+
|
|
180
|
+
def add(name, receiver, kind):
|
|
181
|
+
key = (name, receiver, kind)
|
|
182
|
+
if name and key not in seen:
|
|
183
|
+
seen.add(key)
|
|
184
|
+
out.append(RawCall(name=name, receiver=receiver, kind=kind))
|
|
185
|
+
|
|
186
|
+
stack = [body]
|
|
187
|
+
while stack:
|
|
188
|
+
n = stack.pop()
|
|
189
|
+
if n.type == "function_call_expression":
|
|
190
|
+
add(simple_name(field(n, "function")), None, "function")
|
|
191
|
+
elif n.type == "member_call_expression":
|
|
192
|
+
nm = field(n, "name")
|
|
193
|
+
if nm is not None and nm.type == "name":
|
|
194
|
+
add(txt(nm), call_receiver(field(n, "object")), "method")
|
|
195
|
+
elif n.type == "scoped_call_expression":
|
|
196
|
+
nm = field(n, "name")
|
|
197
|
+
if nm is not None and nm.type == "name":
|
|
198
|
+
add(txt(nm), simple_name(field(n, "scope")), "static")
|
|
199
|
+
elif n.type == "object_creation_expression":
|
|
200
|
+
nm = next((c for c in n.children if c.type in _NAME_NODES), None)
|
|
201
|
+
if nm is not None:
|
|
202
|
+
add(simple_name(nm), None, "new")
|
|
203
|
+
stack.extend(n.children)
|
|
204
|
+
return out
|
|
205
|
+
|
|
206
|
+
# -- members ---------------------------------------------------------------------------
|
|
207
|
+
|
|
208
|
+
def build_method(node) -> RawMethod:
|
|
209
|
+
name = txt(field(node, "name"))
|
|
210
|
+
kind = "CONSTRUCTOR" if name.lower() == "__construct" else "ORDINARY"
|
|
211
|
+
body = field(node, "body")
|
|
212
|
+
ls, le = lines(node)
|
|
213
|
+
return RawMethod(
|
|
214
|
+
name=name,
|
|
215
|
+
kind=kind,
|
|
216
|
+
visibility=visibility_of(node),
|
|
217
|
+
is_static=has_mod(node, "static_modifier"),
|
|
218
|
+
is_abstract=has_mod(node, "abstract_modifier"),
|
|
219
|
+
is_final=has_mod(node, "final_modifier"),
|
|
220
|
+
return_type=type_text(field(node, "return_type")) or None if kind == "ORDINARY" else None,
|
|
221
|
+
parameters=parameters(field(node, "parameters")),
|
|
222
|
+
calls=collect_calls(body),
|
|
223
|
+
source=txt(node),
|
|
224
|
+
doc=leading_comment(node, source),
|
|
225
|
+
line_start=ls, line_end=le,
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
def build_properties(node) -> list[RawProperty]:
|
|
229
|
+
vis = visibility_of(node)
|
|
230
|
+
is_static = has_mod(node, "static_modifier")
|
|
231
|
+
is_readonly = has_mod(node, "readonly_modifier")
|
|
232
|
+
type_txt = type_text(field(node, "type"))
|
|
233
|
+
ls, le = lines(node)
|
|
234
|
+
out = []
|
|
235
|
+
for c in node.children:
|
|
236
|
+
if c.type == "property_element":
|
|
237
|
+
out.append(RawProperty(
|
|
238
|
+
name=var_name(field(c, "name")), type=type_txt, visibility=vis,
|
|
239
|
+
is_static=is_static, is_readonly=is_readonly, line_start=ls, line_end=le,
|
|
240
|
+
))
|
|
241
|
+
return out
|
|
242
|
+
|
|
243
|
+
def build_constants(node) -> list[RawConstant]:
|
|
244
|
+
vis = visibility_of(node)
|
|
245
|
+
ls, le = lines(node)
|
|
246
|
+
out = []
|
|
247
|
+
for c in node.children:
|
|
248
|
+
if c.type == "const_element":
|
|
249
|
+
name_node = next((g for g in c.children if g.type == "name"), None)
|
|
250
|
+
value_node = next((g for g in c.named_children if g.type != "name"), None)
|
|
251
|
+
out.append(RawConstant(
|
|
252
|
+
name=txt(name_node), visibility=vis, value=type_text(value_node),
|
|
253
|
+
line_start=ls, line_end=le,
|
|
254
|
+
))
|
|
255
|
+
return out
|
|
256
|
+
|
|
257
|
+
def enum_cases(enum_body) -> list[RawEnumCase]:
|
|
258
|
+
out = []
|
|
259
|
+
for c in enum_body.children:
|
|
260
|
+
if c.type == "enum_case":
|
|
261
|
+
out.append(RawEnumCase(name=txt(field(c, "name")), value=type_text(field(c, "value"))))
|
|
262
|
+
return out
|
|
263
|
+
|
|
264
|
+
def dispatch_member(member, rt: RawType) -> None:
|
|
265
|
+
if member.type == "method_declaration":
|
|
266
|
+
rt.methods.append(build_method(member))
|
|
267
|
+
elif member.type == "property_declaration":
|
|
268
|
+
rt.properties.extend(build_properties(member))
|
|
269
|
+
elif member.type == "const_declaration":
|
|
270
|
+
rt.constants.extend(build_constants(member))
|
|
271
|
+
elif member.type == "use_declaration": # `use TraitName;`
|
|
272
|
+
rt.traits.extend(name_list(member))
|
|
273
|
+
|
|
274
|
+
def build_type(node, namespace: str) -> RawType:
|
|
275
|
+
kind = _TYPE_DECL_KINDS[node.type]
|
|
276
|
+
ls, le = lines(node)
|
|
277
|
+
rt = RawType(
|
|
278
|
+
kind=kind, name=txt(field(node, "name")), namespace=namespace,
|
|
279
|
+
is_abstract=has_mod(node, "abstract_modifier"), is_final=has_mod(node, "final_modifier"),
|
|
280
|
+
source=txt(node), doc=leading_comment(node, source), line_start=ls, line_end=le,
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
base = next((c for c in node.children if c.type == "base_clause"), None)
|
|
284
|
+
impl = next((c for c in node.children if c.type == "class_interface_clause"), None)
|
|
285
|
+
if kind == "Class":
|
|
286
|
+
bases = name_list(base)
|
|
287
|
+
rt.superclass = bases[0] if bases else None
|
|
288
|
+
rt.interfaces = name_list(impl)
|
|
289
|
+
elif kind == "Interface":
|
|
290
|
+
rt.interfaces = name_list(base) # an interface's base_clause lists super-interfaces
|
|
291
|
+
elif kind == "Enum":
|
|
292
|
+
rt.interfaces = name_list(impl)
|
|
293
|
+
rt.backing_type = next((txt(c) for c in node.children if c.type == "primitive_type"), None)
|
|
294
|
+
|
|
295
|
+
body = field(node, "body")
|
|
296
|
+
if body is not None:
|
|
297
|
+
for member in body.children:
|
|
298
|
+
dispatch_member(member, rt)
|
|
299
|
+
if body.type == "enum_declaration_list":
|
|
300
|
+
rt.enum_cases = enum_cases(body)
|
|
301
|
+
|
|
302
|
+
# constructor property promotion -> Property members (also kept as params on the ctor).
|
|
303
|
+
for m in rt.methods:
|
|
304
|
+
if m.kind == "CONSTRUCTOR":
|
|
305
|
+
for p in m.parameters:
|
|
306
|
+
if p.is_promoted:
|
|
307
|
+
rt.properties.append(RawProperty(
|
|
308
|
+
name=p.name, type=p.type, visibility=p.visibility or "public",
|
|
309
|
+
is_promoted=True, line_start=m.line_start, line_end=m.line_start,
|
|
310
|
+
))
|
|
311
|
+
return rt
|
|
312
|
+
|
|
313
|
+
def build_function(node) -> RawFunction:
|
|
314
|
+
ls, le = lines(node)
|
|
315
|
+
return RawFunction(
|
|
316
|
+
name=txt(field(node, "name")),
|
|
317
|
+
return_type=type_text(field(node, "return_type")) or None,
|
|
318
|
+
parameters=parameters(field(node, "parameters")),
|
|
319
|
+
calls=collect_calls(field(node, "body")),
|
|
320
|
+
source=txt(node), doc=leading_comment(node, source), line_start=ls, line_end=le,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
# -- top-level walk (tracks the current namespace) -------------------------------------
|
|
324
|
+
|
|
325
|
+
def walk_top(nodes, current_ns: str) -> None:
|
|
326
|
+
for child in nodes:
|
|
327
|
+
if child.type == "namespace_definition":
|
|
328
|
+
ns = txt(field(child, "name"))
|
|
329
|
+
body = field(child, "body") # `namespace X { ... }` block form
|
|
330
|
+
if body is not None:
|
|
331
|
+
walk_top(body.children, ns)
|
|
332
|
+
else: # `namespace X;` -- applies to the following siblings
|
|
333
|
+
current_ns = ns
|
|
334
|
+
if not rf.namespace:
|
|
335
|
+
rf.namespace = ns
|
|
336
|
+
elif child.type == "namespace_use_declaration":
|
|
337
|
+
for clause in child.children:
|
|
338
|
+
if clause.type == "namespace_use_clause":
|
|
339
|
+
target = next((c for c in clause.children if c.type in _NAME_NODES), None)
|
|
340
|
+
alias = field(clause, "alias")
|
|
341
|
+
if target is not None:
|
|
342
|
+
rf.uses.append(RawUse(name=txt(target), alias=txt(alias) if alias is not None else None))
|
|
343
|
+
elif child.type in _TYPE_DECL_KINDS:
|
|
344
|
+
rf.types.append(build_type(child, current_ns))
|
|
345
|
+
elif child.type == "function_definition":
|
|
346
|
+
rf.functions.append(build_function(child))
|
|
347
|
+
|
|
348
|
+
walk_top(tree.root_node.children, "")
|
|
349
|
+
return rf
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Git-tracked (and directory) PHP ingestion: moves a project's PHP subgraph to a target
|
|
2
|
+
commit/tree by fully recomputing the target state and diffing it against the graph
|
|
3
|
+
(graph_sync.compute_delta) -- the same recompute-and-delta model as the Java/C/C#/Python
|
|
4
|
+
ingestors (resolution is global -- the project-wide type/method/function index -- so a file-level
|
|
5
|
+
diff can't tell which untouched files a change affects).
|
|
6
|
+
|
|
7
|
+
Fast engine only for now: tree-sitter-php streams each `.php` blob (no disk materialization,
|
|
8
|
+
no autoloader/composer resolution) -> RawFile -> resolution.resolve(). A deep pass (full PHP
|
|
9
|
+
name-resolution: use-statement aliasing, PSR-4 autoloading, trait-flattened and inherited-member
|
|
10
|
+
call targets) would be added as the GitLanguage's `deep_sync` (see plugin.py). Registered as the
|
|
11
|
+
`php` GitLanguage for the multi-language endpoint (core/ingest.py) in both ingest_repository (git)
|
|
12
|
+
and ingest_directory (local)."""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import itertools
|
|
17
|
+
import logging
|
|
18
|
+
import time
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
|
|
21
|
+
from auryn.core.parallel import parse_blobs_ordered
|
|
22
|
+
from auryn.core.graph_model import GraphNode
|
|
23
|
+
from auryn.core.graph_sync import stream_apply
|
|
24
|
+
from auryn.core.registry import LanguageIngestResult
|
|
25
|
+
from auryn.ingestors.php import resolution
|
|
26
|
+
from auryn.ingestors.php.file_parser import parse_source
|
|
27
|
+
from auryn.ingestors.php.ids import project_id
|
|
28
|
+
from auryn.ingestors.php.plugin import EXCLUDED_DIR_NAMES as _EXCLUDED_DIR_NAMES
|
|
29
|
+
from auryn.ingestors.php.plugin import FILE_SUFFIXES as _FILE_SUFFIXES
|
|
30
|
+
from auryn.ingestors.php.schema import NODE_SCHEMAS
|
|
31
|
+
|
|
32
|
+
logger = logging.getLogger(__name__)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class SyncStats:
|
|
37
|
+
nodes_added: int = 0
|
|
38
|
+
nodes_updated: int = 0
|
|
39
|
+
nodes_removed: int = 0
|
|
40
|
+
edges_added: int = 0
|
|
41
|
+
edges_updated: int = 0
|
|
42
|
+
edges_removed: int = 0
|
|
43
|
+
commit_sha: str = ""
|
|
44
|
+
elapsed_seconds: float = 0.0
|
|
45
|
+
files_parsed: int = 0
|
|
46
|
+
files_cached: int = 0
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def sync_at(writer, source, target_sha: str, project_name: str, repo_uri: str, ref: str | None) -> SyncStats:
|
|
50
|
+
"""Recompute the whole PHP subgraph at `target_sha` and delta it onto the graph. `source`
|
|
51
|
+
is duck-typed (GitRepoCache or LocalDirectorySource): only list_files/read_blob used."""
|
|
52
|
+
started = time.monotonic()
|
|
53
|
+
proj_id = project_id(project_name)
|
|
54
|
+
|
|
55
|
+
writer.setup_schema(NODE_SCHEMAS) # brings the graph key into existence before the first read
|
|
56
|
+
if writer.get_property("Project", {"id": proj_id}, "commit_sha") == target_sha:
|
|
57
|
+
return SyncStats(commit_sha=target_sha, elapsed_seconds=time.monotonic() - started)
|
|
58
|
+
|
|
59
|
+
entries = [
|
|
60
|
+
(path, blob_sha)
|
|
61
|
+
for suffix in _FILE_SUFFIXES
|
|
62
|
+
for path, blob_sha in source.list_files(target_sha, suffix, set(_EXCLUDED_DIR_NAMES))
|
|
63
|
+
]
|
|
64
|
+
files = parse_blobs_ordered(
|
|
65
|
+
entries, source.read_blob, parse_source,
|
|
66
|
+
preload=["auryn.ingestors.php.file_parser"], logger=logger,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
project_node = GraphNode(
|
|
70
|
+
"Project", {"id": proj_id}, {"name": project_name, "repo_uri": repo_uri, "commit_sha": target_sha, "ref": ref}
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
def make_target(): # re-iterable: stream_apply runs it once for nodes, once for edges
|
|
74
|
+
return itertools.chain([project_node], resolution.resolve(files, proj_id))
|
|
75
|
+
|
|
76
|
+
return _apply(writer, proj_id, make_target, target_sha, started, files_parsed=len(files))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _apply(writer, proj_id: str, make_target, target_sha: str, started: float, files_parsed: int) -> SyncStats:
|
|
80
|
+
# Cross-repo reconciliation (ADR 0004): fold placeholder<->definer pairs among the linkages
|
|
81
|
+
# this ingest touched -- targeted index seeks, no scan. No-op unless the graph holds >1 repo.
|
|
82
|
+
from auryn.core.linkage import reconcile_linkages
|
|
83
|
+
|
|
84
|
+
# stream_apply writes the delta without materialising the target graph, and returns the
|
|
85
|
+
# linkage strings collected inline (see graph_sync.stream_apply / core.linkage).
|
|
86
|
+
labels = [s.label for s in NODE_SCHEMAS]
|
|
87
|
+
stats, reals, placeholders = stream_apply(writer, proj_id, labels, make_target)
|
|
88
|
+
reconcile_linkages(writer, reals, placeholders)
|
|
89
|
+
|
|
90
|
+
return SyncStats(
|
|
91
|
+
nodes_added=stats.nodes_added,
|
|
92
|
+
nodes_updated=stats.nodes_updated,
|
|
93
|
+
nodes_removed=stats.nodes_removed,
|
|
94
|
+
edges_added=stats.edges_added,
|
|
95
|
+
edges_updated=stats.edges_updated,
|
|
96
|
+
edges_removed=stats.edges_removed,
|
|
97
|
+
commit_sha=target_sha,
|
|
98
|
+
elapsed_seconds=time.monotonic() - started,
|
|
99
|
+
files_parsed=files_parsed,
|
|
100
|
+
files_cached=0,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _git_language_sync(
|
|
105
|
+
writer,
|
|
106
|
+
source,
|
|
107
|
+
target_sha: str,
|
|
108
|
+
*,
|
|
109
|
+
repo_uri: str,
|
|
110
|
+
project_name: str,
|
|
111
|
+
solution_name: str | None,
|
|
112
|
+
ref: str | None,
|
|
113
|
+
cache_root=None,
|
|
114
|
+
) -> LanguageIngestResult:
|
|
115
|
+
"""GitLanguage adapter for the multi-language endpoint (PHP has no solution/cache knobs --
|
|
116
|
+
accepted for the shared interface and ignored)."""
|
|
117
|
+
s = sync_at(writer, source, target_sha, project_name=project_name, repo_uri=repo_uri, ref=ref)
|
|
118
|
+
return LanguageIngestResult(
|
|
119
|
+
nodes_added=s.nodes_added,
|
|
120
|
+
nodes_updated=s.nodes_updated,
|
|
121
|
+
nodes_removed=s.nodes_removed,
|
|
122
|
+
edges_added=s.edges_added,
|
|
123
|
+
edges_updated=s.edges_updated,
|
|
124
|
+
edges_removed=s.edges_removed,
|
|
125
|
+
files_parsed=s.files_parsed,
|
|
126
|
+
files_cached=s.files_cached,
|
|
127
|
+
commit_sha=s.commit_sha,
|
|
128
|
+
elapsed_seconds=s.elapsed_seconds,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# The GitLanguage is registered by the import-light plugin.py (the entry-point target), whose
|
|
133
|
+
# lazy `sync` thunk calls `_git_language_sync` above -- so importing this tree-sitter engine
|
|
134
|
+
# module is deferred until a source actually contains PHP files.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""tree-sitter-php Language/Parser setup, in one place so file_parser.py and tests always get
|
|
2
|
+
the exact same configured parser. Like tree-sitter-java/-c-sharp it ships a pre-built PyPI
|
|
3
|
+
package -- no codegen/vendoring.
|
|
4
|
+
|
|
5
|
+
NOTE: tree-sitter-php does NOT expose a bare `language()`. The package bundles two grammars:
|
|
6
|
+
`language_php()` -- full PHP including the interleaved text/HTML template grammar, i.e. source
|
|
7
|
+
starting with `<?php` -- and `language_php_only()` (PHP-only, no HTML). Real `.php` source files
|
|
8
|
+
are template-aware (`<?php ... ?>` islands in HTML), so we use `language_php()`."""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import tree_sitter_php as tsphp
|
|
13
|
+
from tree_sitter import Language, Parser
|
|
14
|
+
|
|
15
|
+
LANGUAGE = Language(tsphp.language_php())
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def new_parser() -> Parser:
|
|
19
|
+
return Parser(LANGUAGE)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Canonical id scheme for the PHP graph, mirroring the Java/C# ingestors' (a
|
|
2
|
+
namespace/package tier over types-with-members, plus free functions like Python/C). A future
|
|
3
|
+
deep (full name-resolution) engine would key nodes by these same functions, so its output stays
|
|
4
|
+
interchangeable in one graph -- the id-parity the other ingestors rely on.
|
|
5
|
+
|
|
6
|
+
Ids embed names/positions and, for cross-ingestor consistency, parameter *type* text in callable
|
|
7
|
+
signatures -- never resolved/inferred types (the id-stability rule). PHP has no method overloading
|
|
8
|
+
(one name per class), so the signature is effectively redundant here, but it is kept so a PHP
|
|
9
|
+
callable id has the same shape as Java's/C's.
|
|
10
|
+
|
|
11
|
+
PHP namespaces are backslash-separated (`App\\Models`); the namespace id segment normalizes the
|
|
12
|
+
separator to `.` and lowercases (as Java does for dotted packages), so ids never carry a
|
|
13
|
+
backslash -- the authentic `App\\Models` form is preserved in the node's `qualified_name`."""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
_KIND_SLUG = {"Class": "class", "Interface": "interface", "Trait": "trait", "Enum": "enum"}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def project_id(project_name: str) -> str:
|
|
21
|
+
return f"php://{project_name}"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def namespace_id(proj_id: str, backslashed: str) -> str:
|
|
25
|
+
return f"{proj_id}/namespace/{backslashed.replace(chr(92), '.').lower()}"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def file_id(proj_id: str, rel_path: str) -> str:
|
|
29
|
+
return f"{proj_id}/file/{rel_path.replace(chr(92), '/').lstrip('./')}"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def type_id(scope_id: str, kind: str, name: str) -> str:
|
|
33
|
+
"""`scope_id` is the containing namespace_id or project_id (types with no namespace)."""
|
|
34
|
+
return f"{scope_id}.{_KIND_SLUG[kind]}.{name.lower()}"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _signature(param_types: list[str]) -> str:
|
|
38
|
+
return ",".join((t or "").lower() for t in param_types)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def function_id(scope_id: str, name: str, param_types: list[str]) -> str:
|
|
42
|
+
return f"{scope_id}.function.{name.lower()}({_signature(param_types)})"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def method_id(owner_type_id: str, name: str, param_types: list[str]) -> str:
|
|
46
|
+
return f"{owner_type_id}.method.{name.lower()}({_signature(param_types)})"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def property_id(owner_type_id: str, name: str) -> str:
|
|
50
|
+
return f"{owner_type_id}.property.{name.lower()}"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def constant_id(owner_type_id: str, name: str) -> str:
|
|
54
|
+
return f"{owner_type_id}.const.{name.lower()}"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def parameter_id(owner_id: str, position: int, name: str) -> str:
|
|
58
|
+
return f"{owner_id}.param.{position}.{name.lower()}"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def enum_case_id(enum_type_id: str, name: str) -> str:
|
|
62
|
+
return f"{enum_type_id}.case.{name.lower()}"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# -- cross-repo linkage (ADR 0004) -------------------------------------------------------
|
|
66
|
+
# Project-INDEPENDENT key for an externally-visible callable: stamped as the `linkage_key`
|
|
67
|
+
# property on every real Function/Method and used as the id of the placeholder that stands in
|
|
68
|
+
# for an unresolved call. NAME-BASED -- never param types (a consumer can't know a callee's param
|
|
69
|
+
# names). `qualified_name` is what a *consumer* writes: `App\\Billing::charge` (a method),
|
|
70
|
+
# `App\\helper` (a namespaced free function), or `strlen` (a global builtin). So `charge` defined
|
|
71
|
+
# in repo B and a `charge()` call in repo A land on one node when both share a graph. (Distinct
|
|
72
|
+
# from a node's project-scoped `id`, which keeps two repos' same-named locals from colliding.)
|
|
73
|
+
def extern_function_key(qualified_name: str) -> str:
|
|
74
|
+
return f"extern://php/{qualified_name}"
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Import-light PHP language registration -- the `auryn.languages` entry-point target.
|
|
2
|
+
|
|
3
|
+
Importing this module registers the PHP GitLanguage without importing tree-sitter: the
|
|
4
|
+
GitLanguage's `sync` is a lazy thunk that imports auryn.ingestors.php.git_sync only when a
|
|
5
|
+
source actually contains `.php` files. Single-phase today (fast tree-sitter-php); a deep pass --
|
|
6
|
+
full PHP name-resolution (use-statement aliasing, autoloading/PSR-4, inherited-member calls,
|
|
7
|
+
trait-flattened method sets) for precise cross-file symbol binding -- would slot in here as
|
|
8
|
+
`deep_sync` (making PHP two-phase like Python). Suffix/exclude constants live here (single source
|
|
9
|
+
of truth); git_sync imports them back."""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from auryn.core.registry import GitLanguage, register_git_language
|
|
14
|
+
|
|
15
|
+
LANGUAGE = "php"
|
|
16
|
+
FILE_SUFFIXES = (".php",)
|
|
17
|
+
EXCLUDED_DIR_NAMES = frozenset(
|
|
18
|
+
{".git", "vendor", "node_modules", "build", "dist", "cache", ".cache", "storage", ".idea"}
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _sync(*args, **kwargs):
|
|
23
|
+
"""Deferred until a source has PHP files -- imports the tree-sitter-php engine."""
|
|
24
|
+
from auryn.ingestors.php.git_sync import _git_language_sync
|
|
25
|
+
|
|
26
|
+
return _git_language_sync(*args, **kwargs)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
register_git_language(
|
|
30
|
+
GitLanguage(
|
|
31
|
+
language=LANGUAGE,
|
|
32
|
+
file_suffixes=FILE_SUFFIXES,
|
|
33
|
+
excluded_dir_names=EXCLUDED_DIR_NAMES,
|
|
34
|
+
sync=_sync,
|
|
35
|
+
)
|
|
36
|
+
)
|