Skip to content

Commit 6e672a7

Browse files
Custom namespace support (#549)
* Add support to use restify in different directory and namespace * Make sure we can resolve repositories from any path * allow dev branches * revert config changes * repositoriesFrom now supports passing namespace * Register in default app now requires to pass default namespace * Trait which is used to load repositories from any service provider * Remove unused RestifyApi * Setup fixtures + test namespaces * Data provider trait to use with testing fixtures * Unit test to resolve repositories from namespace * Tests all repositories are loaded from service provider with valid json * Document repository registration
1 parent c2acbbd commit 6e672a7

20 files changed

+334
-10
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
"autoload-dev": {
5050
"psr-4": {
5151
"Binaryk\\LaravelRestify\\Tests\\": "tests",
52-
"Binaryk\\LaravelRestify\\Tests\\Database\\Factories\\": "tests/database/factories"
52+
"Binaryk\\LaravelRestify\\Tests\\Database\\Factories\\": "tests/database/factories",
53+
"App\\": "tests/Fixtures/App",
54+
"CustomNamespace\\": "tests/Fixtures/CustomNamespace"
5355
}
5456
},
5557
"scripts": {
@@ -72,5 +74,6 @@
7274
"Restify": "Binaryk\\LaravelRestify\\RestifyFacade"
7375
}
7476
}
75-
}
77+
},
78+
"minimum-stability": "dev"
7679
}

config/restify.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
],
136136

