Skip to content
Closed
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
3 changes: 3 additions & 0 deletions src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('mapping')
->addDefaultsIfNotSet()
->children()
->arrayNode('imports')
->prototype('scalar')->end()
->end()
->arrayNode('paths')
->prototype('scalar')->end()
->end()
Expand Down
38 changes: 38 additions & 0 deletions src/Bundle/DependencyInjection/SyliusResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
use Sylius\Resource\Twig\Context\Factory\ContextFactoryInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\Finder\Finder;
use function Symfony\Component\String\u;

final class SyliusResourceExtension extends Extension implements PrependExtensionInterface
Expand Down Expand Up @@ -66,6 +69,7 @@
$container->setParameter('sylius.resource.settings', $config['settings']);
$container->setAlias('sylius.resource_controller.authorization_checker', $config['authorization_checker']);

$this->registerMetadataConfiguration($container, $config);
$this->autoRegisterResources($config, $container);

$this->loadPersistence($config['drivers'], $config['resources'], $loader, $container);
Expand Down Expand Up @@ -320,4 +324,38 @@
}
}
}

private function registerMetadataConfiguration(ContainerBuilder $container, array $config): void
{
$resources = $this->getResourcesToWatch($container, $config);

$container->getDefinition('sylius.metadata.resource_extractor.php_file')->replaceArgument(0, $resources);
}

private function getResourcesToWatch(ContainerBuilder $container, array $config): array
{
$resources = [];

foreach ($config['mapping']['imports'] ?? [] as $path) {
if (is_dir($path)) {
foreach (Finder::create()->followLinks()->files()->in($path)->name('/\.php$/')->sortByName() as $file) {
$resources[] = $file->getRealPath();
}

$container->addResource(new DirectoryResource($path, '/\.php$/'));

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 2.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 2.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 2.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 3.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 3.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 2.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 2.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 3.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 3.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

Check failure on line 345 in src/Bundle/DependencyInjection/SyliusResourceExtension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 3.*

PossiblyInvalidArgument

src/Bundle/DependencyInjection/SyliusResourceExtension.php:345:63: PossiblyInvalidArgument: Argument 1 of Symfony\Component\Config\Resource\DirectoryResource::__construct expects string, but possibly different type array<array-key, mixed>|string provided (see https://psalm.dev/092)

continue;
}

if ($container->fileExists($path, false)) {
$resources[] = $path;

continue;
}

throw new RuntimeException(\sprintf('Could not open file or directory "%s".', $path));
}

return $resources;
}
}
23 changes: 23 additions & 0 deletions src/Bundle/Resources/config/services/metadata/extractor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--

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.

-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.metadata.resource_extractor.php_file"
class="Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor"
>
<argument type="collection" />
<argument type="service" id="service_container" />
</service>
</services>
</container>
120 changes: 120 additions & 0 deletions src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?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 Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;

/**
* Base file extractor.
*/
abstract class AbstractResourceExtractor implements ResourceExtractorInterface
{
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->extractPath($path);
}

return $this->resources;
}

/**
* Extracts metadata from a given path.
*/
abstract protected function extractPath(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);

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 2.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 2.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 2.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 3.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 3.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 2.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 2.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 3.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 3.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)

Check failure on line 106 in src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 3.*

PossiblyNullReference

src/Component/src/Metadata/Extractor/AbstractResourceExtractor.php:106:47: PossiblyNullReference: Cannot call method get on possibly null value (see https://psalm.dev/083)
}

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 src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php
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;

final class PhpFileResourceExtractor extends AbstractResourceExtractor
{
/**
* @inheritdoc
*/
protected function extractPath(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

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 2.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 2.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 2.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 3.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 3.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 2.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 2.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 3.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 3.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)

Check failure on line 47 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 3.*

InvalidNullableReturnType

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:47:59: InvalidNullableReturnType: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but 'Closure|null' contains null (see https://psalm.dev/144)
{
return \Closure::bind(function () use ($filePath): mixed {

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 2.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 2.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 2.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 3.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 3.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 2.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 2.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 3.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 3.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)

Check failure on line 49 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 3.*

NullableReturnStatement

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:49:16: NullableReturnStatement: The declared return type 'Closure' for Sylius\Resource\Metadata\Extractor\PhpFileResourceExtractor::getPHPFileClosure is not nullable, but the function returns 'Closure|null' (see https://psalm.dev/139)
return require $filePath;

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 2.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 2.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 2.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^7.1, ORM 3.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 3.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^6.4, ORM 2.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 2.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2, Symfony ^7.1, ORM 3.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1, Symfony ^6.4, ORM 3.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)

Check failure on line 50 in src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php

View workflow job for this annotation

GitHub Actions / PHP 8.3, Symfony ^6.4, ORM 3.*

UnresolvableInclude

src/Component/src/Metadata/Extractor/PhpFileResourceExtractor.php:50:20: UnresolvableInclude: Cannot resolve the given expression to a file path (see https://psalm.dev/106)
}, null, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\Exception\InvalidArgumentException;

/**
* Extracts an array of metadata from a file or a list of files.
*/
interface ResourceExtractorInterface
{
/**
* Parses all metadata files and convert them in an array.
*
* @throws InvalidArgumentException
*/
public function getResources(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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;
use Sylius\Resource\Tests\Dummy\PullRequest;

final class PhpFileResourceExtractorTest extends TestCase
{
public function testItGetsResourcesFromPhpFileThatReturnsResourceMetadata(): void
{
$extractor = new PhpFileResourceExtractor([__DIR__ . '/php/valid_php_file.php']);

$expectedResource = $this->createPullRequestResourceMetadata();

$this->assertEquals([$expectedResource], $extractor->getResources());
}

public function testItExcludesResourcesFromPhpFileThatDoesNotReturnResourceMetadata(): void
{
$extractor = new PhpFileResourceExtractor([__DIR__ . '/php/invalid_php_file.php']);

$this->assertEquals([], $extractor->getResources());
}

private function createPullRequestResourceMetadata(): ResourceMetadata
{
return (new ResourceMetadata())
->withClass(PullRequest::class)
;
}
}
16 changes: 16 additions & 0 deletions src/Component/tests/Metadata/Extractor/php/invalid_php_file.php
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();
19 changes: 19 additions & 0 deletions src/Component/tests/Metadata/Extractor/php/valid_php_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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;
use Sylius\Resource\Tests\Dummy\PullRequest;

return (new ResourceMetadata())
->withClass(PullRequest::class)
;
2 changes: 2 additions & 0 deletions tests/Application/config/sylius/resources.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
sylius_resource:
mapping:
imports:
- '%kernel.project_dir%/config/sylius/resources'
paths:
- '%kernel.project_dir%/src/BoardGameBlog/Infrastructure/Sylius/Resource'
- '%kernel.project_dir%/src/Subscription/Entity'
Expand Down
Loading
Loading