From 81e5a0a89f9c4803e4c10530452382675d137ba9 Mon Sep 17 00:00:00 2001 From: noone-silent Date: Tue, 30 Jul 2024 07:35:27 +0700 Subject: [PATCH] refactor(builder): refactor from zephir. --- .env.example | 1 + docker-compose.yml | 36 +++++++++---------- psalm.xml | 19 ++++++++++ src/Mvc/Model.php | 4 +++ src/Mvc/Model/Query/Builder.php | 10 +++--- .../fixtures/Migrations/AbstractMigration.php | 26 +++++++------- 6 files changed, 58 insertions(+), 38 deletions(-) create mode 100644 psalm.xml diff --git a/.env.example b/.env.example index 6d1c9dce4..b4d7009d8 100644 --- a/.env.example +++ b/.env.example @@ -30,6 +30,7 @@ DATA_MONGODB_NAME="phalcon" DATA_REDIS_HOST="redis" DATA_REDIS_PORT=6379 DATA_REDIS_NAME="0" +DATA_REDIS_PASS="bitnami" # SQLite DATA_SQLITE_NAME="tests/_data/phalcon_test.sqlite" diff --git a/docker-compose.yml b/docker-compose.yml index 7b8ba792b..a603f1442 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,22 +15,10 @@ services: hostname: phalcon-83 build: resources/docker/8.3 working_dir: /srv - links: - - mysql - - postgres - volumes: - - .:/srv - - phalcon-8.3: - container_name: phalcon-8.3 - hostname: phalcon-83 - build: docker/8.3 - working_dir: /srv - links: - - mysql - - postgres volumes: - .:/srv + extra_hosts: + - "host.docker.internal:host-gateway" mysql: container_name: phalcon-mysql @@ -40,6 +28,8 @@ services: - MYSQL_USER=${DATA_MYSQL_USER} - MYSQL_DATABASE=${DATA_MYSQL_NAME} - MYSQL_PASSWORD=${DATA_MYSQL_PASS} + extra_hosts: + - "host.docker.internal:host-gateway" postgres: container_name: phalcon-postgres @@ -48,6 +38,8 @@ services: - POSTGRES_PASSWORD=${DATA_POSTGRES_PASS} - POSTGRES_USER=${DATA_POSTGRES_USER} - POSTGRES_DB=${DATA_POSTGRES_NAME} + extra_hosts: + - "host.docker.internal:host-gateway" mongodb: container_name: phalcon-mongodb @@ -56,6 +48,8 @@ services: - MONGO_INITDB_ROOT_USERNAME=${DATA_MONGODB_USER} - MONGO_INITDB_ROOT_PASSWORD=${DATA_MONGODB_PASS} - MONGO_INITDB_DATABASE=${DATA_MONGODB_NAME} + extra_hosts: + - "host.docker.internal:host-gateway" redis: container_name: phalcon-redis @@ -67,7 +61,7 @@ services: container_name: phalcon-redis-node-0 image: docker.io/bitnami/redis-cluster:7.2 environment: - - 'REDIS_PASSWORD=bitnami' + - REDIS_PASSWORD=${DATA_REDIS_PASS} - 'REDIS_NODES=phalcon-redis-node-0 phalcon-redis-node-1 phalcon-redis-node-2 phalcon-redis-node-3 phalcon-redis-node-4 phalcon-redis-node-5' extra_hosts: - "host.docker.internal:host-gateway" @@ -76,7 +70,7 @@ services: container_name: phalcon-redis-node-1 image: docker.io/bitnami/redis-cluster:7.2 environment: - - 'REDIS_PASSWORD=bitnami' + - REDIS_PASSWORD=${DATA_REDIS_PASS} - 'REDIS_NODES=phalcon-redis-node-0 phalcon-redis-node-1 phalcon-redis-node-2 phalcon-redis-node-3 phalcon-redis-node-4 phalcon-redis-node-5' extra_hosts: - "host.docker.internal:host-gateway" @@ -85,7 +79,7 @@ services: container_name: phalcon-redis-node-2 image: docker.io/bitnami/redis-cluster:7.2 environment: - - 'REDIS_PASSWORD=bitnami' + - REDIS_PASSWORD=${DATA_REDIS_PASS} - 'REDIS_NODES=phalcon-redis-node-0 phalcon-redis-node-1 phalcon-redis-node-2 phalcon-redis-node-3 phalcon-redis-node-4 phalcon-redis-node-5' extra_hosts: - "host.docker.internal:host-gateway" @@ -94,7 +88,7 @@ services: container_name: phalcon-redis-node-3 image: docker.io/bitnami/redis-cluster:7.2 environment: - - 'REDIS_PASSWORD=bitnami' + - REDIS_PASSWORD=${DATA_REDIS_PASS} - 'REDIS_NODES=phalcon-redis-node-0 phalcon-redis-node-1 phalcon-redis-node-2 phalcon-redis-node-3 phalcon-redis-node-4 phalcon-redis-node-5' extra_hosts: - "host.docker.internal:host-gateway" @@ -103,7 +97,7 @@ services: container_name: phalcon-redis-node-4 image: docker.io/bitnami/redis-cluster:7.2 environment: - - 'REDIS_PASSWORD=bitnami' + - REDIS_PASSWORD=${DATA_REDIS_PASS} - 'REDIS_NODES=phalcon-redis-node-0 phalcon-redis-node-1 phalcon-redis-node-2 phalcon-redis-node-3 phalcon-redis-node-4 phalcon-redis-node-5' extra_hosts: - "host.docker.internal:host-gateway" @@ -118,7 +112,7 @@ services: - redis-node-3 - redis-node-4 environment: - - 'REDIS_PASSWORD=bitnami' + - REDIS_PASSWORD=${DATA_REDIS_PASS} - 'REDISCLI_AUTH=bitnami' - 'REDIS_CLUSTER_REPLICAS=1' - 'REDIS_NODES=phalcon-redis-node-0 phalcon-redis-node-1 phalcon-redis-node-2 phalcon-redis-node-3 phalcon-redis-node-4 phalcon-redis-node-5' @@ -129,3 +123,5 @@ services: memcached: container_name: phalcon-memcached image: memcached:1.6-alpine + extra_hosts: + - "host.docker.internal:host-gateway" diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 000000000..02ab5addb --- /dev/null +++ b/psalm.xml @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/src/Mvc/Model.php b/src/Mvc/Model.php index c30020a6f..5afbe9514 100644 --- a/src/Mvc/Model.php +++ b/src/Mvc/Model.php @@ -29,6 +29,7 @@ use Phalcon\Mvc\Model\Criteria; use Phalcon\Mvc\Model\CriteriaInterface; use Phalcon\Mvc\Model\Exception; +use Phalcon\Mvc\Model\Manager; use Phalcon\Mvc\Model\ManagerInterface; use Phalcon\Mvc\Model\MetaDataInterface; use Phalcon\Mvc\Model\QueryInterface; @@ -6298,6 +6299,7 @@ private static function getPreparedQuery( mixed $limit = null ): QueryInterface { $container = Di::getDefault(); + /** @var Manager $manager */ $manager = $container->getShared("modelsManager"); /** @@ -6309,6 +6311,8 @@ private static function getPreparedQuery( if ($limit != null) { $builder->limit($limit); + } elseif (isset($params['limit'])) { + $builder->limit($params['limit'], $params['offset'] ?? null); } $query = $builder->getQuery(); diff --git a/src/Mvc/Model/Query/Builder.php b/src/Mvc/Model/Query/Builder.php index 54b59660f..efd1869f9 100644 --- a/src/Mvc/Model/Query/Builder.php +++ b/src/Mvc/Model/Query/Builder.php @@ -71,9 +71,9 @@ class Builder implements BuilderInterface, InjectionAwareInterface protected array | string | null $columns = null; /** - * @var string|null + * @var array|string|null */ - protected string | null $conditions = null; + protected array | string | null $conditions = null; /** * @var DiInterface|null @@ -113,7 +113,7 @@ class Builder implements BuilderInterface, InjectionAwareInterface /** * @var array|int|string */ - protected array | int | string $limit; + protected array | int | string $limit = 0; /** * @var array|string|null @@ -128,7 +128,7 @@ class Builder implements BuilderInterface, InjectionAwareInterface /** * @var array|string */ - protected array | string $order; + protected array | string $order = []; /** * @var bool @@ -147,7 +147,7 @@ public function __construct(array | string | null $params = null, DiInterface | /** * Process conditions */ - $this->conditions = $params[0] ?? $params['conditions']; + $this->conditions = $params[0] ?? $params['conditions'] ?? []; } if (is_array($this->conditions)) { diff --git a/tests/_data/fixtures/Migrations/AbstractMigration.php b/tests/_data/fixtures/Migrations/AbstractMigration.php index 3f0d1414a..74c436365 100644 --- a/tests/_data/fixtures/Migrations/AbstractMigration.php +++ b/tests/_data/fixtures/Migrations/AbstractMigration.php @@ -19,7 +19,7 @@ /** * Class AbstractMigration * - * @property PDO $connection + * @property PDO $connection * @property string $table */ abstract class AbstractMigration @@ -92,21 +92,21 @@ public function clear(): int $driver = $this->getDriverName(); -// if ($driver === 'mysql') { -// return $this->connection->exec( -// 'truncate table ' . $this->table . ';' -// ); -// } -// -// if ($driver === 'sqlite') { + if ($driver === 'mysql') { + return $this->connection->exec( + 'TRUNCATE TABLE ' . $this->table . ';' + ); + } + + if ($driver === 'sqlite') { return $this->connection->exec( 'delete from ' . $this->table . ';' ); -// } -// -// return $this->connection->exec( -// 'truncate table ' . $this->table . ' cascade;' -// ); + } + + return $this->connection->exec( + 'truncate table ' . $this->table . ' cascade;' + ); } /**