-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from gbalcewicz/location-resolver
Added location resolver. Implemented only for field setting value.
- Loading branch information
Showing
9 changed files
with
209 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\API\Handler; | ||
|
||
use Kaliop\eZMigrationBundle\Core\API\LocationResolver\LocationResolverInterface; | ||
|
||
class LocationResolverHandler | ||
{ | ||
/** | ||
* @var LocationResolverInterface[] | ||
*/ | ||
private $resolvers; | ||
|
||
/** | ||
* @param LocationResolverInterface $resolver | ||
*/ | ||
public function addResolver(LocationResolverInterface $resolver) | ||
{ | ||
$this->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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Core/API/LocationResolver/AbstractRepositoryAwareLocationResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\API\LocationResolver; | ||
|
||
use eZ\Publish\API\Repository\Repository; | ||
|
||
abstract class AbstractRepositoryAwareLocationResolver implements LocationResolverInterface | ||
{ | ||
/** | ||
* @var Repository | ||
*/ | ||
protected $repository; | ||
|
||
/** | ||
* AbstractRepositoryAwareLocationResolver constructor. | ||
* | ||
* @param Repository $repository | ||
*/ | ||
public function __construct(Repository $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Core/API/LocationResolver/LocationRemoteIdLocationResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\API\LocationResolver; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Location; | ||
|
||
class LocationRemoteIdLocationResolver extends AbstractRepositoryAwareLocationResolver | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $remoteId; | ||
|
||
/** | ||
* Resolves reference to location id | ||
* | ||
* @param $reference | ||
* @return int | ||
* @throws \Exception | ||
*/ | ||
public function resolve($reference) | ||
{ | ||
if (empty($this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\Core\API\LocationResolver; | ||
|
||
interface LocationResolverInterface | ||
{ | ||
/** | ||
* Resolves reference to location id | ||
* | ||
* @param $reference | ||
* @return int | ||
*/ | ||
public function resolve($reference); | ||
|
||
/** | ||
* Tests if $reference should be resolved to location id | ||
* | ||
* @param $reference | ||
* @return bool | ||
*/ | ||
public function shouldResolve($reference); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
DependencyInjection/CompilerPass/LocationResolverCompilerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Kaliop\eZMigrationBundle\DependencyInjection\CompilerPass; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class LocationResolverCompilerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* @param ContainerBuilder $container | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->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) | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters