Skip to content

Commit

Permalink
Merge pull request #1776 from dpfaffenbauer/features/eaiser-custom-re…
Browse files Browse the repository at this point in the history
…sources
  • Loading branch information
dpfaffenbauer authored Nov 9, 2021
2 parents eddea3f + 3e39aa7 commit 22a62b2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@

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;
use Symfony\Component\DependencyInjection\Definition;
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
{
Expand Down Expand Up @@ -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', [
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 22a62b2

Please sign in to comment.