2025-09-08 09:20:42 +01:00
|
|
|
local ls = require 'luasnip'
|
2025-08-26 18:23:14 +01:00
|
|
|
local s = ls.snippet
|
|
|
|
|
local t = ls.text_node
|
|
|
|
|
local i = ls.insert_node
|
|
|
|
|
local f = ls.function_node
|
|
|
|
|
|
2025-11-09 23:03:51 +00:00
|
|
|
local function get_psr4_root(path)
|
|
|
|
|
-- local dir = vim.fs.dirname(path)
|
|
|
|
|
-- while dir ~= '/' and dir ~= nil do
|
|
|
|
|
-- local composer_json_path = dir .. '/composer.json'
|
|
|
|
|
-- if vim.fn.filereadable(composer_json_path) == 1 then
|
|
|
|
|
-- break
|
|
|
|
|
-- end
|
|
|
|
|
-- dir = vim.fs.dirname(dir)
|
|
|
|
|
-- end
|
2025-09-08 09:19:08 +01:00
|
|
|
local handle = io.popen [[php -r "echo array_keys(json_decode(file_get_contents('composer.json'), true)['autoload']['psr-4'])[0];"]]
|
|
|
|
|
local ns_root = handle and handle:read '*a' or ''
|
|
|
|
|
if handle then
|
|
|
|
|
handle:close()
|
|
|
|
|
end
|
|
|
|
|
ns_root = ns_root:gsub('\\$', '') -- Remove trailing backslash
|
|
|
|
|
return ns_root
|
|
|
|
|
end
|
|
|
|
|
|
2025-08-26 18:23:14 +01:00
|
|
|
local function psr_namespace(args, snip)
|
2025-09-08 09:20:42 +01:00
|
|
|
local path = snip.env.TM_FILENAME_FULL or ''
|
2025-09-08 09:19:08 +01:00
|
|
|
local composer_root = get_psr4_root()
|
|
|
|
|
-- Find the directory mapped by composer.json
|
|
|
|
|
local root_dir = composer_root:gsub('\\', '/')
|
|
|
|
|
local ns = path:match(root_dir .. '/(.*)/[^/]+%.php$')
|
2025-08-26 18:23:14 +01:00
|
|
|
if ns then
|
2025-09-08 09:19:08 +01:00
|
|
|
return composer_root .. '\\' .. ns:gsub('/', '\\')
|
2025-08-26 18:23:14 +01:00
|
|
|
else
|
2025-09-08 09:19:08 +01:00
|
|
|
return composer_root ~= '' and composer_root or 'App'
|
2025-08-26 18:23:14 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function class_name(args, snip)
|
2025-09-08 09:20:42 +01:00
|
|
|
local filename = snip.env.TM_FILENAME or ''
|
|
|
|
|
return filename:match '([^%.]+)' or 'ClassName'
|
2025-08-26 18:23:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return {
|
2025-09-08 09:20:42 +01:00
|
|
|
s('du', { t 'dump(', i(0), t ');' }),
|
|
|
|
|
s('dt', { t '\\PhpStan\\dumpType(', i(0), t ');' }),
|
|
|
|
|
s('ql', {
|
|
|
|
|
t '\\Illuminate\\Support\\Facades\\DB::listen(function (\\Illuminate\\Database\\Events\\QueryExecuted $e) {',
|
|
|
|
|
t { '', ' dump($e->sql, $e->bindings);' },
|
|
|
|
|
t { '', '});' },
|
2025-08-26 18:23:14 +01:00
|
|
|
}),
|
2025-09-08 09:20:42 +01:00
|
|
|
s('test', {
|
|
|
|
|
t 'test(',
|
|
|
|
|
i(1),
|
|
|
|
|
t ', function () {',
|
|
|
|
|
t { '', ' ' },
|
|
|
|
|
i(0),
|
|
|
|
|
t { '', ');' },
|
2025-08-26 18:23:14 +01:00
|
|
|
}),
|
2025-09-08 09:20:42 +01:00
|
|
|
s('/**', {
|
|
|
|
|
t '/**',
|
|
|
|
|
t { '', ' * ' },
|
|
|
|
|
i(0),
|
|
|
|
|
t { '', ' */' },
|
2025-08-26 18:23:14 +01:00
|
|
|
}),
|
2025-09-08 09:20:42 +01:00
|
|
|
s('@p', { t '@property ', i(1), t ' $', i(0) }),
|
|
|
|
|
s('@pb', { t '@property bool $', i(0) }),
|
|
|
|
|
s('@ps', { t '@property string $', i(0) }),
|
|
|
|
|
s('@pi', { t '@property int $', i(0) }),
|
|
|
|
|
s('@pc', { t '@property \\Illuminate\\Support\\Collection<int, ', i(1), t '> $', i(0) }),
|
|
|
|
|
s('@pd', { t '@property \\Illuminate\\Support\\Carbon $', i(0) }),
|
|
|
|
|
s('@pp', {
|
|
|
|
|
t '/**',
|
|
|
|
|
t { '', ' * @property int $id' },
|
|
|
|
|
t { '', ' * ' },
|
|
|
|
|
i(1),
|
|
|
|
|
t { '', ' * @property \\Illuminate\\Support\\Carbon $created_at' },
|
|
|
|
|
t { '', ' * @property \\Illuminate\\Support\\Carbon $updated_at' },
|
|
|
|
|
t { '', ' *', ' * Relationships' },
|
|
|
|
|
t { '', ' * ' },
|
|
|
|
|
i(0),
|
|
|
|
|
t { '', ' */' },
|
2025-08-26 18:23:14 +01:00
|
|
|
}),
|
2025-09-08 09:20:42 +01:00
|
|
|
s('php', {
|
|
|
|
|
t '<?php',
|
|
|
|
|
t { '', '' },
|
|
|
|
|
t 'namespace ',
|
2025-08-26 18:23:14 +01:00
|
|
|
f(psr_namespace, {}),
|
2025-09-08 09:20:42 +01:00
|
|
|
t ';',
|
|
|
|
|
t { '', '', '/**', ' * Class ' },
|
2025-08-26 18:23:14 +01:00
|
|
|
f(class_name, {}),
|
2025-09-08 09:20:42 +01:00
|
|
|
t { '', ' */', 'class ' },
|
2025-08-26 18:23:14 +01:00
|
|
|
f(class_name, {}),
|
|
|
|
|
i(1),
|
2025-09-08 09:20:42 +01:00
|
|
|
t { '', '{', ' public function __construct()', ' {', ' ' },
|
|
|
|
|
i(0, '// TODO: Implement constructor'),
|
|
|
|
|
t { '', ' }', '}' },
|
2025-08-26 18:23:14 +01:00
|
|
|
}),
|
2025-09-08 09:20:42 +01:00
|
|
|
s('pub', {
|
|
|
|
|
t 'public function ',
|
|
|
|
|
i(1),
|
|
|
|
|
t '(',
|
|
|
|
|
i(2),
|
|
|
|
|
t ')',
|
|
|
|
|
t { '', '{' },
|
|
|
|
|
t { '', ' ' },
|
|
|
|
|
i(0),
|
|
|
|
|
t { '', '}' },
|
2025-08-26 18:23:14 +01:00
|
|
|
}),
|
2025-09-08 09:20:42 +01:00
|
|
|
s('pro', {
|
|
|
|
|
t 'protected function ',
|
|
|
|
|
i(1),
|
|
|
|
|
t '(',
|
|
|
|
|
i(2),
|
|
|
|
|
t ')',
|
|
|
|
|
t { '', '{' },
|
|
|
|
|
t { '', ' ' },
|
|
|
|
|
i(0),
|
|
|
|
|
t { '', '}' },
|
2025-08-26 18:23:14 +01:00
|
|
|
}),
|
|
|
|
|
}
|