Skip to content

Commit

Permalink
Rename UrlRoutable method to not conflict with laravel UrlRoutable (#558
Browse files Browse the repository at this point in the history
)

Fixes #451
  • Loading branch information
eigan authored Aug 14, 2023
1 parent e4529c8 commit 8e6d22e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 41 deletions.
3 changes: 3 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ If you have been setting "driverOptions" on your MySQL database config, you shou
## Metadata driver `config` removed
Used deprecated YamlDriver and was not supported by doctrine.

## UrlRoutable::getRouteKeyName renamed to getRouteKeyNameStatic
This method was renamed to not conflict with the UrlRoutable trait of Laravel.

## Logging configuration changed
DBAL deprecated the SQLLogger functionality in favor of the new middleware functionality.
Logging moved to the new middlewares section.
Expand Down
30 changes: 0 additions & 30 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1178,36 +1178,11 @@ parameters:
count: 1
path: src/Loggers/Logger.php

-
message: "#^If condition is always true\\.$#"
count: 1
path: src/Middleware/SubstituteBindings.php

-
message: "#^Method LaravelDoctrine\\\\ORM\\\\Middleware\\\\SubstituteBindings\\:\\:substituteImplicitBindings\\(\\) has no return type specified\\.$#"
count: 1
path: src/Middleware/SubstituteBindings.php

-
message: "#^Parameter \\#1 \\$callback of function call_user_func expects callable\\(\\)\\: mixed, array\\{string\\|null, 'getRouteKeyName'\\} given\\.$#"
count: 1
path: src/Middleware/SubstituteBindings.php

-
message: "#^Parameter \\#1 \\$criteria of method Doctrine\\\\Persistence\\\\ObjectRepository\\<object\\>\\:\\:findOneBy\\(\\) expects array\\<string, mixed\\>, array\\<int\\|string, mixed\\> given\\.$#"
count: 1
path: src/Middleware/SubstituteBindings.php

-
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string\\|null given\\.$#"
count: 1
path: src/Middleware/SubstituteBindings.php

-
message: "#^Parameter \\#1 \\$persistentObject of method Doctrine\\\\Persistence\\\\ManagerRegistry\\:\\:getRepository\\(\\) expects class\\-string\\<object\\>, string\\|null given\\.$#"
count: 1
path: src/Middleware/SubstituteBindings.php

-
message: "#^Parameter \\#1 \\$route of method Illuminate\\\\Contracts\\\\Routing\\\\Registrar\\:\\:substituteBindings\\(\\) expects Illuminate\\\\Routing\\\\Route, object\\|string\\|null given\\.$#"
count: 1
Expand All @@ -1218,11 +1193,6 @@ parameters:
count: 1
path: src/Middleware/SubstituteBindings.php

-
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\Persistence\\\\ManagerRegistry\\:\\:getRepository\\(\\)$#"
count: 1
path: src/Middleware/SubstituteBindings.php

-
message: "#^Cannot call method routeNotificationFor\\(\\) on class\\-string\\|object\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/UrlRoutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface UrlRoutable
{
public static function getRouteKeyName(): string;
public static function getRouteKeyNameStatic(): string;
}
15 changes: 10 additions & 5 deletions src/Middleware/SubstituteBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Routing\Route;
use LaravelDoctrine\ORM\Contracts\UrlRoutable;
use ReflectionParameter;
use function class_exists;
use function is_a;

class SubstituteBindings
{
Expand Down Expand Up @@ -67,11 +69,11 @@ protected function substituteImplicitBindings(Route $route)
$id = $parameters[$parameter->name];
$class = $this->getClassName($parameter);

if ($repository = $this->registry->getRepository($class)) {
$reflectionClass = new \ReflectionClass($class);
if ($class) {
$repository = $this->registry->getRepository($class);

if ($reflectionClass->implementsInterface(UrlRoutable::class)) {
$name = call_user_func([$class, 'getRouteKeyName']);
if (is_a($class, UrlRoutable::class, true)) {
$name = call_user_func([$class, 'getRouteKeyNameStatic']);

$entity = $repository->findOneBy([
$name => $id
Expand Down Expand Up @@ -105,6 +107,9 @@ private function signatureParameters(Route $route)
})->toArray();
}

/**
* @return class-string
*/
private function getClassName(ReflectionParameter $parameter): ?string
{
$class = null;
Expand All @@ -113,6 +118,6 @@ private function getClassName(ReflectionParameter $parameter): ?string
$class = $type->getName();
}

return $class;
return class_exists($class) ? $class : null;
}
}
10 changes: 5 additions & 5 deletions tests/Middleware/SubstituteBindingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function test_entity_binding()
$router = $this->getRouter();
$router->get('foo/{entity}', [
'middleware' => SubstituteBindings::class,
'uses' => 'EntityController@returnEntityName',
'uses' => 'EntityController@returnEntityName',
]);

$this->mockRegistry();
Expand All @@ -85,7 +85,7 @@ public function test_entity_binding_expect_entity_not_found_exception()

$router->get('foo/{entity}', [
'middleware' => SubstituteBindings::class,
'uses' => 'EntityController@returnEntityName',
'uses' => 'EntityController@returnEntityName',
]);

$this->mockRegistry();
Expand All @@ -99,7 +99,7 @@ public function test_entity_binding_get_null_entity()
$router = $this->getRouter();
$router->get('foo/{entity}', [
'middleware' => SubstituteBindings::class,
'uses' => 'EntityController@returnEntity',
'uses' => 'EntityController@returnEntity',
]);

$this->mockRegistry();
Expand All @@ -120,7 +120,7 @@ public function test_binding_value()

$router->get('doc/trine', [
'middleware' => SubstituteBindings::class,
'uses' => 'EntityController@checkRequest',
'uses' => 'EntityController@checkRequest',
]);

$this->assertEquals('request', $router->dispatch(Request::create('doc/trine', 'GET'))->getContent());
Expand Down Expand Up @@ -225,7 +225,7 @@ public function getName()
return strtolower($this->name);
}

public static function getRouteKeyName(): string
public static function getRouteKeyNameStatic(): string
{
return 'name';
}
Expand Down

0 comments on commit 8e6d22e

Please sign in to comment.