From 3e39aa71b5bd44227f02f165f87aeb5048d7a284 Mon Sep 17 00:00:00 2001 From: Dominik Pfaffenbauer Date: Tue, 9 Nov 2021 17:09:20 +0100 Subject: [PATCH] [ResourceBundle] allow easier custom resources --- .../DependencyInjection/Configuration.php | 29 ++++++++ .../CoreShopResourceExtension.php | 70 ++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/CoreShop/Bundle/ResourceBundle/DependencyInjection/Configuration.php b/src/CoreShop/Bundle/ResourceBundle/DependencyInjection/Configuration.php index a82322f314..7e15fc9df6 100644 --- a/src/CoreShop/Bundle/ResourceBundle/DependencyInjection/Configuration.php +++ b/src/CoreShop/Bundle/ResourceBundle/DependencyInjection/Configuration.php @@ -16,7 +16,9 @@ use CoreShop\Bundle\ResourceBundle\Controller\ResourceController; use CoreShop\Bundle\ResourceBundle\CoreShopResourceBundle; +use CoreShop\Bundle\ResourceBundle\Pimcore\PimcoreRepository; use CoreShop\Component\Resource\Factory\Factory; +use CoreShop\Component\Resource\Factory\PimcoreFactory; use CoreShop\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; @@ -79,6 +81,33 @@ private function addResourcesSection(ArrayNodeDefinition $node): void ->end() ->end() ->end() + ->arrayNode('pimcore') + ->useAttributeAsKey('name') + ->arrayPrototype() + ->children() + ->variableNode('options')->end() + ->arrayNode('path') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() + ->arrayNode('classes') + ->children() + ->scalarNode('model')->isRequired()->cannotBeEmpty()->end() + ->scalarNode('interface')->cannotBeEmpty()->end() + ->scalarNode('repository')->cannotBeEmpty()->defaultValue(PimcoreRepository::class)->end() + ->scalarNode('factory')->defaultValue(PimcoreFactory::class)->end() + ->scalarNode('install_file')->end() + ->scalarNode('type')->defaultValue(CoreShopResourceBundle::PIMCORE_MODEL_TYPE_OBJECT)->end() + ->arrayNode('pimcore_controller') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() + ->end() + ->end() + ->end() + ->end() + ->end() + ->end() ->end(); } diff --git a/src/CoreShop/Bundle/ResourceBundle/DependencyInjection/CoreShopResourceExtension.php b/src/CoreShop/Bundle/ResourceBundle/DependencyInjection/CoreShopResourceExtension.php index 3258df87f6..2319141ca2 100644 --- a/src/CoreShop/Bundle/ResourceBundle/DependencyInjection/CoreShopResourceExtension.php +++ b/src/CoreShop/Bundle/ResourceBundle/DependencyInjection/CoreShopResourceExtension.php @@ -14,10 +14,14 @@ namespace CoreShop\Bundle\ResourceBundle\DependencyInjection; +use CoreShop\Bundle\PimcoreBundle\DependencyInjection\Extension\AbstractPimcoreExtension; +use CoreShop\Bundle\ResourceBundle\CoreShopResourceBundle; use CoreShop\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterInstallersPass; +use CoreShop\Bundle\ResourceBundle\DependencyInjection\Driver\DriverProvider; use CoreShop\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractModelExtension; use CoreShop\Bundle\ResourceBundle\EventListener\BodyListener; use CoreShop\Bundle\ResourceBundle\Installer\ResourceInstallerInterface; +use CoreShop\Component\Resource\Metadata\Metadata; use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -25,7 +29,7 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -final class CoreShopResourceExtension extends AbstractModelExtension +final class CoreShopResourceExtension extends AbstractPimcoreExtension { public function load(array $configs, ContainerBuilder $container): void { @@ -59,6 +63,9 @@ public function load(array $configs, ContainerBuilder $container): void } $this->loadPersistence($configs['drivers'], $configs['resources'], $loader); + $this->loadResources($configs['resources'], $container); + $this->loadPimcoreModels($configs['pimcore'], $container); + $bodyListener = new Definition(BodyListener::class); $bodyListener->addTag('kernel.event_listener', [ @@ -90,4 +97,65 @@ private function loadPersistence(array $drivers, array $resources, LoaderInterfa $loader->load(sprintf('services/integrations/%s.yml', $driver)); } } + + private function loadResources(array $loadedResources, ContainerBuilder $container): void + { + $resources = $container->hasParameter('coreshop.resources') ? $container->getParameter('coreshop.resources') : []; + + foreach ($loadedResources as $alias => $resourceConfig) { + $metadata = Metadata::fromAliasAndConfiguration($alias, $resourceConfig); + + $resources[$alias] = $resourceConfig; + $container->setParameter('coreshop.resources', $resources); + + DriverProvider::get($metadata)->load($container, $metadata); + + if ($metadata->hasParameter('translation')) { + $alias .= '_translation'; + $resourceConfig = array_merge(['driver' => $resourceConfig['driver']], $resourceConfig['translation']); + + $resources[$alias] = $resourceConfig; + $container->setParameter('coreshop.resources', $resources); + + $metadata = Metadata::fromAliasAndConfiguration($alias, $resourceConfig); + + DriverProvider::get($metadata)->load($container, $metadata); + } + } + } + + protected function loadPimcoreModels(array $models, ContainerBuilder $container): void + { + foreach ($models as $alias => $resourceConfig) { + $resourceConfig['driver'] = CoreShopResourceBundle::DRIVER_PIMCORE; + $resourceConfig['pimcore_class'] = match ($resourceConfig['classes']['type']) { + CoreShopResourceBundle::PIMCORE_MODEL_TYPE_FIELD_COLLECTION => str_replace( + 'Pimcore\Model\DataObject\Fieldcollection\Data\\', + '', + $resourceConfig['classes']['model'] + ), + CoreShopResourceBundle::PIMCORE_MODEL_TYPE_BRICK => str_replace( + 'Pimcore\Model\DataObject\Objectbrick\Data\\', + '', + $resourceConfig['classes']['model'] + ), + default => str_replace( + 'Pimcore\Model\DataObject\\', + '', + $resourceConfig['classes']['model'] + ), + }; + + $metadata = Metadata::fromAliasAndConfiguration($alias, $resourceConfig); + + foreach (['coreshop.all.pimcore_classes', sprintf('%s.pimcore_classes', $metadata->getApplicationName())] as $parameter) { + $resources = $container->hasParameter($parameter) ? $container->getParameter($parameter) : []; + $resources[$alias] = $resourceConfig; + + $container->setParameter($parameter, $resources); + } + + DriverProvider::get($metadata)->load($container, $metadata); + } + } }