This repository has been archived by the owner on Dec 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #2 add Symfony routing support for "action" type (sstok)
This PR was merged into the master branch. Discussion ---------- |Q |A | |--- |---| |Bug Fix? |no | |New Feature? |yes| |BC Breaks? |no | |Deprecations?|no | |Fixed Tickets| | Commits ------- c879f79 add Symfony routing support for "action" type
- Loading branch information
Showing
10 changed files
with
801 additions
and
2 deletions.
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
49 changes: 49 additions & 0 deletions
49
src/DependencyInjection/Compiler/RequestUriProviderPass.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,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksDatagrid package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Bundle\DatagridBundle\DependencyInjection\Compiler; | ||
|
||
use Rollerworks\Component\Datagrid\Twig\Extension\DatagridExtension as TwigDatagridExtension; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Sebastiaan Stok <[email protected]> | ||
*/ | ||
class RequestUriProviderPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('rollerworks_datagrid.column_extension.action')) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition('rollerworks_datagrid.column_extension.action'); | ||
|
||
// Symfony >=2.4 | ||
if ($container->hasDefinition('request_stack') || $container->hasAlias('request_stack')) { | ||
$definition->addArgument(new Reference('rollerworks_datagrid.request_uri_provider.request_stack')); | ||
|
||
$container->removeDefinition('rollerworks_datagrid.request_uri_provider.request_service'); | ||
$container->removeDefinition('rollerworks_datagrid.event_subscriber.request'); | ||
} else { | ||
// Symfony 2.3 | ||
$definition->addArgument(new Reference('rollerworks_datagrid.request_uri_provider.request_service')); | ||
|
||
$container->removeDefinition('rollerworks_datagrid.request_uri_provider.request_stack'); | ||
} | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
src/Extension/Symfony/ColumnTypeExtension/ActionTypeExtension.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,153 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksDatagrid package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Bundle\DatagridBundle\Extension\Symfony\ColumnTypeExtension; | ||
|
||
use Rollerworks\Bundle\DatagridBundle\Extension\Symfony\RequestUriProviderInterface; | ||
use Rollerworks\Component\Datagrid\Column\AbstractColumnTypeExtension; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
/** | ||
* @author Sebastiaan Stok <[email protected]> | ||
*/ | ||
class ActionTypeExtension extends AbstractColumnTypeExtension | ||
{ | ||
/** | ||
* Router to generate urls. | ||
* | ||
* @var UrlGeneratorInterface | ||
*/ | ||
private $router; | ||
|
||
/** | ||
* RequestUriProvider. | ||
* | ||
* This is used for getting the current URI for redirects. | ||
* | ||
* @var RequestUriProviderInterface | ||
*/ | ||
private $requestUriProvider; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param UrlGeneratorInterface $router | ||
* @param RequestUriProviderInterface $requestUriProvider | ||
*/ | ||
public function __construct(UrlGeneratorInterface $router, RequestUriProviderInterface $requestUriProvider) | ||
{ | ||
$this->router = $router; | ||
$this->requestUriProvider = $requestUriProvider; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults( | ||
[ | ||
'route_name' => null, | ||
'parameters_field_mapping' => [], | ||
'additional_parameters' => [], | ||
'uri_scheme' => function (Options $options, $value) { | ||
// Value was already provided so just use that one. | ||
// Always do this check as lazy options don't overwrite. | ||
if (is_string($value)) { | ||
return $value; | ||
} | ||
|
||
if (null !== $options['route_name']) { | ||
return $this->createRouteGenerator( | ||
$options['route_name'], | ||
$options['reference_type'], | ||
$options['parameters_field_mapping'], | ||
$options['additional_parameters'] | ||
); | ||
} | ||
}, | ||
'reference_type' => UrlGeneratorInterface::ABSOLUTE_PATH, | ||
|
||
'redirect_route' => null, | ||
'redirect_parameters_field_mapping' => [], | ||
'redirect_additional_parameters' => [], | ||
'redirect_uri' => function (Options $options, $value) { | ||
// Value was already provided so just use that one. | ||
// Always do this check as lazy options don't overwrite. | ||
if (is_string($value)) { | ||
return $value; | ||
} | ||
|
||
if (null !== $options['redirect_route']) { | ||
return $this->createRouteGenerator( | ||
$options['redirect_route'], | ||
$options['reference_type'], | ||
$options['redirect_parameters_field_mapping'], | ||
$options['redirect_additional_parameters'] | ||
); | ||
} | ||
|
||
return $this->requestUriProvider->getRequestUri(); | ||
}, | ||
] | ||
); | ||
|
||
if ($resolver instanceof OptionsResolverInterface) { | ||
$resolver->setAllowedTypes( | ||
[ | ||
'route_name' => ['string', 'null'], | ||
'parameters_field_mapping' => ['array'], | ||
'additional_parameters' => ['array'], | ||
|
||
'redirect_route' => ['string', 'null'], | ||
'redirect_parameters_field_mapping' => ['array'], | ||
'redirect_additional_parameters' => ['array'], | ||
] | ||
); | ||
} else { | ||
$resolver->setAllowedTypes('route_name', ['string', 'null']); | ||
$resolver->setAllowedTypes('parameters_field_mapping', ['array']); | ||
$resolver->setAllowedTypes('additional_parameters', ['array']); | ||
|
||
$resolver->setAllowedTypes('redirect_route', ['string', 'null']); | ||
$resolver->setAllowedTypes('redirect_parameters_field_mapping', ['array']); | ||
$resolver->setAllowedTypes('redirect_additional_parameters', ['array']); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getExtendedType() | ||
{ | ||
return 'action'; | ||
} | ||
|
||
private function createRouteGenerator($routeName, $referenceType, array $fieldMapping, array $additionalParameters) | ||
{ | ||
return function (array $values) use ($routeName, $referenceType, $fieldMapping, $additionalParameters) { | ||
$routeParameters = []; | ||
|
||
foreach ($fieldMapping as $parameterName => $mappingField) { | ||
$routeParameters[$parameterName] = $values[$mappingField]; | ||
} | ||
|
||
return $this->router->generate( | ||
$routeName, | ||
array_merge($routeParameters, $additionalParameters), | ||
$referenceType | ||
); | ||
}; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Extension/Symfony/EventSubscriber/RequestSubscriber.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,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksDatagrid package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Bundle\DatagridBundle\Extension\Symfony\EventSubscriber; | ||
|
||
use Rollerworks\Bundle\DatagridBundle\Extension\Symfony\RequestUriProviderByListener; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\KernelEvent; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
class RequestSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var RequestUriProviderByListener | ||
*/ | ||
private $uriProvider; | ||
|
||
/** | ||
* @param RequestUriProviderByListener $uriProvider | ||
*/ | ||
public function __construct(RequestUriProviderByListener $uriProvider) | ||
{ | ||
$this->uriProvider = $uriProvider; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => 'onRequest' | ||
]; | ||
} | ||
|
||
/** | ||
* @param KernelEvent $event | ||
*/ | ||
public function onRequest(KernelEvent $event) | ||
{ | ||
if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { | ||
$this->uriProvider->setRequest($event->getRequest()); | ||
} | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksDatagrid package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Bundle\DatagridBundle\Extension\Symfony; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class RequestUriProviderByListener implements RequestUriProviderInterface | ||
{ | ||
/** | ||
* @var Request | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRequestUri() | ||
{ | ||
if (null === $this->request) { | ||
throw new \LogicException( | ||
'No Request set for RequestUriProviderByListener. Do not use this service manually.' | ||
); | ||
} | ||
|
||
return $this->request->getRequestUri(); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
*/ | ||
public function setRequest(Request $request = null) | ||
{ | ||
$this->request = $request; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Extension/Symfony/RequestUriProviderByRequestStack.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,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksDatagrid package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Bundle\DatagridBundle\Extension\Symfony; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
class RequestUriProviderByRequestStack implements RequestUriProviderInterface | ||
{ | ||
/** | ||
* @var RequestStack | ||
*/ | ||
private $requestStack; | ||
|
||
public function __construct(RequestStack $requestStack) | ||
{ | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRequestUri() | ||
{ | ||
return $this->requestStack->getMasterRequest()->getRequestUri(); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the RollerworksDatagrid package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Bundle\DatagridBundle\Extension\Symfony; | ||
|
||
/** | ||
* The RequestUriProvide provides access to the current URI of the master-request. | ||
* | ||
* This interface only exists to be compatible with Symfony <2.4, | ||
* where the RequestStack did not exist yet. | ||
*/ | ||
interface RequestUriProviderInterface | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getRequestUri(); | ||
} |
Oops, something went wrong.