From f2c82e616e54ea8150168f300a4a9997aef7d2e7 Mon Sep 17 00:00:00 2001 From: dantleech Date: Sat, 14 Mar 2015 08:43:47 +0000 Subject: [PATCH] POC for defunct route options --- ConfigurableInterface.php | 29 +++++++++++++++++++ .../AbstractDefunctRouteHandler.php | 0 .../DelegatingDefunctRouteHandler.php | 15 ++++++++-- .../LeaveRedirectDefunctRouteHandler.php | 16 ++++++++-- .../RemoveDefunctRouteHandler.php | 2 +- DefunctRouteHandlerInterface.php | 7 +++-- TokenProviderInterface.php | 9 +----- 7 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 ConfigurableInterface.php create mode 100644 DefunctRouteHandler/AbstractDefunctRouteHandler.php diff --git a/ConfigurableInterface.php b/ConfigurableInterface.php new file mode 100644 index 0000000..ee662f5 --- /dev/null +++ b/ConfigurableInterface.php @@ -0,0 +1,29 @@ + + */ +interface ConfigurableInterface +{ + /** + * Configure the options for this token provider + * + * @param OptionsResolverInterface $optionsResolver + */ + public function configureOptions(OptionsResolverInterface $optionsResolver); +} diff --git a/DefunctRouteHandler/AbstractDefunctRouteHandler.php b/DefunctRouteHandler/AbstractDefunctRouteHandler.php new file mode 100644 index 0000000..e69de29 diff --git a/DefunctRouteHandler/DelegatingDefunctRouteHandler.php b/DefunctRouteHandler/DelegatingDefunctRouteHandler.php index e7384ba..cd6b6fe 100644 --- a/DefunctRouteHandler/DelegatingDefunctRouteHandler.php +++ b/DefunctRouteHandler/DelegatingDefunctRouteHandler.php @@ -17,6 +17,8 @@ use Symfony\Cmf\Component\RoutingAuto\ServiceRegistry; use Symfony\Cmf\Component\RoutingAuto\DefunctRouteHandlerInterface; use Symfony\Cmf\Component\RoutingAuto\UriContextCollection; +use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Cmf\Component\RoutingAuto\ConfigurableInterface; /** * Defunct route handler which delegates the handling of @@ -48,7 +50,7 @@ public function __construct( /** * {@inheritDoc} */ - public function handleDefunctRoutes(UriContextCollection $uriContextCollection) + public function handleDefunctRoutes(UriContextCollection $uriContextCollection, array $options = array()) { $subject = $uriContextCollection->getSubjectObject(); $realClassName = $this->adapter->getRealClassName(get_class($uriContextCollection->getSubjectObject())); @@ -57,6 +59,15 @@ public function handleDefunctRoutes(UriContextCollection $uriContextCollection) $defunctRouteHandlerConfig = $metadata->getDefunctRouteHandler(); $defunctHandler = $this->serviceRegistry->getDefunctRouteHandler($defunctRouteHandlerConfig['name']); - $defunctHandler->handleDefunctRoutes($uriContextCollection); + + $options = array(); + + if ($defunctHandler instanceof ConfigurableInterface) { + $optionsResolver = new OptionsResolver(); + $defunctHandler->configureOptions($optionsResolver); + $options = $optionsResolver->resolve($defunctRouteHandlerConfig['options']); + } + + $defunctHandler->handleDefunctRoutes($uriContextCollection, $options); } } diff --git a/DefunctRouteHandler/LeaveRedirectDefunctRouteHandler.php b/DefunctRouteHandler/LeaveRedirectDefunctRouteHandler.php index 963717a..d1a64d9 100644 --- a/DefunctRouteHandler/LeaveRedirectDefunctRouteHandler.php +++ b/DefunctRouteHandler/LeaveRedirectDefunctRouteHandler.php @@ -9,20 +9,22 @@ * file that was distributed with this source code. */ - namespace Symfony\Cmf\Component\RoutingAuto\DefunctRouteHandler; use Symfony\Cmf\Component\RoutingAuto\DefunctRouteHandlerInterface; use Symfony\Cmf\Component\RoutingAuto\UriContextCollection; use Symfony\Cmf\Component\RoutingAuto\AdapterInterface; -class LeaveRedirectDefunctRouteHandler implements DefunctRouteHandlerInterface +class LeaveRedirectDefunctRouteHandler implements DefunctRouteHandlerInterface, ConfigurableInterface { /** * @var AdapterInterface */ protected $adapter; + /** + * @param AdapterInterface $adapter + */ public function __construct(AdapterInterface $adapter) { $this->adapter = $adapter; @@ -31,7 +33,15 @@ public function __construct(AdapterInterface $adapter) /** * {@inheritDoc} */ - public function handleDefunctRoutes(UriContextCollection $uriContextCollection) + public function configureOptions(OptionsResolverInterface $optionsResolver) + { + $optionsResolver->setDefault('http_code', 301); + } + + /** + * {@inheritDoc} + */ + public function handleDefunctRoutes(UriContextCollection $uriContextCollection, array $options = array()) { $referringAutoRouteCollection = $this->adapter->getReferringAutoRoutes($uriContextCollection->getSubjectObject()); diff --git a/DefunctRouteHandler/RemoveDefunctRouteHandler.php b/DefunctRouteHandler/RemoveDefunctRouteHandler.php index a4c7a2b..9e41b5e 100644 --- a/DefunctRouteHandler/RemoveDefunctRouteHandler.php +++ b/DefunctRouteHandler/RemoveDefunctRouteHandler.php @@ -34,7 +34,7 @@ public function __construct(AdapterInterface $adapter) /** * {@inheritDoc} */ - public function handleDefunctRoutes(UriContextCollection $uriContextCollection) + public function handleDefunctRoutes(UriContextCollection $uriContextCollection, array $options = array()) { $referringAutoRouteCollection = $this->adapter->getReferringAutoRoutes($uriContextCollection->getSubjectObject()); diff --git a/DefunctRouteHandlerInterface.php b/DefunctRouteHandlerInterface.php index 37a5532..0e31c25 100644 --- a/DefunctRouteHandlerInterface.php +++ b/DefunctRouteHandlerInterface.php @@ -12,6 +12,8 @@ namespace Symfony\Cmf\Component\RoutingAuto; +use Symfony\Component\OptionsResolver\OptionsResolverInterface; + /** * @author Daniel Leech */ @@ -28,7 +30,8 @@ interface DefunctRouteHandlerInterface * or perhaps replaced with a redirect route, or indeed * left alone to continue depending on the configuration. * - * TODO + * @param UriContextCollection $uriContextCollection + * @param array $options */ - public function handleDefunctRoutes(UriContextCollection $uriContextCollection); + public function handleDefunctRoutes(UriContextCollection $uriContextCollection, array $options = array()); } diff --git a/TokenProviderInterface.php b/TokenProviderInterface.php index adfe0ac..919245b 100644 --- a/TokenProviderInterface.php +++ b/TokenProviderInterface.php @@ -14,7 +14,7 @@ use Symfony\Component\OptionsResolver\OptionsResolverInterface; -interface TokenProviderInterface +interface TokenProviderInterface extends ConfigurableInterface { /** * Return a token value for the given configuration and @@ -26,11 +26,4 @@ interface TokenProviderInterface * @return string */ public function provideValue(UriContext $uriContext, $options); - - /** - * Configure the options for this token provider - * - * @param OptionsResolverInterface $optionsResolver - */ - public function configureOptions(OptionsResolverInterface $optionsResolver); }