-
-
Notifications
You must be signed in to change notification settings - Fork 164
[Metadata] Add PHP file resource extractor #1004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
loic425
merged 4 commits into
Sylius:external-routing-files
from
loic425:feat/php-file-resource-extractor
Jun 4, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
125 changes: 125 additions & 0 deletions
125
src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php
This file contains hidden or 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,125 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\Resource\Metadata\Extractor; | ||
|
|
||
| use Psr\Container\ContainerInterface; | ||
| use Sylius\Resource\Exception\RuntimeException; | ||
| use Sylius\Resource\Metadata\ResourceMetadata; | ||
| use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; | ||
|
|
||
| /** | ||
| * Base file extractor. | ||
| * | ||
| * @experimental | ||
| */ | ||
| abstract class AbstractResourceExtractor implements ResourceExtractorInterface | ||
| { | ||
| /** @var ResourceMetadata[]|null */ | ||
| protected ?array $resources = null; | ||
|
|
||
| private array $collectedParameters = []; | ||
|
|
||
| /** @param string[] $paths */ | ||
| public function __construct( | ||
| protected array $paths, | ||
| private readonly ?ContainerInterface $container = null, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function getResources(): array | ||
| { | ||
| if (null !== $this->resources) { | ||
| return $this->resources; | ||
| } | ||
|
|
||
| $this->resources = []; | ||
| foreach ($this->paths as $path) { | ||
| $this->extractFromPath($path); | ||
| } | ||
|
|
||
| return $this->resources; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts metadata from a given path. | ||
| */ | ||
| abstract protected function extractFromPath(string $path): void; | ||
|
|
||
| /** | ||
| * Recursively replaces placeholders with the service container parameters. | ||
| * | ||
| * @see https://github.com/symfony/symfony/blob/6fec32c/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php | ||
| * | ||
| * @param mixed $value The source which might contain "%placeholders%" | ||
| * | ||
| * @throws \RuntimeException When a container value is not a string or a numeric value | ||
| * | ||
| * @return mixed The source with the placeholders replaced by the container | ||
| * parameters. Arrays are resolved recursively. | ||
| */ | ||
| protected function resolve(mixed $value): mixed | ||
| { | ||
| if (null === $this->container) { | ||
| return $value; | ||
| } | ||
|
|
||
| if (\is_array($value)) { | ||
| foreach ($value as $key => $val) { | ||
| $value[$key] = $this->resolve($val); | ||
| } | ||
|
|
||
| return $value; | ||
| } | ||
|
|
||
| if (!\is_string($value)) { | ||
| return $value; | ||
| } | ||
|
|
||
| $escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($value) { | ||
| $parameter = $match[1] ?? null; | ||
|
|
||
| // skip %% | ||
| if (!isset($parameter)) { | ||
| return '%%'; | ||
| } | ||
|
|
||
| if (preg_match('/^env\(\w+\)$/', $parameter)) { | ||
| throw new RuntimeException(\sprintf('Using "%%%s%%" is not allowed in routing configuration.', $parameter)); | ||
| } | ||
|
|
||
| if (\array_key_exists($parameter, $this->collectedParameters)) { | ||
| return $this->collectedParameters[$parameter]; | ||
| } | ||
|
|
||
| if ($this->container instanceof SymfonyContainerInterface) { | ||
| $resolved = $this->container->getParameter($parameter); | ||
| } else { | ||
| $resolved = $this->container?->get($parameter); | ||
| } | ||
|
|
||
| if (\is_string($resolved) || is_numeric($resolved)) { | ||
| $this->collectedParameters[$parameter] = $resolved; | ||
|
|
||
| return (string) $resolved; | ||
| } | ||
|
|
||
| throw new RuntimeException(\sprintf('The container parameter "%s", used in the resource configuration value "%s", must be a string or numeric, but it is of type %s.', $parameter, $value, \gettype($resolved))); | ||
| }, $value); | ||
|
|
||
| return str_replace('%%', '%', $escapedValue ?? ''); | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php
This file contains hidden or 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,53 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\Resource\Metadata\Extractor; | ||
|
|
||
| use Sylius\Resource\Metadata\ResourceMetadata; | ||
|
|
||
| /** | ||
| * @experimental | ||
| */ | ||
| final class PhpFileResourceExtractor extends AbstractResourceExtractor | ||
| { | ||
| protected function extractFromPath(string $path): void | ||
| { | ||
| $resource = $this->getPHPFileClosure($path)(); | ||
|
|
||
| if (!$resource instanceof ResourceMetadata) { | ||
| return; | ||
| } | ||
|
|
||
| $resourceReflection = new \ReflectionClass($resource); | ||
|
|
||
| foreach ($resourceReflection->getProperties() as $property) { | ||
| $property->setAccessible(true); | ||
| $resolvedValue = $this->resolve($property->getValue($resource)); | ||
| $property->setValue($resource, $resolvedValue); | ||
| } | ||
|
|
||
| $this->resources[] = $resource; | ||
| } | ||
|
|
||
| /** | ||
| * Scope isolated include. | ||
| * | ||
| * Prevents access to $this/self from included files. | ||
| */ | ||
| private function getPHPFileClosure(string $filePath): \Closure | ||
| { | ||
| return \Closure::bind(function () use ($filePath): mixed { | ||
| return require $filePath; | ||
| }, null, null); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/Component/src/Metadata/Extractor/ResourceExtractorInterface.php
This file contains hidden or 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,31 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\Resource\Metadata\Extractor; | ||
|
|
||
| use Sylius\Resource\Metadata\ResourceMetadata; | ||
|
|
||
| /** | ||
| * Extracts an array of metadata from a file or a list of files. | ||
| * | ||
| * @experimental | ||
| */ | ||
| interface ResourceExtractorInterface | ||
| { | ||
| /** | ||
| * Parses all metadata files and convert them in an array. | ||
diimpp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @return ResourceMetadata[] | ||
| */ | ||
| public function getResources(): array; | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
src/Component/tests/Metadata/Extractor/PhpFileResourceExtractorTest.php
This file contains hidden or 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,37 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\Resource\Tests\Metadata\Extractor; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor; | ||
| use Sylius\Resource\Metadata\ResourceMetadata; | ||
|
|
||
| final class PhpFileResourceExtractorTest extends TestCase | ||
| { | ||
| public function testItGetsResourcesFromPhpFileThatReturnsResourceMetadata(): void | ||
| { | ||
| $extractor = new PhpFileResourceExtractor([__DIR__ . '/php/valid_php_file.php', __DIR__ . '/php/another_valid_php_file.php']); | ||
|
|
||
| $expectedResources = [new ResourceMetadata(alias: 'dummy'), new ResourceMetadata(alias: 'another_dummy')]; | ||
|
|
||
| $this->assertEquals($expectedResources, $extractor->getResources()); | ||
| } | ||
|
|
||
| public function testItExcludesResourcesFromPhpFileThatDoesNotReturnResourceMetadata(): void | ||
| { | ||
| $extractor = new PhpFileResourceExtractor([__DIR__ . '/php/invalid_php_file.php']); | ||
|
|
||
| $this->assertEquals([], $extractor->getResources()); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/Component/tests/Metadata/Extractor/php/another_valid_php_file.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Sylius\Resource\Metadata\ResourceMetadata; | ||
|
|
||
| return new ResourceMetadata(alias: 'another_dummy'); |
16 changes: 16 additions & 0 deletions
16
src/Component/tests/Metadata/Extractor/php/invalid_php_file.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Sylius\Resource\Tests\Dummy\PullRequest; | ||
|
|
||
| return new PullRequest(); |
16 changes: 16 additions & 0 deletions
16
src/Component/tests/Metadata/Extractor/php/valid_php_file.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Sylius package. | ||
| * | ||
| * (c) Sylius Sp. z o.o. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Sylius\Resource\Metadata\ResourceMetadata; | ||
|
|
||
| return new ResourceMetadata(alias: 'dummy'); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.