Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,14 @@ public function register(): void
return new SerializerClassMetadataFactory($app->make(ClassMetadataFactoryInterface::class));
});

$this->app->bind(PathSegmentNameGeneratorInterface::class, UnderscorePathSegmentNameGenerator::class);
$this->app->bind(PathSegmentNameGeneratorInterface::class, static function (Application $app): PathSegmentNameGeneratorInterface {
/** @var ConfigRepository */
$config = $app['config'];
/** @var class-string<PathSegmentNameGeneratorInterface> $class */
$class = $config->get('api-platform.path_segment_name_generator') ?? UnderscorePathSegmentNameGenerator::class;

return $app->make($class);
});

$this->app->singleton(ResourceNameCollectionFactoryInterface::class, static function (Application $app) {
/** @var ConfigRepository */
Expand Down
52 changes: 52 additions & 0 deletions src/Laravel/Tests/PathSegmentNameGeneratorDashTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Metadata\Operation\DashPathSegmentNameGenerator;
use ApiPlatform\Metadata\Operation\PathSegmentNameGeneratorInterface;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Foundation\Application;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

final class PathSegmentNameGeneratorDashTest extends TestCase
{
use WithWorkbench;

/**
* @param Application $app
*/
protected function defineEnvironment($app): void
{
tap($app->make('config'), static function (Repository $config): void {
$config->set('api-platform.path_segment_name_generator', DashPathSegmentNameGenerator::class);
$config->set('app.debug', true);
});
}

public function testConfigOverrideBindingResolvesToDashGenerator(): void
{
$generator = $this->app->make(PathSegmentNameGeneratorInterface::class);

$this->assertInstanceOf(DashPathSegmentNameGenerator::class, $generator);
}

public function testRouteUsesDashForMultiWordResource(): void
{
$segments = PathSegmentNameGeneratorTest::collectApiRouteSegments($this->app);

$this->assertContains('api/product-orders', $segments);
$this->assertNotContains('api/product_orders', $segments);
}
}
58 changes: 58 additions & 0 deletions src/Laravel/Tests/PathSegmentNameGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Metadata\Operation\PathSegmentNameGeneratorInterface;
use ApiPlatform\Metadata\Operation\UnderscorePathSegmentNameGenerator;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Routing\Router;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

final class PathSegmentNameGeneratorTest extends TestCase
{
use WithWorkbench;

public function testDefaultBindingResolvesToUnderscoreGenerator(): void
{
$generator = $this->app->make(PathSegmentNameGeneratorInterface::class);

$this->assertInstanceOf(UnderscorePathSegmentNameGenerator::class, $generator);
}

public function testDefaultRouteUsesUnderscoreForMultiWordResource(): void
{
$segments = self::collectApiRouteSegments($this->app);

$this->assertContains('api/product_orders', $segments);
}

/**
* @return list<string>
*/
public static function collectApiRouteSegments(Application $app): array
{
/** @var Router $router */
$router = $app->make(Router::class);

$segments = [];
foreach ($router->getRoutes()->getRoutes() as $route) {
$uri = $route->uri();
$uri = preg_replace('/\{[^}]+\}/', '', $uri);
$segments[] = rtrim($uri, '/');
}

return $segments;
}
}
9 changes: 8 additions & 1 deletion src/Laravel/config/api-platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

declare(strict_types=1);

use ApiPlatform\Metadata\Operation\UnderscorePathSegmentNameGenerator;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
Expand Down Expand Up @@ -154,10 +155,16 @@

// 'openapi' => [
// 'tags' => [],
// ],
// ],

'url_generation_strategy' => UrlGeneratorInterface::ABS_PATH,

// Class implementing PathSegmentNameGeneratorInterface used to derive route
// segments from resource short names (e.g. `ProductOrder` -> `product_orders`).
// Set to DashPathSegmentNameGenerator::class for dasherized segments
// (e.g. `product-orders`).
'path_segment_name_generator' => UnderscorePathSegmentNameGenerator::class,

'serializer' => [
'hydra_prefix' => false,
// 'datetime_format' => \DateTimeInterface::RFC3339,
Expand Down
Loading