From 5af53b6c6806c412e74c620a6f0909e79a713d2f Mon Sep 17 00:00:00 2001 From: Toni Rudolf Date: Sat, 27 Feb 2021 09:57:56 +0100 Subject: [PATCH 1/2] Syfmony 5 support --- Controller/SortableAdminController.php | 12 +++--- DependencyInjection/Configuration.php | 4 +- .../PixSortableBehaviorExtension.php | 4 +- Resources/config/services.yml | 13 +++--- Services/PositionHandler.php | 13 ++---- Services/PositionODMHandler.php | 2 +- Services/PositionORMHandler.php | 7 +--- Twig/ObjectPositionExtension.php | 42 +++++-------------- composer.json | 9 ++-- 9 files changed, 40 insertions(+), 66 deletions(-) diff --git a/Controller/SortableAdminController.php b/Controller/SortableAdminController.php index ded3558..4b0e3b4 100755 --- a/Controller/SortableAdminController.php +++ b/Controller/SortableAdminController.php @@ -14,11 +14,10 @@ use Pix\SortableBehaviorBundle\Services\PositionHandler; use Sonata\AdminBundle\Controller\CRUDController; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\PropertyAccess\PropertyAccess; /** - * Class SortableAdminController - * * @package Pix\SortableBehaviorBundle */ class SortableAdminController extends CRUDController @@ -28,9 +27,12 @@ class SortableAdminController extends CRUDController * * @param string $position * - * @return RedirectResponse|\Symfony\Component\HttpFoundation\Response + * @return RedirectResponse|Response */ - public function moveAction($position) + public function moveAction( + $position, + PositionHandler $positionHandler + ): Response { $translator = $this->get('translator'); @@ -46,8 +48,6 @@ public function moveAction($position) )); } - /** @var PositionHandler $positionHandler */ - $positionHandler = $this->get('pix_sortable_behavior.position'); $object = $this->admin->getSubject(); $lastPositionNumber = $positionHandler->getLastPosition($object); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 24e485d..c03f2ec 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -27,8 +27,8 @@ public function getConfigTreeBuilder() { $supportedDrivers = array('orm', 'mongodb'); - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('pix_sortable_behavior'); + $treeBuilder = new TreeBuilder('pix_sortable_behavior'); + $rootNode = $treeBuilder->getRootNode(); $rootNode ->children() diff --git a/DependencyInjection/PixSortableBehaviorExtension.php b/DependencyInjection/PixSortableBehaviorExtension.php index bfff2b0..c0ef606 100644 --- a/DependencyInjection/PixSortableBehaviorExtension.php +++ b/DependencyInjection/PixSortableBehaviorExtension.php @@ -10,6 +10,7 @@ namespace Pix\SortableBehaviorBundle\DependencyInjection; +use Pix\SortableBehaviorBundle\Services\PositionHandler; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -42,7 +43,6 @@ public function load(array $configs, ContainerBuilder $container) $config['db_driver'] ); - $container->setAlias('pix_sortable_behavior.position', new Alias($positionHandler)); - $container->getAlias('pix_sortable_behavior.position')->setPublic(true); + $container->setAlias(PositionHandler::class, new Alias($positionHandler)); } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 88fc867..bfbebf2 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,7 +1,10 @@ services: + Pix\SortableBehaviorBundle\Controller\SortableAdminController: + tags: + - { name: controller.service_arguments } + pix_sortable_behavior.position.orm: class: Pix\SortableBehaviorBundle\Services\PositionORMHandler - public: false arguments: - "@doctrine.orm.entity_manager" calls: @@ -10,16 +13,12 @@ services: pix_sortable_behavior.position.mongodb: class: Pix\SortableBehaviorBundle\Services\PositionODMHandler - public: false arguments: - "@doctrine_mongodb.odm.document_manager" calls: - [ 'setPositionField', ["%pix.sortable.behavior.position.field%"]] - pix_sortable_behavior.twig.extension: - class: Pix\SortableBehaviorBundle\Twig\ObjectPositionExtension - arguments: - - "@pix_sortable_behavior.position" - public: false + Pix\SortableBehaviorBundle\Twig\ObjectPositionExtension: + autowire: true tags: - { name: twig.extension } diff --git a/Services/PositionHandler.php b/Services/PositionHandler.php index 62e6d47..d10bd65 100644 --- a/Services/PositionHandler.php +++ b/Services/PositionHandler.php @@ -49,9 +49,8 @@ private function getAccessor() /** * @param object $entity - * @return int */ - abstract public function getLastPosition($entity); + abstract public function getLastPosition($entity): int; /** * @param array $positionField @@ -109,22 +108,18 @@ public function getSortableGroupsFieldByEntity($entity) /** * @param $entity - * - * @return int */ - public function getCurrentPosition($entity) + public function getCurrentPosition($entity): int { - return $this->getAccessor()->getValue($entity, $this->getPositionFieldByEntity($entity)); + return (int) $this->getAccessor()->getValue($entity, $this->getPositionFieldByEntity($entity)); } /** * @param object $object * @param string $movePosition * @param int $lastPosition - * - * @return int */ - public function getPosition($object, $movePosition, $lastPosition) + public function getPosition($object, $movePosition, $lastPosition): int { $currentPosition = $this->getCurrentPosition($object); $newPosition = 0; diff --git a/Services/PositionODMHandler.php b/Services/PositionODMHandler.php index 29a4dea..ce69465 100644 --- a/Services/PositionODMHandler.php +++ b/Services/PositionODMHandler.php @@ -25,7 +25,7 @@ public function __construct(DocumentManager $documentManager) $this->dm = $documentManager; } - public function getLastPosition($entity) + public function getLastPosition($entity): int { $entityClass = ClassUtils::getClass($entity); $parentEntityClass = true; diff --git a/Services/PositionORMHandler.php b/Services/PositionORMHandler.php index 2c66edd..f9aface 100644 --- a/Services/PositionORMHandler.php +++ b/Services/PositionORMHandler.php @@ -15,14 +15,12 @@ use Doctrine\ORM\EntityManagerInterface; /** - * Class PositionORMHandler - * * @package Pix\SortableBehaviorBundle */ class PositionORMHandler extends PositionHandler { /** - * @var array + * @var array */ private static $cacheLastPosition = []; @@ -41,9 +39,8 @@ public function __construct(EntityManagerInterface $entityManager) /** * @param object $entity - * @return int */ - public function getLastPosition($entity) + public function getLastPosition($entity): int { $entityClass = ClassUtils::getClass($entity); $parentEntityClass = true; diff --git a/Twig/ObjectPositionExtension.php b/Twig/ObjectPositionExtension.php index fdd2c43..3a02576 100644 --- a/Twig/ObjectPositionExtension.php +++ b/Twig/ObjectPositionExtension.php @@ -3,61 +3,41 @@ namespace Pix\SortableBehaviorBundle\Twig; use Pix\SortableBehaviorBundle\Services\PositionHandler; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; /** * Description of ObjectPositionExtension * * @author Volker von Hoesslin */ -class ObjectPositionExtension extends \Twig_Extension +class ObjectPositionExtension extends AbstractExtension { - const NAME = 'sortableObjectPosition'; - /** - * PositionHandler + * @var PositionHandler */ private $positionHandler; - /** - * @param PositionHandler $positionHandler - */ public function __construct(PositionHandler $positionHandler) { $this->positionHandler = $positionHandler; } - /** - * Returns the name of the extension. - * - * @return string The extension name - */ - public function getName() - { - return self::NAME; - } - - /** - * @return array - */ - public function getFunctions() + public function getFunctions(): array { return array( - new \Twig_SimpleFunction('currentObjectPosition', array($this, 'currentPosition')), - new \Twig_SimpleFunction('lastPosition', array($this, 'lastPosition')) + new TwigFunction('currentObjectPosition', array($this, 'currentPosition')), + new TwigFunction('lastPosition', array($this, 'lastPosition')) ); } - /** - * @return int - */ - public function currentPosition($entity) { + public function currentPosition($entity): int + { return $this->positionHandler->getCurrentPosition($entity); } - /** - * @return int - */ - public function lastPosition($entity) { + public function lastPosition($entity): int + { return $this->positionHandler->getLastPosition($entity); } } diff --git a/composer.json b/composer.json index ecf5fbe..5a93661 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,12 @@ } ], "require": { - "php": ">=5.3.0", - "symfony/framework-bundle": ">=2.1", - "symfony/form": ">=2.1" + "php": ">=7.2", + "symfony/dependency-injection": ">=5.0", + "symfony/form": ">=5.0", + "symfony/config": ">=5.0", + "symfony/http-kernel": ">=5.0", + "twig/twig": "^2.0 || ^3.0" }, "require-dev": { "gedmo/doctrine-extensions": ">=2.2" From 0462e7e3251903e36f6704a92bc5523038492c45 Mon Sep 17 00:00:00 2001 From: Toni Rudolf Date: Sat, 9 Oct 2021 13:08:39 +0200 Subject: [PATCH 2/2] Add request param to isXmlHttpRequest --- Controller/SortableAdminController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Controller/SortableAdminController.php b/Controller/SortableAdminController.php index 4b0e3b4..31c3a60 100755 --- a/Controller/SortableAdminController.php +++ b/Controller/SortableAdminController.php @@ -14,6 +14,7 @@ use Pix\SortableBehaviorBundle\Services\PositionHandler; use Sonata\AdminBundle\Controller\CRUDController; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\PropertyAccess\PropertyAccess; @@ -31,6 +32,7 @@ class SortableAdminController extends CRUDController */ public function moveAction( $position, + Request $request, PositionHandler $positionHandler ): Response { @@ -58,7 +60,7 @@ public function moveAction( $this->admin->update($object); - if ($this->isXmlHttpRequest()) { + if ($this->isXmlHttpRequest($request)) { return $this->renderJson(array( 'result' => 'ok', 'objectId' => $this->admin->getNormalizedIdentifier($object)