Skip to content

Commit 2fcfc76

Browse files
Merge pull request #19 from dimitriBouteille/develop
Release 1.1.0
2 parents 982dc84 + 381e002 commit 2fcfc76

File tree

7 files changed

+76
-12
lines changed

7 files changed

+76
-12
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# WordPress Rest API
22

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)
44

55
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.
66

@@ -48,13 +48,17 @@ use Dbout\WpRestApi\RouteLoader;
4848

4949
// One folder
5050
$loader = new RouteLoader(__DIR__ . '/src/Api/Routes');
51-
$loader->register();
5251

5352
// Multiple folders
5453
$loader = new RouteLoader([
5554
__DIR__ . '/themes/my-theme/api'
5655
__DIR__ . '/src/Api/Routes',
5756
]);
57+
58+
// You can also use pattern
59+
$loader = new RouteLoader(__DIR__ . '/src/Modules/*/Api/Routes');
60+
61+
$loader->register();
5862
```
5963

6064
> 💡 The module will automatically search for all classes that are in the folder and sub folder.

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
}
3535
},
3636
"require-dev": {
37-
"friendsofphp/php-cs-fixer": "^3.28",
37+
"friendsofphp/php-cs-fixer": "^3.54",
3838
"phpstan/phpstan": "^1.10",
39-
"rector/rector": "1.0.1",
39+
"rector/rector": "^1.0",
4040
"phpunit/phpunit": "^10.5",
4141
"phpstan/phpstan-phpunit": "^1.3",
4242
"phpstan/extension-installer": "^1.3",
4343
"szepeviktor/phpstan-wordpress": "^1.3",
44-
"roots/wordpress": "6.4"
44+
"roots/wordpress": "^6.5"
4545
},
4646
"config": {
4747
"allow-plugins": {

src/Helpers/Parser.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,29 @@ public static function findClassName(?string $fileContent): ?string
2525
throw new \InvalidArgumentException('The content does not contain PHP code.');
2626
}
2727

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+
}
3039
}
3140

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;
3352
}
3453
}

src/Loaders/AnnotationDirectoryLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ function (\SplFileInfo $current): bool {
5252

5353
$routes = [];
5454
foreach ($files as $file) {
55-
if (!$file->isFile() || !str_ends_with($file->getFilename(), '.php')) {
55+
if (!$file->isFile() || $file->getExtension() !== 'php') {
5656
continue;
5757
}
5858

5959
/** @var class-string|null $class */
6060
$class = $this->findClass($file);
61-
if ($class === null) {
61+
if ($class === null || !class_exists($class)) {
6262
continue;
6363
}
6464

src/RouteLoader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,19 @@ protected function getRoutes(): array
6060
*/
6161
protected function findRoutes(): array
6262
{
63-
$directories = is_array($this->routeDirectory) ? $this->routeDirectory : [$this->routeDirectory];
63+
$tmpDirectories = is_array($this->routeDirectory) ? $this->routeDirectory : [$this->routeDirectory];
6464
$routes = [];
6565

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+
6676
foreach ($directories as $directory) {
6777
$directory = new \SplFileInfo($directory);
6878
if (!$directory->isDir()) {

tests/Helpers/ParserTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function providerFindClassName(): \Generator
4343
return $fileContent;
4444
};
4545

46-
yield 'Light php file' => [
46+
yield 'Light php file' => [
4747
$load('source-1.php'),
4848
'App\Routes\MyRoute',
4949
];
@@ -57,6 +57,11 @@ public static function providerFindClassName(): \Generator
5757
$load('source-3.php'),
5858
'App\Routes\MyRoute',
5959
];
60+
61+
yield 'With phpdoc intro & multiple class/namespace label' => [
62+
$load('source-4.php'),
63+
'App\Routes\MyRoute',
64+
];
6065
}
6166

6267
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
 (0)