Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Controller/SortableAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
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;

/**
* Class SortableAdminController
*
* @package Pix\SortableBehaviorBundle
*/
class SortableAdminController extends CRUDController
Expand All @@ -28,9 +28,13 @@ class SortableAdminController extends CRUDController
*
* @param string $position
*
* @return RedirectResponse|\Symfony\Component\HttpFoundation\Response
* @return RedirectResponse|Response
*/
public function moveAction($position)
public function moveAction(
$position,
Request $request,
PositionHandler $positionHandler
): Response
{
$translator = $this->get('translator');

Expand All @@ -46,8 +50,6 @@ public function moveAction($position)
));
}

/** @var PositionHandler $positionHandler */
$positionHandler = $this->get('pix_sortable_behavior.position');
$object = $this->admin->getSubject();

$lastPositionNumber = $positionHandler->getLastPosition($object);
Expand All @@ -58,7 +60,7 @@ public function moveAction($position)

$this->admin->update($object);

if ($this->isXmlHttpRequest()) {
if ($this->isXmlHttpRequest($request)) {
return $this->renderJson(array(
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object)
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/PixSortableBehaviorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
13 changes: 6 additions & 7 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 }
13 changes: 4 additions & 9 deletions Services/PositionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Services/PositionODMHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions Services/PositionORMHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
use Doctrine\ORM\EntityManagerInterface;

/**
* Class PositionORMHandler
*
* @package Pix\SortableBehaviorBundle
*/
class PositionORMHandler extends PositionHandler
{
/**
* @var array
* @var array<integer>
*/
private static $cacheLastPosition = [];

Expand All @@ -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;
Expand Down
42 changes: 11 additions & 31 deletions Twig/ObjectPositionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <volker.von.hoesslin@empora.com>
*/
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);
}
}
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down