Skip to content

Commit

Permalink
Merge 3.x into master (#508)
Browse files Browse the repository at this point in the history
* handle SonataAdminBundle Pager::nbresults deprecation (#501)

* handle SonataAdminBundle Pager::nbresults deprecation

* make setResultsCount() private

* Remove suggested package

The user should have ext-mongodb installed.

* Avoid triggering deprecations

These methods were deprecated in sonata-project/admin-bundle.

* Add upgrade note

It was missing from #501

* DevKit updates (#505)

* fix compatibility

* fix compatibility

* cs-fixer

* remove redundant cast to int

Co-authored-by: David Maicher <[email protected]>
Co-authored-by: Fran Moreno <[email protected]>
Co-authored-by: Sonata CI <[email protected]>
  • Loading branch information
4 people authored Jan 16, 2021
1 parent 12d8ab2 commit 4882ca1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 deletions.
9 changes: 9 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
UPGRADE 3.x
===========

UPGRADE FROM 3.x to 3.x
=======================

### Sonata\DoctrineMongoDBAdminBundle\Datagrid\Pager

Deprecated `computeNbResult()` method.
Deprecated `getNbResults()` method, you SHOULD use `countResults()` instead.
Deprecated `setNbResults()` method.

UPGRADE FROM 3.5 to 3.6
=======================

Expand Down
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
"symfony/phpunit-bridge": "^5.1.8",
"vimeo/psalm": "^4.1.1"
},
"suggest": {
"alcaeus/mongo-php-adapter": "Allows usage of PHP 7"
},
"config": {
"sort-packages": true
},
Expand Down
2 changes: 2 additions & 0 deletions docs/.doctor-rst.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

rules:
american_english: ~
blank_line_after_anchor: ~
blank_line_after_directive: ~
blank_line_after_filepath_in_code_block: ~
composer_dev_option_at_the_end: ~
Expand All @@ -27,6 +28,7 @@ rules:
no_php_open_tag_in_code_block_php_directive: ~
no_php_prefix_before_bin_console: ~
no_space_before_self_xml_closing_tag: ~
max_colons: ~
only_backslashes_in_namespace_in_php_code_block: ~
only_backslashes_in_use_statements_in_php_code_block: ~
replacement: ~
Expand Down
110 changes: 99 additions & 11 deletions src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,62 @@ class Pager extends BasePager
{
protected $queryBuilder = null;

public function computeNbResult()
/**
* @var int
*/
private $resultsCount = 0;

public function countResults(): int
{
$countQuery = clone $this->getQuery();
// NEXT_MAJOR: just return "$this->resultsCount" directly.
$deprecatedCount = $this->getNbResults('sonata_deprecation_mute');

if ($deprecatedCount === $this->resultsCount) {
return $this->resultsCount;
}

@trigger_error(sprintf(
'Relying on the protected property "%s::$nbResults" and its getter/setter is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will fail 4.0. Use "countResults()" and "setResultsCount()" instead.',
self::class,
), E_USER_DEPRECATED);

return $deprecatedCount;
}

if (\count($this->getParameters()) > 0) {
$countQuery->setParameters($this->getParameters());
/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*/
public function getNbResults(): int
{
if ('sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0. Use "countResults()" instead.',
__METHOD__,
), E_USER_DEPRECATED);
}

return $this->nbResults;
}

/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*
* @return int
*/
public function computeNbResult()
{
if ('sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0.',
__METHOD__,
), E_USER_DEPRECATED);
}

return $countQuery->count()->getQuery()->execute();
return $this->computeResultsCount();
}

public function getResults(): array
Expand All @@ -45,26 +92,67 @@ public function getResults(): array

public function init(): void
{
$this->resetIterator();
// NEXT_MAJOR: Remove next line.
$this->resetIterator('sonata_deprecation_mute');

$this->setNbResults($this->computeNbResult());
// NEXT_MAJOR: Remove next line and uncomment the next one.
$this->setResultsCount($this->computeNbResult('sonata_deprecation_mute'));
// $this->setResultsCount($this->computeResultsCount());

$this->getQuery()->setFirstResult(0);
$this->getQuery()->setMaxResults(0);

if (\count($this->getParameters()) > 0) {
$this->getQuery()->setParameters($this->getParameters());
// NEXT_MAJOR: Remove this block.
if (\count($this->getParameters('sonata_deprecation_mute')) > 0) {
$this->getQuery()->setParameters($this->getParameters('sonata_deprecation_mute'));
}

if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->getNbResults()) {
if (0 === $this->getPage() || 0 === $this->getMaxPerPage() || 0 === $this->countResults()) {
$this->setLastPage(0);
} else {
$offset = ($this->getPage() - 1) * $this->getMaxPerPage();

$this->setLastPage((int) ceil($this->getNbResults() / $this->getMaxPerPage()));
$this->setLastPage((int) ceil($this->countResults() / $this->getMaxPerPage()));

$this->getQuery()->setFirstResult($offset);
$this->getQuery()->setMaxResults($this->getMaxPerPage());
}
}

/**
* NEXT_MAJOR: remove this method.
*
* @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x
*/
protected function setNbResults(int $nb): void
{
if ('sonata_deprecation_mute' !== (\func_get_args()[1] ?? null)) {
@trigger_error(sprintf(
'The %s() method is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in 4.0. Use "setResultsCount()" instead.',
__METHOD__,
), E_USER_DEPRECATED);
}

$this->nbResults = $nb;
$this->resultsCount = $nb;
}

private function computeResultsCount(): int
{
$countQuery = clone $this->getQuery();

// NEXT_MAJOR: Remove this block.
if (\count($this->getParameters('sonata_deprecation_mute')) > 0) {
$countQuery->setParameters($this->getParameters('sonata_deprecation_mute'));
}

return (int) $countQuery->count()->getQuery()->execute();
}

private function setResultsCount(int $count): void
{
$this->resultsCount = $count;
// NEXT_MAJOR: Remove this line.
$this->setNbResults($count, 'sonata_deprecation_mute');
}
}

0 comments on commit 4882ca1

Please sign in to comment.