File tree 7 files changed +76
-12
lines changed
7 files changed +76
-12
lines changed Original file line number Diff line number Diff line change 1
1
# WordPress Rest API
2
2
3
- ![ GitHub Release] ( https://img.shields.io/github/v/release/dimitriBouteille/wp-module-rest-api ) [ ![ tests] ( https://img.shields.io/github/actions/workflow/status/dimitriBouteille/wp-orm/tests.yml?label=tests )] ( https://github.com/dimitriBouteille/wp-module-rest-api/actions/workflows/tests.yml )
3
+ [ ![ GitHub Release] ( https://img.shields.io/github/v/release/dimitriBouteille/wp-module-rest-api )] ( https://github.com/dimitriBouteille/wp-module-rest-api/releases ) [ ![ tests] ( https://img.shields.io/github/actions/workflow/status/dimitriBouteille/wp-orm/tests.yml?label=tests )] ( https://github.com/dimitriBouteille/wp-module-rest-api/actions/workflows/tests.yml ) [ ![ Packagist Downloads ] ( https://img.shields.io/packagist/dt/dbout/wp-module-rest-api?color=yellow )] ( https://packagist.org/packages/dbout/wp-module-rest-api )
4
4
5
5
WordPress module designed for developers that want to add routes to the [ WordPress Rest API] ( https://developer.wordpress.org/rest-api/ ) in a few moments.
6
6
@@ -48,13 +48,17 @@ use Dbout\WpRestApi\RouteLoader;
48
48
49
49
// One folder
50
50
$loader = new RouteLoader(__DIR__ . '/src/Api/Routes');
51
- $loader->register();
52
51
53
52
// Multiple folders
54
53
$loader = new RouteLoader([
55
54
__DIR__ . '/themes/my-theme/api'
56
55
__DIR__ . '/src/Api/Routes',
57
56
]);
57
+
58
+ // You can also use pattern
59
+ $loader = new RouteLoader(__DIR__ . '/src/Modules/*/Api/Routes');
60
+
61
+ $loader->register();
58
62
```
59
63
60
64
> 💡 The module will automatically search for all classes that are in the folder and sub folder.
Original file line number Diff line number Diff line change 34
34
}
35
35
},
36
36
"require-dev" : {
37
- "friendsofphp/php-cs-fixer" : " ^3.28 " ,
37
+ "friendsofphp/php-cs-fixer" : " ^3.54 " ,
38
38
"phpstan/phpstan" : " ^1.10" ,
39
- "rector/rector" : " 1.0.1 " ,
39
+ "rector/rector" : " ^ 1.0" ,
40
40
"phpunit/phpunit" : " ^10.5" ,
41
41
"phpstan/phpstan-phpunit" : " ^1.3" ,
42
42
"phpstan/extension-installer" : " ^1.3" ,
43
43
"szepeviktor/phpstan-wordpress" : " ^1.3" ,
44
- "roots/wordpress" : " 6.4 "
44
+ "roots/wordpress" : " ^6.5 "
45
45
},
46
46
"config" : {
47
47
"allow-plugins" : {
Original file line number Diff line number Diff line change @@ -25,10 +25,29 @@ public static function findClassName(?string $fileContent): ?string
25
25
throw new \InvalidArgumentException ('The content does not contain PHP code. ' );
26
26
}
27
27
28
- if (preg_match ('#^namespace\s+(.+?);.*class\s+(\w+).+;$#sm ' , $ fileContent , $ m )) {
29
- return $ m [1 ].'\\' .$ m [2 ];
28
+ $ class = null ;
29
+ $ i = 0 ;
30
+ $ counter = count ($ tokens );
31
+ for (;$ i < $ counter ;$ i ++) {
32
+ if ($ tokens [$ i ][0 ] === T_CLASS ) {
33
+ for ($ j = $ i + 1 ;$ j < $ counter ;$ j ++) {
34
+ if ($ tokens [$ j ] === '{ ' ) {
35
+ $ class = $ tokens [$ i + 2 ][1 ];
36
+ }
37
+ }
38
+ }
30
39
}
31
40
32
- return null ;
41
+ if ($ class === null || $ class === '' ) {
42
+ return null ;
43
+ }
44
+
45
+ $ namespace = null ;
46
+ if (preg_match ('#(^|\s)namespace(.*?)\s*;#sm ' , $ fileContent , $ m )) {
47
+ $ namespace = $ m [2 ] ?? null ;
48
+ $ namespace = $ namespace !== null ? trim ($ namespace ) : null ;
49
+ }
50
+
51
+ return $ namespace ? $ namespace . "\\" . $ class : $ class ;
33
52
}
34
53
}
Original file line number Diff line number Diff line change @@ -52,13 +52,13 @@ function (\SplFileInfo $current): bool {
52
52
53
53
$ routes = [];
54
54
foreach ($ files as $ file ) {
55
- if (!$ file ->isFile () || ! str_ends_with ( $ file ->getFilename (), ' . php ') ) {
55
+ if (!$ file ->isFile () || $ file ->getExtension () !== ' php ' ) {
56
56
continue ;
57
57
}
58
58
59
59
/** @var class-string|null $class */
60
60
$ class = $ this ->findClass ($ file );
61
- if ($ class === null ) {
61
+ if ($ class === null || ! class_exists ( $ class ) ) {
62
62
continue ;
63
63
}
64
64
Original file line number Diff line number Diff line change @@ -60,9 +60,19 @@ protected function getRoutes(): array
60
60
*/
61
61
protected function findRoutes (): array
62
62
{
63
- $ directories = is_array ($ this ->routeDirectory ) ? $ this ->routeDirectory : [$ this ->routeDirectory ];
63
+ $ tmpDirectories = is_array ($ this ->routeDirectory ) ? $ this ->routeDirectory : [$ this ->routeDirectory ];
64
64
$ routes = [];
65
65
66
+ $ directories = [];
67
+ foreach ($ tmpDirectories as $ dir ) {
68
+ $ globalDirs = glob ($ dir );
69
+ if (!is_array ($ globalDirs )) {
70
+ continue ;
71
+ }
72
+
73
+ $ directories = array_merge ($ directories , $ globalDirs );
74
+ }
75
+
66
76
foreach ($ directories as $ directory ) {
67
77
$ directory = new \SplFileInfo ($ directory );
68
78
if (!$ directory ->isDir ()) {
Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ public static function providerFindClassName(): \Generator
43
43
return $ fileContent ;
44
44
};
45
45
46
- yield 'Light php file ' => [
46
+ yield 'Light php file ' => [
47
47
$ load ('source-1.php ' ),
48
48
'App\Routes\MyRoute ' ,
49
49
];
@@ -57,6 +57,11 @@ public static function providerFindClassName(): \Generator
57
57
$ load ('source-3.php ' ),
58
58
'App\Routes\MyRoute ' ,
59
59
];
60
+
61
+ yield 'With phpdoc intro & multiple class/namespace label ' => [
62
+ $ load ('source-4.php ' ),
63
+ 'App\Routes\MyRoute ' ,
64
+ ];
60
65
}
61
66
62
67
/**
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * Copyright (c) 2024 Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
4
+ * See LICENSE.txt for license details.
5
+ *
6
+ * Author: Dimitri BOUTEILLE <bonjour@dimitri-bouteille.fr>
7
+ */
8
+
9
+ namespace App \Routes ;
10
+
11
+ use Dbout \WpRestApi \Attributes \Action ;
12
+ use Dbout \WpRestApi \Attributes \Route ;
13
+ use Dbout \WpRestApi \Enums \Method ;
14
+
15
+ #[Route(
16
+ namespace: 'app/v2 ' ,
17
+ route: 'document/(?P<documentId>\d+) '
18
+ )]
19
+ class MyRoute
20
+ {
21
+ #[Action(Method::GET )]
22
+ public function get (): \WP_REST_Response
23
+ {
24
+ throw new \Exception ('Invalid builder class type. ' );
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments