forked from schmittjoh/object-routing
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a dedicated AttributeDriver, deprecate annotations (#9)
This PR prepares the removal of annotations support, which helps to prepare https://github.com/webfactory/BGObjectRoutingBundle for Symfony 7. symfony/framework-bundle 7.x does not ship the `annotation_reader` anymore, and instead of wiring up an annotation reader ourselves, I'd prefer to go down the attributes path.
- Loading branch information
Showing
8 changed files
with
169 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright 2013 Johannes M. Schmitt <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace JMS\ObjectRouting\Attribute; | ||
|
||
/** @final */ | ||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS)] | ||
class ObjectRoute | ||
{ | ||
/** @var string @Required */ | ||
public $type; | ||
|
||
/** @var string @Required */ | ||
public $name; | ||
|
||
/** @var array */ | ||
public $params = array(); | ||
|
||
public function __construct(string $type, string $name, array $params = []) | ||
{ | ||
$this->type = $type; | ||
$this->name = $name; | ||
$this->params = $params; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* Copyright 2013 Johannes M. Schmitt <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace JMS\ObjectRouting\Metadata\Driver; | ||
|
||
use JMS\ObjectRouting\Attribute\ObjectRoute; | ||
use JMS\ObjectRouting\Metadata\ClassMetadata; | ||
use Metadata\Driver\DriverInterface; | ||
|
||
class AttributeDriver implements DriverInterface | ||
{ | ||
public function loadMetadataForClass(\ReflectionClass $class): ?ClassMetadata | ||
{ | ||
$metadata = new ClassMetadata($class->name); | ||
|
||
$hasMetadata = false; | ||
foreach ($this->fetchAttributes($class) as $attribute) { | ||
$hasMetadata = true; | ||
$metadata->addRoute($attribute->type, $attribute->name, $attribute->params); | ||
} | ||
|
||
return $hasMetadata ? $metadata : null; | ||
} | ||
|
||
private function fetchAttributes(\ReflectionClass $class): array | ||
{ | ||
$attributes = []; | ||
|
||
foreach ($class->getAttributes(ObjectRoute::class) as $attr) { | ||
$attributes[] = $attr->newInstance(); | ||
} | ||
|
||
return $attributes; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/JMS/Tests/ObjectRouting/Metadata/Driver/AttributeDriverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace JMS\Tests\ObjectRouting\Metadata\Driver; | ||
|
||
use JMS\ObjectRouting\Metadata\Driver\AttributeDriver; | ||
use JMS\Tests\ObjectRouting\Metadata\Driver\Fixture\BlogPostWithAttributes; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AttributeDriverTest extends TestCase | ||
{ | ||
private readonly AttributeDriver $driver; | ||
|
||
public function testLoad() | ||
{ | ||
$metadata = $this->driver->loadMetadataForClass(new \ReflectionClass(BlogPostWithAttributes::class)); | ||
$this->assertCount(2, $metadata->routes); | ||
|
||
$routes = [ | ||
'view' => ['name' => 'blog_post_view', 'params' => ['slug' => 'slug']], | ||
'edit' => ['name' => 'blog_post_edit', 'params' => ['slug' => 'slug']], | ||
]; | ||
$this->assertEquals($routes, $metadata->routes); | ||
} | ||
|
||
public function testLoadReturnsNullWhenNoRoutes() | ||
{ | ||
$this->assertNull($this->driver->loadMetadataForClass(new \ReflectionClass('stdClass'))); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->driver = new AttributeDriver(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/JMS/Tests/ObjectRouting/Metadata/Driver/Fixture/BlogPostWithAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace JMS\Tests\ObjectRouting\Metadata\Driver\Fixture; | ||
|
||
use JMS\ObjectRouting\Attribute\ObjectRoute; | ||
|
||
#[ObjectRoute(type: "view", name: "blog_post_view", params: ['slug' => 'slug'])] | ||
#[ObjectRoute(type: "edit", name: "blog_post_edit", params: ['slug' => 'slug'])] | ||
class BlogPostWithAttributes | ||
{ | ||
private $slug; | ||
|
||
public function __construct($slug) | ||
{ | ||
$this->slug = $slug; | ||
} | ||
|
||
public function getSlug() | ||
{ | ||
return $this->slug; | ||
} | ||
} |