Skip to content

Commit

Permalink
support route::controller, route:resource
Browse files Browse the repository at this point in the history
support Laravel 9 lang folder
  • Loading branch information
absszero committed Apr 10, 2022
1 parent fb67ede commit bb8dcbd
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 48 deletions.
10 changes: 5 additions & 5 deletions lib/namespace.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import sublime
from re import compile

patterns = dict([
(compile(r"""namespace\s*\(\s*(['"])\s*([^'"]+)\1"""), True),
(compile(r"""['"]namespace['"]\s*=>\s*(['"])([^'"]+)\1"""), True),
patterns = [
(compile(r"""(controller)\s*\(\s*['"]?([^'")]+)"""), False),
(compile(r"""resource\s*\(\s*['"][^'"]+['"]\s*,\s*(['"]?)([^,'"]+)"""), False),
])
(compile(r"""namespace\s*\(\s*(['"])\s*([^'"]+)\1"""), True),
(compile(r"""['"]namespace['"]\s*=>\s*(['"])([^'"]+)\1"""), True),
]


class Namespace:
Expand All @@ -24,7 +24,7 @@ def find(self, blocks):
def get_blocks(self, selection):
'''get all closure blocks'''
blocks = []
for pattern, isNamespace in patterns.items():
for pattern, isNamespace in patterns:
for match in pattern.finditer(self.fullText):
start = match.start()
if selection.a < start:
Expand Down
36 changes: 23 additions & 13 deletions lib/place.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_place(selection):
return view_place(path, line, selection)


def set_controller_action(path, selected):
def set_controller_action(path, selected, blocks):
''' set the controller action '''

path = path.replace('@', '.php@')
Expand All @@ -77,30 +77,40 @@ def set_controller_action(path, selected):
matched = class_controller_pattern.search(path)
if matched:
path = matched.group(1) + '.php@' + matched.group(2)

elif len(blocks) and blocks[0]['is_namespace'] is False:
"""resource or controller route"""
new_path = blocks[0]['namespace']
if new_path != path:
path = new_path + '.php@' + path
else:
path = new_path + '.php'

return path


def set_controller_namespace(path, selection):
def set_controller_namespace(path, selection, ns):
''' set the controller namespace '''

if '\\' != path[0]:
if '\\' != path[0] and ns:
# it's not absolute path namespace, get group namespace
namespace = Namespace(selection.view)
blocks = namespace.get_blocks(selection)
ns = namespace.find(blocks)
if ns:
path = ns + '\\' + path.lstrip('\\')
path = ns + '\\' + path.lstrip('\\')

return path


def controller_place(path, line, selection):
find = "@" in path or "Controller" in path or selection.is_class
if find is False:
namespace = Namespace(selection.view)
blocks = namespace.get_blocks(selection)
is_controller = "Controller" in path or selection.is_class

if is_controller is False and 0 == len(blocks):
return False

path = set_controller_action(path, selection)
path = set_controller_namespace(path, selection)
path = set_controller_action(path, selection, blocks)

ns = namespace.find(blocks)
path = set_controller_namespace(path, selection, ns)

place = Place(path)
place.is_controller = True
Expand Down Expand Up @@ -132,7 +142,7 @@ def lang_place(path, line, selected):
if (3 == len(split)):
vendor = '/vendor/' + split[0]
keys = split[-1].split('.')
path = 'resources/lang' + vendor + '/' + keys[0] + '.php'
path = 'lang' + vendor + '/' + keys[0] + '.php'

location = None
if (2 <= len(keys)):
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Goto various Laravel files

- Go to Config files and highlight option (EX. config/app.php)

- Go to Language files and highlight option (EX. resources/lang/en/messages.php )
- Go to Language files and highlight option (EX. lang/en/messages.php )

- Go to .env and highlight option

Expand Down
14 changes: 14 additions & 0 deletions tests/sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,17 @@
</x-alert>

<x-forms.input/>

Route::controller(HelloController::class)->group(function () {
Route::get('/posts/{id}', 'show');
});

Route::controller('HelloController')->group(function () {
Route::get('/posts/{id}', 'show');
});

Route::group(['namespace' => 'Resource'], function () {
Route::resource('photo', 'HelloController', ['only' => [
'index', 'show'
]]);
});
108 changes: 79 additions & 29 deletions tests/test_place.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@


