@@ -1763,6 +1763,59 @@ static const char *php_chain_prefix_arg(CBMArena *a, TSNode call_node, const cha
17631763 return NULL ;
17641764}
17651765
1766+ /* The value of a plain PHP string literal ('...' or "..." with no
1767+ * interpolation or escapes), "" for an empty one; NULL for anything else. */
1768+ static const char * php_plain_string_value (CBMArena * a , TSNode node , const char * source ) {
1769+ const char * kind = ts_node_type (node );
1770+ if (strcmp (kind , "string" ) != 0 && strcmp (kind , "encapsed_string" ) != 0 ) {
1771+ return NULL ;
1772+ }
1773+ uint32_t nc = ts_node_named_child_count (node );
1774+ for (uint32_t i = 0 ; i < nc ; i ++ ) {
1775+ if (strcmp (ts_node_type (ts_node_named_child (node , i )), "string_content" ) != 0 ) {
1776+ return NULL ;
1777+ }
1778+ }
1779+ return nc == 0 ? "" : cbm_node_text (a , ts_node_named_child (node , 0 ), source );
1780+ }
1781+
1782+ /* The first argument's value node of a PHP call (unwrapping `argument`). */
1783+ static TSNode php_first_arg_value (TSNode call_node ) {
1784+ TSNode none = {0 };
1785+ TSNode args = ts_node_child_by_field_name (call_node , TS_FIELD ("arguments" ));
1786+ if (ts_node_is_null (args ) || ts_node_named_child_count (args ) == 0 ) {
1787+ return none ;
1788+ }
1789+ TSNode arg = ts_node_named_child (args , 0 );
1790+ if (strcmp (ts_node_type (arg ), "argument" ) == 0 ) {
1791+ return ts_node_named_child_count (arg ) == SKIP_ONE ? ts_node_named_child (arg , 0 ) : none ;
1792+ }
1793+ return arg ;
1794+ }
1795+
1796+ /* The literal 'prefix' of an attribute-array group (#1146):
1797+ * Route::group(['prefix' => 'v1/autocomplete', 'as' => ...], function () {...}),
1798+ * the form Firefly III's whole API is written in. */
1799+ static const char * php_group_array_prefix (CBMArena * a , TSNode group_call , const char * source ) {
1800+ TSNode attrs = php_first_arg_value (group_call );
1801+ if (ts_node_is_null (attrs ) || strcmp (ts_node_type (attrs ), "array_creation_expression" ) != 0 ) {
1802+ return NULL ;
1803+ }
1804+ uint32_t nc = ts_node_named_child_count (attrs );
1805+ for (uint32_t i = 0 ; i < nc ; i ++ ) {
1806+ TSNode el = ts_node_named_child (attrs , i );
1807+ if (strcmp (ts_node_type (el ), "array_element_initializer" ) != 0 ||
1808+ ts_node_named_child_count (el ) != PAIR_LEN ) {
1809+ continue ;
1810+ }
1811+ const char * key = php_plain_string_value (a , ts_node_named_child (el , 0 ), source );
1812+ if (key && strcmp (key , "prefix" ) == 0 ) {
1813+ return php_plain_string_value (a , ts_node_named_child (el , SKIP_ONE ), source );
1814+ }
1815+ }
1816+ return NULL ;
1817+ }
1818+
17661819static const char * php_group_prefix_for_call (CBMArena * a , TSNode node , const char * source ) {
17671820 const char * parts [PHP_PREFIX_PARTS_MAX ];
17681821 int part_count = 0 ;
@@ -1785,7 +1838,14 @@ static const char *php_group_prefix_for_call(CBMArena *a, TSNode node, const cha
17851838 strcmp (ts_node_type (p ), "scoped_call_expression" ) == 0 )) {
17861839 TSNode gname = ts_node_child_by_field_name (p , TS_FIELD ("name" ));
17871840 char * gtxt = ts_node_is_null (gname ) ? NULL : cbm_node_text (a , gname , source );
1788- if (gtxt && strcmp (gtxt , "group" ) == 0 ) {
1841+ const char * array_prefix = gtxt && strcmp (gtxt , "group" ) == 0
1842+ ? php_group_array_prefix (a , p , source )
1843+ : NULL ;
1844+ if (array_prefix ) {
1845+ if (part_count < PHP_PREFIX_PARTS_MAX ) {
1846+ parts [part_count ++ ] = array_prefix ; /* inner-first */
1847+ }
1848+ } else if (gtxt && strcmp (gtxt , "group" ) == 0 ) {
17891849 /* Scan the receiver chain for prefix('...'). */
17901850 TSNode recv = ts_node_child_by_field_name (p , TS_FIELD ("object" ));
17911851 for (int hops = 0 ; hops < PHP_GROUP_WALK_MAX && !ts_node_is_null (recv );
@@ -1843,6 +1903,81 @@ static const char *php_group_prefix_for_call(CBMArena *a, TSNode node, const cha
18431903 return pos ? cbm_arena_strndup (a , buf , pos ) : NULL ;
18441904}
18451905
1906+ /* A Laravel route registrar receiver (#1146): the Route facade — also at the
1907+ * root of a registrar chain, Route::middleware('auth')->get(...) — or the
1908+ * $router instance route files are loaded with (RouteServiceProvider, Lumen).
1909+ * The same literal `Route` scope the #952 callee qualification accepts. */
1910+ static bool php_is_laravel_registrar (CBMArena * a , TSNode call_node , const char * source ) {
1911+ TSNode cur = call_node ;
1912+ for (int hops = 0 ; hops < PHP_GROUP_WALK_MAX && !ts_node_is_null (cur ); hops ++ ) {
1913+ const char * kind = ts_node_type (cur );
1914+ if (strcmp (kind , "scoped_call_expression" ) == 0 ) {
1915+ TSNode scope = ts_node_child_by_field_name (cur , TS_FIELD ("scope" ));
1916+ const char * st = ts_node_is_null (scope ) ? NULL : cbm_node_text (a , scope , source );
1917+ return st && strcmp (st , "Route" ) == 0 ;
1918+ }
1919+ if (strcmp (kind , "variable_name" ) == 0 ) {
1920+ const char * vt = cbm_node_text (a , cur , source );
1921+ return vt && strcmp (vt , "$router" ) == 0 ;
1922+ }
1923+ if (strcmp (kind , "member_call_expression" ) != 0 ) {
1924+ return false;
1925+ }
1926+ cur = ts_node_child_by_field_name (cur , TS_FIELD ("object" ));
1927+ }
1928+ return false;
1929+ }
1930+
1931+ /* Laravel accepts a route URI with or without its leading slash and trims
1932+ * '/' from both ends: Route::get('users', ...) serves /users, and '' is the
1933+ * group root. The route passes only mint a Route for a path that starts with
1934+ * '/', so give a recognised registration's literal URI its slash here, where
1935+ * the first argument is still at hand (#1146). Returns NULL — leave the call
1936+ * alone — for any other call, any non-literal URI, or one already slashed:
1937+ * `$request->get('name')`, `Cache::get('key')` and a collection's get('k')
1938+ * keep their argument. */
1939+ static const char * php_laravel_route_uri (CBMArena * a , TSNode call_node , const char * source ) {
1940+ static const char * const verbs [] = {"get" , "post" , "put" , "patch" , "delete" , NULL };
1941+ TSNode mname = ts_node_child_by_field_name (call_node , TS_FIELD ("name" ));
1942+ const char * verb = ts_node_is_null (mname ) ? NULL : cbm_node_text (a , mname , source );
1943+ bool is_verb = false;
1944+ for (int i = 0 ; verb && verbs [i ] && !is_verb ; i ++ ) {
1945+ is_verb = strcmp (verb , verbs [i ]) == 0 ;
1946+ }
1947+ if (!is_verb || !php_is_laravel_registrar (a , call_node , source )) {
1948+ return NULL ;
1949+ }
1950+ TSNode arg0 = php_first_arg_value (call_node );
1951+ const char * uri = ts_node_is_null (arg0 ) ? NULL : php_plain_string_value (a , arg0 , source );
1952+ if (!uri || uri [0 ] == '/' ) {
1953+ return NULL ;
1954+ }
1955+ size_t len = strlen (uri );
1956+ while (len > 0 && uri [len - SKIP_ONE ] == '/' ) {
1957+ len -- ;
1958+ }
1959+ return cbm_arena_sprintf (a , "/%.*s" , (int )len , uri );
1960+ }
1961+
1962+ /* Is a Laravel registration's action (its last argument) a string — 'index'
1963+ * inside a Route::controller() group, 'UserController@show'? Such an action
1964+ * does not say which class owns the method, so resolving it could only guess a
1965+ * same-named function anywhere in the repo. */
1966+ static bool php_route_action_is_string (TSNode call_node ) {
1967+ TSNode args = ts_node_child_by_field_name (call_node , TS_FIELD ("arguments" ));
1968+ uint32_t nc = ts_node_is_null (args ) ? 0 : ts_node_named_child_count (args );
1969+ if (nc < PAIR_LEN ) {
1970+ return false;
1971+ }
1972+ TSNode action = ts_node_named_child (args , nc - SKIP_ONE );
1973+ if (strcmp (ts_node_type (action ), "argument" ) == 0 &&
1974+ ts_node_named_child_count (action ) == SKIP_ONE ) {
1975+ action = ts_node_named_child (action , 0 );
1976+ }
1977+ const char * kind = ts_node_type (action );
1978+ return strcmp (kind , "string" ) == 0 || strcmp (kind , "encapsed_string" ) == 0 ;
1979+ }
1980+
18461981static bool is_nested_verilog_call_wrapper (CBMLanguage lang , TSNode node ) {
18471982 if (lang != CBM_LANG_VERILOG || strcmp (ts_node_type (node ), "subroutine_call" ) != 0 ) {
18481983 return false;
@@ -3941,6 +4076,16 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
39414076 }
39424077 if (!ts_node_is_null (args )) {
39434078 call .first_string_arg = extract_url_or_topic_arg (ctx , args , call .callee_name );
4079+ /* #1146: a Laravel registration's slashless URI ('users', '')
4080+ * becomes '/users' / '/' so it mints and composes below. */
4081+ const char * laravel_uri = NULL ;
4082+ if (ctx -> language == CBM_LANG_PHP && call .callee_name &&
4083+ cbm_service_pattern_route_method (call .callee_name ) != NULL ) {
4084+ laravel_uri = php_laravel_route_uri (ctx -> arena , node , ctx -> source );
4085+ if (laravel_uri ) {
4086+ call .first_string_arg = laravel_uri ;
4087+ }
4088+ }
39444089 /* #952: routes registered inside Laravel `prefix()->group()`
39454090 * closures must carry the composed path — the resolve passes
39464091 * only see the flat CBMCall, so the enclosing chain can only
@@ -3959,7 +4104,10 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
39594104 : cbm_arena_strndup (ctx -> arena , gp , strlen (gp ));
39604105 }
39614106 }
3962- if (call .first_string_arg && call .first_string_arg [0 ] == '/' ) {
4107+ /* A slashless registration's string action would only be
4108+ * guessed at (#1146): mint its Route without a handler. */
4109+ if (call .first_string_arg && call .first_string_arg [0 ] == '/' &&
4110+ !(laravel_uri && php_route_action_is_string (node ))) {
39634111 call .second_arg_name = extract_handler_arg (ctx , args );
39644112 }
39654113 if (ctx -> language == CBM_LANG_OBJECTSCRIPT_UDL ||
0 commit comments