diff --git a/Core/API/Handler/LocationResolverHandler.php b/Core/API/Handler/LocationResolverHandler.php new file mode 100644 index 00000000..76cfa2f9 --- /dev/null +++ b/Core/API/Handler/LocationResolverHandler.php @@ -0,0 +1,36 @@ +resolvers[] = $resolver; + } + + /** + * @param string $identifier + * @return int + */ + public function resolve($identifier) + { + foreach ($this->resolvers as $resolver) { + if ($resolver->shouldResolve($identifier)) { + return $resolver->resolve($identifier); + } + } + + return $identifier; + } +} diff --git a/Core/API/LocationResolver/AbstractRepositoryAwareLocationResolver.php b/Core/API/LocationResolver/AbstractRepositoryAwareLocationResolver.php new file mode 100644 index 00000000..543734d0 --- /dev/null +++ b/Core/API/LocationResolver/AbstractRepositoryAwareLocationResolver.php @@ -0,0 +1,23 @@ +repository = $repository; + } +} diff --git a/Core/API/LocationResolver/LocationRemoteIdLocationResolver.php b/Core/API/LocationResolver/LocationRemoteIdLocationResolver.php new file mode 100644 index 00000000..1dc2c626 --- /dev/null +++ b/Core/API/LocationResolver/LocationRemoteIdLocationResolver.php @@ -0,0 +1,52 @@ +remoteId)) { + throw new \Exception('Remote Id was not found, first check if reference should be resolved'); + } + + $location = $this->repository->getLocationService()->loadLocationByRemoteId($this->remoteId); + + if (!$location instanceof Location) { + throw new \Exception(sprintf('Location with remote id "%s" not found', $this->remoteId)); + } + + return $location->id; + } + + /** + * Tests if $reference should be resolved to location id + * + * @param $reference + * @return bool + */ + public function shouldResolve($reference) + { + if (preg_match('/^location_remote_id:(.*)$/', $reference, $match)) { + $this->remoteId = $match[1]; + + return true; + } + + return false; + } +} diff --git a/Core/API/LocationResolver/LocationResolverInterface.php b/Core/API/LocationResolver/LocationResolverInterface.php new file mode 100644 index 00000000..cea2ab1c --- /dev/null +++ b/Core/API/LocationResolver/LocationResolverInterface.php @@ -0,0 +1,22 @@ +getReference($identifier); } + /** + * @return ContainerInterface + */ public function getContainer() { return $this->container; } + /** + * @return LocationResolverHandler + */ + public function getLocationResolverHandler() + { + return $this->container->get('ez_migration_bundle.handler.location_resolver'); + } + /** * Helper method to log in a user that can make changes to the system. */ diff --git a/Core/API/Managers/ContentTypeManager.php b/Core/API/Managers/ContentTypeManager.php index 05b3a46a..0d80b96d 100644 --- a/Core/API/Managers/ContentTypeManager.php +++ b/Core/API/Managers/ContentTypeManager.php @@ -323,7 +323,7 @@ private function setFieldSettings( $value ) $ret = array(); foreach ($value as $key => $val) { - $ret[$key] = $val; + $ret[$key] = $this->parseFieldSettingValue($val); if ( $this->isReference($val) ) { $ret[$key] = $this->getReference($val); @@ -338,6 +338,17 @@ private function setFieldSettings( $value ) return $ret; } + /** + * Returns transformed value if needed + * + * @param $value + * @return mixed + */ + private function parseFieldSettingValue($value) + { + return $this->getLocationResolverHandler()->resolve($value); + } + /** * Helper to find out if a Field is already defined in a ContentType * diff --git a/DependencyInjection/CompilerPass/LocationResolverCompilerPass.php b/DependencyInjection/CompilerPass/LocationResolverCompilerPass.php new file mode 100644 index 00000000..64ff09a3 --- /dev/null +++ b/DependencyInjection/CompilerPass/LocationResolverCompilerPass.php @@ -0,0 +1,29 @@ +has('ez_migration_bundle.handler.location_resolver')) { + return; + } + + $locationResolverHandler = $container->findDefinition('ez_migration_bundle.handler.location_resolver'); + $locationResolvers = $container->findTaggedServiceIds('ez_migration_bundle.location_resolver'); + + foreach ($locationResolvers as $id => $tags) { + $locationResolverHandler->addMethodCall('addResolver', array( + new Reference($id) + )); + } + } +} diff --git a/EzMigrationBundle.php b/EzMigrationBundle.php index ca0bbfdb..e3188aee 100644 --- a/EzMigrationBundle.php +++ b/EzMigrationBundle.php @@ -2,8 +2,16 @@ namespace Kaliop\eZMigrationBundle; +use Kaliop\eZMigrationBundle\DependencyInjection\CompilerPass\LocationResolverCompilerPass; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; class EzMigrationBundle extends Bundle { + public function build(ContainerBuilder $container) + { + parent::build($container); + + $container->addCompilerPass(new LocationResolverCompilerPass()); + } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index e5fd3e5f..7682d25b 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -7,6 +7,9 @@ parameters: ez_migration_bundle.complex.field.ezbinaryfile.class: Kaliop\eZMigrationBundle\Core\API\ComplexFields\EzBinaryFile ez_migration_bundle.complex.field.ezxmltext.class: Kaliop\eZMigrationBundle\Core\API\ComplexFields\EzXmlText ez_migration_bundle.complex.field.ezpage.class: Kaliop\eZMigrationBundle\Core\API\ComplexFields\EzPage + ez_migration_bundle.handler.location_resolver.class: Kaliop\eZMigrationBundle\Core\API\Handler\LocationResolverHandler + ez_migration_bundle.location_resolver.repository_aware.class: Kaliop\eZMigrationBundle\Core\API\LocationResolver\AbstractRepositoryAwareLocationResolver + ez_migration_bundle.location_resolver.location_remote_id.class: Kaliop\eZMigrationBundle\Core\API\LocationResolver\LocationRemoteIdLocationResolver services: ez_migration_bundle.complex.field.manager: @@ -25,3 +28,15 @@ services: - @ezpublish.api.repository - @service_container - %ezpublish.siteaccess.list% + ez_migration_bundle.handler.location_resolver: + class: %ez_migration_bundle.handler.location_resolver.class% + ez_migration_bundle.location_resolver.repository_aware: + class: %ez_migration_bundle.location_resolver.repository_aware.class% + abstract: true + arguments: + - @ezpublish.api.repository + ez_migration_bundle.location_resolver.location_remote_id: + class: %ez_migration_bundle.location_resolver.location_remote_id.class% + parent: ez_migration_bundle.location_resolver.repository_aware + tags: + - { name: ez_migration_bundle.location_resolver }