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
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')
Comment thread
diimpp marked this conversation as resolved.
->prototype('scalar')->end()
->end()
->arrayNode('paths')
->prototype('scalar')->end()
->end()
Expand Down
43 changes: 43 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 @@ public function load(array $configs, ContainerBuilder $container): void
$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,43 @@ private function loadResources(array $loadedResources, ContainerBuilder $contain
}
}
}

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

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

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

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

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

continue;
}

if ($container->fileExists($path, false)) {
if (!str_ends_with($path, '.php')) {
throw new RuntimeException(\sprintf('Unsupported mapping type in "%s", supported type is PHP.', $path));
}

$files[] = $path;

continue;
}

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

return $files;
}
}
8 changes: 8 additions & 0 deletions src/Bundle/Resources/config/services/metadata/extractor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<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" public="false">
<argument type="collection" />
<argument type="service" id="service_container" />
</service>
</services>
</container>
2 changes: 2 additions & 0 deletions tests/Bundle/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function it_has_no_default_mapping_paths(): void
],
[
'mapping' => [
'imports' => [],
'paths' => [],
],
],
Expand All @@ -89,6 +90,7 @@ public function its_mapping_paths_can_be_customized(): void
],
[
'mapping' => [
'imports' => [],
'paths' => [
'path/to/resources',
],
Expand Down
31 changes: 31 additions & 0 deletions tests/Bundle/DependencyInjection/SyliusResourceExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function it_registers_parameter_for_paths(): void
]);

$this->assertContainerBuilderHasParameter('sylius.resource.mapping', [
'imports' => [],
'paths' => [
__DIR__ . '/Dummy',
],
Expand Down Expand Up @@ -181,6 +182,36 @@ public function it_registers_doctrine_related_services_when_doctrine_is_availabl
$this->assertContainerBuilderHasService(RemoveProcessor::class);
}

public function testItRegistersMetadataConfigurationWithADirectoryAsImportPath(): void
{
$this->load([
'mapping' => [
'imports' => [
__DIR__ . '/php',
],
],
]);

$emptyPhpFile = realpath(__DIR__ . '/php/empty_file.php');
$this->assertContainerBuilderHasService('sylius.metadata.resource_extractor.php_file');
$this->assertSame([$emptyPhpFile], $this->container->getDefinition('sylius.metadata.resource_extractor.php_file')->getArgument(0));
}

public function testItRegistersMetadataConfigurationWithAFileAsImportPath(): void
{
$this->load([
'mapping' => [
'imports' => [
__DIR__ . '/php/empty_file.php',
],
],
]);

$emptyPhpFile = realpath(__DIR__ . '/php/empty_file.php');
$this->assertContainerBuilderHasService('sylius.metadata.resource_extractor.php_file');
$this->assertSame([$emptyPhpFile], $this->container->getDefinition('sylius.metadata.resource_extractor.php_file')->getArgument(0));
}

protected function getContainerExtensions(): array
{
$this->setParameter('kernel.bundles', []);
Expand Down
12 changes: 12 additions & 0 deletions tests/Bundle/DependencyInjection/php/empty_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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);
Loading