137137
'repositories' => [
138+
138139
/*
139140
| Specify either to serialize index meta (policy) information or not. For performance reasons we recommend disabling it.
140141
*/

docs-v2/content/en/api/repositories-advanced.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,33 @@ public static function collectMiddlewares(RestifyRequest $request): ?Collection
146146
}
147147
```
148148

149+
## Repository registration
150+
151+
Laravel Restify registers all repositories automatically in the App namespace. However, you can register your own repositories from any service provider using the InteractsWithRestifyRepositories trait. Here's an example:
152+
153+
```php
154+
<?php
155+
156+
namespace MyPackage\Cart;
157+
158+
use Binaryk\LaravelRestify\Traits\InteractsWithRestifyRepositories;
159+
use Illuminate\Support\ServiceProvider;
160+
161+
class MyPackageCart extends ServiceProvider
162+
{
163+
use InteractsWithRestifyRepositories;
164+
165+
public function register(): void
166+
{
167+
$this->loadRestifyFrom(__DIR__.'/Restify', __NAMESPACE__.'\\Restify\\');
168+
169+
// The rest of your package's registration code goes here.
170+
}
171+
}
172+
```
173+
174+
If you want to load Restify from your own service provider, you must use the InteractsWithRestifyRepositories trait in the service provider class. The loadRestifyFrom method takes the path to the directory containing the repositories and the namespace under which the repositories will be registered.
175+
149176
## Dependency injection
150177

151178
The Laravel [service container](https://laravel.com/docs/7.x/container) is used to resolve all the Laravel Restify

src/Restify.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,13 @@ public static function repositories(array $repositories)
136136
}
137137

138138
/**
139-
* Register all of the repository classes in the given directory.
140-
*
139+
* Register all repository classes in the given directory and namespace.
141140
*
142141
* @throws ReflectionException
143142
*/
144-
public static function repositoriesFrom(string $directory): void
143+
public static function repositoriesFrom(string $directory, string $namespace): void
145144
{
146-
$namespace = app()->getNamespace();
147-
145+
$basePath = $namespace === 'App\\' ? app_path() : $directory;
148146
$repositories = [];
149147

150148
if (! is_dir($directory)) {
@@ -155,7 +153,7 @@ public static function repositoriesFrom(string $directory): void
155153
$repository = $namespace.str_replace(
156154
['/', '.php'],
157155
['\\', ''],
158-
Str::after($repository->getPathname(), app_path().DIRECTORY_SEPARATOR)
156+
Str::after($repository->getPathname(), $basePath.DIRECTORY_SEPARATOR)
159157
);
160158

161159
if (is_subclass_of(
@@ -280,7 +278,7 @@ public static function isRestify(Request $request): bool
280278
public static function ensureRepositoriesLoaded(): void
281279
{
282280
if (empty(static::$repositories)) {
283-
static::repositoriesFrom(app_path('Restify'));
281+
static::repositoriesFrom(app_path('Restify'), app()->getNamespace());
284282
}
285283
}
286284
}

src/RestifyApplicationServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function boot()
3939
*/
4040
protected function repositories(): void
4141
{
42-
Restify::repositoriesFrom(app_path('Restify'));
42+
Restify::repositoriesFrom(app_path('Restify'), app()->getNamespace());
4343
}
4444

4545
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Traits;
4+
5+
use Binaryk\LaravelRestify\Restify;
6+
use ReflectionException;
7+
8+
trait InteractsWithRestifyRepositories
9+
{
10+
/**
11+
* Register the application's Rest resources.
12+
*
13+
* @throws ReflectionException
14+
*/
15+
protected function loadRestifyFrom(string $directory, string $namespace): void
16+
{
17+
Restify::repositoriesFrom(
18+
directory: $directory,
19+
namespace: $namespace,
20+
);
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Concerns;
4+
5+
trait WithRepositoriesDataProvider
6+
{
7+
public static function repositoryPathsFromFixtures(): array
8+
{
9+
return [
10+
[
11+
'directory' => realpath(__DIR__.'/../Fixtures/CustomNamespace/PackageA/Restify'),
12+
'namespace' => 'CustomNamespace\\PackageA\\Restify\\',
13+
'serviceProvider' => 'CustomNamespace\\PackageA\\PackageAServiceProvider',
14+
],
15+
[
16+
'directory' => realpath(__DIR__.'/../Fixtures/CustomNamespace/PackageB/Restify'),
17+
'namespace' => 'CustomNamespace\\PackageB\\Restify\\',
18+
'serviceProvider' => 'CustomNamespace\\PackageB\\PackageBServiceProvider',
19+
],
20+
[
21+
'directory' => realpath(__DIR__.'/../Fixtures/CustomNamespace/PackageC/Restify'),
22+
'namespace' => 'CustomNamespace\\PackageC\\Restify\\',
23+
'serviceProvider' => 'CustomNamespace\\PackageC\\PackageCServiceProvider',
24+
],
25+
];
26+
}
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Controllers;
4+
5+
use Binaryk\LaravelRestify\Restify;
6+
use Binaryk\LaravelRestify\Tests\Concerns\WithRepositoriesDataProvider;
7+
use Binaryk\LaravelRestify\Tests\IntegrationTestCase;
8+
9+
class RepositoryLoadedFromServiceProviderTest extends IntegrationTestCase
10+
{
11+
use WithRepositoriesDataProvider;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
Restify::$repositories = [];
18+
}
19+
20+
protected function tearDown(): void
21+
{
22+
parent::tearDown();
23+
}
24+
25+
/** @dataProvider repositoryPathsFromFixtures */
26+
public function test_repositories_can_be_loaded_with_service_provider_register_method(
27+
string $directory,
28+
string $namespace,
29+
?string $serviceProvider = null,
30+
): void
31+
{
32+
if (!$serviceProvider) {
33+
$this->markTestSkipped('No service provider was found in directory '.$directory.' skipping this iteration.');
34+
}
35+
36+
$this->app->register($serviceProvider);
37+
38+
foreach (Restify::$repositories as $repository) {
39+
$route = $repository::route();
40+
$this->getJson($route)->assertOk();
41+
}
42+
43+
// Clears repositories so it does not affect other tests.
44+
Restify::$repositories = [];
45+
}
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Restify;
4+
5+
use App\User;
6+
7+
class UserRepository extends \Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository
8+
{
9+
public static string $model = User::class;
10+
}

tests/Fixtures/App/User.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class User extends \Binaryk\LaravelRestify\Tests\Fixtures\User\User
6+
{
7+
8+
}

0 commit comments

Comments
 (0)