class TestPlace(unittest.ViewTestCase):
def test_controller_route(self):
self.fixture("""
Route::group(['namespace' => 'Resource'], function () {
Route::controller('HelloController')->group(function () {
Route::get('/posts/{id}', 'sh|ow');
});
});""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual(True, place.is_controller)
self.assertEqual("Resource\\HelloController.php@show", place.path)

def test_resource_route(self):
self.fixture("""
Route::group(['namespace' => 'Resource'], function () {
Route::resource('photo', 'Hello|Controller', ['only' => [
'index', 'show'
]]);
});""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual(True, place.is_controller)
self.assertEqual("Resource\\HelloController.php", place.path)

def test_resource_route_action(self):
self.fixture("""
Route::group(['namespace' => 'Resource'], function () {
Route::resource('photo', 'HelloController', ['only' => [
'index', 'sho|w'
]]);
});""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual(True, place.is_controller)
self.assertEqual("Resource\\HelloController.php@show", place.path)

def test_controller(self):
self.fixture("""Route::get('/', 'HelloControll|er@index');""")

Expand Down Expand Up @@ -42,9 +84,10 @@ def test_component_with_namespace(self):
self.assertEqual("namespace/alert.php", place.path)

def test_view(self):
self.fixture("""Route::get('/', function () {
return view('hello|_view');
});""")
self.fixture("""
Route::get('/', function () {
return view('hello|_view');
});""")

selection = Selection(self.view)
place = get_place(selection)
Expand All @@ -60,9 +103,10 @@ def test_staticFile(self):
self.assertEqual("hello.JS", place.path)

def test_namespace58(self):
self.fixture("""Route::namespace('58')->group(function () {
Route::get('/', 'FiveEightC|ontroller@index');
});""")
self.fixture("""
Route::namespace('58')->group(function () {
Route::get('/', 'FiveEightC|ontroller@index');
});""")

selection = Selection(self.view)
place = get_place(selection)
Expand All @@ -71,9 +115,10 @@ def test_namespace58(self):
self.assertEqual('58\\FiveEightController.php@index', place.path)

def test_namespace52(self):
self.fixture("""Route::group(['namespace' => '52'], function () {
Route::get('/', 'FiveTw|oController@index');
});""")
self.fixture("""
Route::group(['namespace' => '52'], function () {
Route::get('/', 'FiveTw|oController@index');
});""")

selection = Selection(self.view)
place = get_place(selection)
Expand All @@ -82,9 +127,10 @@ def test_namespace52(self):
self.assertEqual('52\\FiveTwoController.php@index', place.path)

def test_namespaceLumen(self):
self.fixture("""$router->group(['namespace' => 'Lumen'], function () use ($router) {
Route::get('/', 'LumenCo|ntroller@index');
});""")
self.fixture("""
$router->group(['namespace' => 'Lumen'], function () use ($router) {
Route::get('/', 'LumenCo|ntroller@index');
});""")

selection = Selection(self.view)
place = get_place(selection)
Expand All @@ -93,19 +139,21 @@ def test_namespaceLumen(self):
self.assertEqual('Lumen\\LumenController.php@index', place.path)

def test_view_namespace(self):
self.fixture("""Route::get('/', function () {
return view('Namespace::h|ello_view');
});""")
self.fixture("""
Route::get('/', function () {
return view('Namespace::h|ello_view');
});""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual('hello_view.blade.php', place.path)

def test_absolute_path(self):
self.fixture("""Route::group(['namespace' => 'Abc'], function () {
Route::get('/', '\\Absolute\\IndexCont|roller@index')->name('index');
});""")
self.fixture("""
Route::group(['namespace' => 'Abc'], function () {
Route::get('/', '\\Absolute\\IndexCont|roller@index')->name('index');
});""")

selection = Selection(self.view)
place = get_place(selection)
Expand Down Expand Up @@ -172,39 +220,39 @@ def test_lang_underscore(self):
selection = Selection(self.view)
place = get_place(selection)

self.assertEqual('resources/lang/messages.php', place.path)
self.assertEqual('lang/messages.php', place.path)

def test_lang_blade_directive(self):
self.fixture("""@lang('messages.we|lcome');""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual('resources/lang/messages.php', place.path)
self.assertEqual('lang/messages.php', place.path)

def test_lang_trans(self):
self.fixture("""trans('messages.we|lcome');""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual('resources/lang/messages.php', place.path)
self.assertEqual('lang/messages.php', place.path)

def test_lang_trans_choice(self):
self.fixture("""trans_choice('messages.a|pples', 10);""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual('resources/lang/messages.php', place.path)
self.assertEqual('lang/messages.php', place.path)

def test_lang_trans_package(self):
self.fixture("""trans('package::messa|ges');""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual('resources/lang/vendor/package/messages.php', place.path)
self.assertEqual('lang/vendor/package/messages.php', place.path)

def test_relative_path_static_file(self):
self.fixture("""'./../../hel|lo.css'""")
Expand Down Expand Up @@ -296,19 +344,21 @@ def test_v8_route(self):
self.assertEqual('EightController.php', place.path)

def test_v8_group_namespae_abs_route(self):
self.fixture("""Route::group(['namespace' => 'L8'], function () {
Route::get('/', [\\EightControl|ler::class, 'index']);
});""")
self.fixture("""
Route::group(['namespace' => 'L8'], function () {
Route::get('/', [\\EightControl|ler::class, 'index']);
});""")

selection = Selection(self.view)
place = get_place(selection)

self.assertEqual('\\EightController.php@index', place.path)

def test_v8_group_namespae_route(self):
self.fixture("""Route::group(['namespace' => 'L8'], function () {
Route::get('/', [EightCon|troller::class, 'index']);
});""")
self.fixture("""
Route::group(['namespace' => 'L8'], function () {
Route::get('/', [EightCon|troller::class, 'index']);
});""")

selection = Selection(self.view)
place = get_place(selection)
Expand Down

0 comments on commit bb8dcbd

Please sign in to comment.