Skip to content

Commit

Permalink
Support CakePHP 4.x (#15)
Browse files Browse the repository at this point in the history
* Support CakePHP 4.x

* Drop support for PHP < 7.2

* Update Query::__construct()

* Fix by suggestions

* Apply suggested change

* Add a test for Paginator::paginate()

* Update documentation

* Update documentation

* Update documentation

* Update documentation for CakePHP 4.x
  • Loading branch information
chitoku-k authored Jan 12, 2020
1 parent bee1598 commit 00f7f88
Show file tree
Hide file tree
Showing 23 changed files with 169 additions and 124 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.6
- 7.2
- 7.3
- 7.4
Expand Down
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ Rapid pagination without using OFFSET

## Requirements

- PHP: ^5.6 || ^7.0
- CakePHP: ^3.6
- PHP: ^7.2
- CakePHP: ^4.0
- [lampager/lampager][]: ^0.4

Note: [lampager/lampager-cakephp2][]
for CakePHP 2.x is available!
### Note

- For CakePHP 2.x, use [lampager/lampager-cakephp2][].
- For CakePHP 3.x, use [lampager/lampager-cakephp v1.x][].
- For CakePHP 4.x, use lampager/lampager-cakephp v2.x (this version).

## Installing

```bash
composer require lampager/lampager-cakephp
composer require lampager/lampager-cakephp:^2.0
```

For SQLite users, see [SQLite](#sqlite) to configure.
Expand All @@ -50,7 +53,7 @@ use Lampager\Cake\Datasource\Paginator;

class AppController extends Controller
{
public function initialize()
public function initialize(): void
{
parent::initialize();

Expand Down Expand Up @@ -96,7 +99,7 @@ use Lampager\Cake\Model\Behavior\LampagerBehavior;

class AppTable extends Table
{
public function initialize(array $config)
public function initialize(array $config): void
{
parent::initialize($config);

Expand Down Expand Up @@ -294,7 +297,7 @@ class PostsController extends AppController
/**
* This method shows how to pass options by a query and array.
*/
public function query()
public function query(): void
{
// Get cursor parameters
$previous = json_decode($this->request->getQuery('previous_cursor'), true);
Expand Down Expand Up @@ -322,7 +325,7 @@ class PostsController extends AppController
/**
* This method shows how to pass options from an array.
*/
public function options()
public function options(): void
{
// Get cursor parameters
$previous = json_decode($this->request->getQuery('previous_cursor'), true);
Expand Down Expand Up @@ -414,7 +417,8 @@ return [
];
```

[lampager/lampager]: https://github.com/lampager/lampager
[lampager/lampager-cakephp2]: https://github.com/lampager/lampager-cakephp2
[Pagination]: https://book.cakephp.org/3/en/controllers/components/pagination.html
[Working with Result Sets]: https://book.cakephp.org/3/en/orm/retrieving-data-and-resultsets.html#working-with-result-sets
[lampager/lampager]: https://github.com/lampager/lampager
[lampager/lampager-cakephp v1.x]: https://github.com/lampager/lampager-cakephp/tree/v1.x
[lampager/lampager-cakephp2]: https://github.com/lampager/lampager-cakephp2
[Pagination]: https://book.cakephp.org/4/en/controllers/components/pagination.html
[Working with Result Sets]: https://book.cakephp.org/4/en/orm/retrieving-data-and-resultsets.html#working-with-result-sets
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
}
},
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.2",
"lampager/lampager": "^0.4"
},
"require-dev": {
"cakephp/cakephp": "^3.6",
"phpunit/phpunit": "^5.7 || ^6.0",
"cakephp/cakephp": "^4.0",
"phpunit/phpunit": "^8.0",
"php-coveralls/php-coveralls": "^2.2",
"nilportugues/sql-query-formatter": "^1.2"
},
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<phpunit colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="Lampager CakePHP Test Suite">
Expand Down
4 changes: 3 additions & 1 deletion src/ArrayProcessor.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Lampager\Cake;

use Cake\ORM\Entity;
Expand All @@ -25,7 +27,7 @@ protected function defaultFormat($rows, array $meta, LampagerQuery $query)
* @param string $alias Current model alias
* @return string Unaliased column where applicable
*/
protected function removeAlias($column, $alias)
protected function removeAlias(string $column, string $alias): string
{
if (strpos($column, "{$alias}.") !== 0) {
return $column;
Expand Down
5 changes: 4 additions & 1 deletion src/Database/Driver/Sqlite.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?php

declare(strict_types=1);

namespace Lampager\Cake\Database\Driver;

use Cake\Database\Driver\Sqlite as BaseSqlite;
use Cake\Database\QueryCompiler;
use Lampager\Cake\Database\SqliteCompiler;

class Sqlite extends BaseSqlite
{
/**
* {@inheritdoc}
*/
public function newCompiler()
public function newCompiler(): QueryCompiler
{
return new SqliteCompiler();
}
Expand Down
8 changes: 6 additions & 2 deletions src/Database/SqliteCompiler.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php

declare(strict_types=1);

namespace Lampager\Cake\Database;

use Cake\Database\Query;
use Cake\Database\SqliteCompiler as BaseSqliteCompiler;
use Cake\Database\ValueBinder;

class SqliteCompiler extends BaseSqliteCompiler
{
/**
* {@inheritdoc}
*/
protected function _buildSelectPart($parts, $query, $generator)
protected function _buildSelectPart(array $parts, Query $query, ValueBinder $generator): string
{
if (!$query->clause('union')) {
return parent::_buildSelectPart($parts, $query, $generator);
Expand All @@ -21,7 +25,7 @@ protected function _buildSelectPart($parts, $query, $generator)
/**
* {@inheritdoc}
*/
protected function _buildUnionPart($parts, $query, $generator)
protected function _buildUnionPart(array $parts, Query $query, ValueBinder $generator): string
{
$parts = array_map(function ($p) use ($generator) {
$p['query'] = $p['query']->sql($generator);
Expand Down
9 changes: 8 additions & 1 deletion src/Datasource/Paginator.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

declare(strict_types=1);

namespace Lampager\Cake\Datasource;

use Cake\Datasource\Paginator as CakePaginator;
use Cake\Datasource\QueryInterface;
use Cake\Datasource\ResultSetInterface;
use Exception;
use Lampager\Cake\ORM\Query;
use Lampager\Cake\PaginationResult;
use Lampager\Exceptions\InvalidArgumentException;
Expand All @@ -15,12 +19,15 @@ class Paginator extends CakePaginator
* @throws InvalidArgumentException if the \Lampager\Cake\ORM\Query is given
* @return PaginationResult
*/
public function paginate($object, array $params = [], array $settings = [])
public function paginate(object $object, array $params = [], array $settings = []): ResultSetInterface
{
$query = null;
if ($object instanceof QueryInterface) {
$query = $object;
$object = $query->getRepository();
if ($object === null) {
throw new Exception('No repository set for query.');
}
}

if ($query instanceof Query) {
Expand Down
7 changes: 3 additions & 4 deletions src/Model/Behavior/LampagerBehavior.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

declare(strict_types=1);

namespace Lampager\Cake\Model\Behavior;

use Cake\ORM\Behavior;
use Lampager\Cake\ORM\Query;

class LampagerBehavior extends Behavior
{
/**
* @return Query
*/
public function lampager()
public function lampager(): Query
{
$query = new Query($this->getTable()->getConnection(), $this->getTable());
$query->select();
Expand Down
40 changes: 20 additions & 20 deletions src/ORM/Query.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php

declare(strict_types=1);

namespace Lampager\Cake\ORM;

use Cake\Database\Connection;
use Cake\Database\Expression\OrderByExpression;
use Cake\Database\Expression\OrderClauseExpression;
use Cake\Database\Expression\QueryExpression;
use Cake\Database\ExpressionInterface;
use Cake\Datasource\ResultSetInterface;
use Cake\ORM\Query as BaseQuery;
use Cake\ORM\Table;
use Lampager\Cake\PaginationResult;
use Lampager\Cake\Paginator;
use Lampager\Contracts\Cursor;
Expand Down Expand Up @@ -34,10 +39,10 @@ class Query extends BaseQuery
/**
* Construct query.
*
* @param \Cake\Database\Connection $connection The connection object
* @param \Cake\ORM\Table $table The table this query is starting on
* @param Connection $connection The connection object
* @param Table $table The table this query is starting on
*/
public function __construct($connection, $table)
public function __construct(Connection $connection, Table $table)
{
parent::__construct($connection, $table);

Expand All @@ -47,10 +52,8 @@ public function __construct($connection, $table)
/**
* Create query based on the existing query. This factory copies the internal
* state of the given query to a new instance.
*
* @param BaseQuery $query
*/
public static function fromQuery($query)
public static function fromQuery(BaseQuery $query)
{
$obj = new static($query->getConnection(), $query->getRepository());

Expand Down Expand Up @@ -124,24 +127,20 @@ public function limit($num)
* {@inheritdoc}
* @return PaginationResult
*/
public function all()
public function all(): ResultSetInterface
{
return $this->_paginator->paginate($this->_cursor);
}

/**
* {@inheritdoc}
*/
protected function _performCount()
protected function _performCount(): int
{
return $this->all()->count();
}

/**
* @param null|OrderByExpression $order
* @return void
*/
protected function _executeOrder($order)
protected function _executeOrder(?OrderByExpression $order): void
{
$this->_paginator->clearOrderBy();

Expand All @@ -168,6 +167,8 @@ protected function _executeOrder($order)

/** @var string $direction */
$direction = $matches['direction'];

/** @var ExpressionInterface|string $field */
$field = $condition->getField();

if ($field instanceof ExpressionInterface) {
Expand All @@ -188,10 +189,9 @@ protected function _executeOrder($order)
}

/**
* @param null|int|QueryExpression $limit
* @return void
* @param null|int|QueryExpression $limit
*/
protected function _executeLimit($limit)
protected function _executeLimit($limit): void
{
if (is_int($limit)) {
$this->_paginator->limit($limit);
Expand Down Expand Up @@ -219,7 +219,7 @@ protected function _executeLimit($limit)
/**
* {@inheritdoc}
*/
public function __call($method, $args)
public function __call(string $method, array $arguments)
{
static $options = [
'forward',
Expand All @@ -232,17 +232,17 @@ public function __call($method, $args)
];

if (in_array($method, $options, true)) {
$this->_paginator->$method(...$args);
$this->_paginator->$method(...$arguments);
return $this;
}

return parent::__call($method, $args);
return parent::__call($method, $arguments);
}

/**
* {@inheritdoc}
*/
public function __debugInfo()
public function __debugInfo(): array
{
try {
$info = $this->_paginator->build($this->_cursor)->__debugInfo();
Expand Down
Loading

0 comments on commit 00f7f88

Please sign in to comment.