tzachi / phalcon-repository

Phalcon repository 是一个库,用于在采用 Phalcon 框架的项目中实现仓库模式

dev-master / 0.1.x-dev 2019-08-20 11:08 UTC

This package is auto-updated.

Last update: 2024-09-20 22:07:02 UTC


README

License Total Downloads Latest Stable Version Unstable Version

Branch master Build Status Scrutinizer Code Quality Code Coverage

Phalcon Repository 是一个库,用于在采用 Phalcon PHP 框架的项目中实现仓库模式

安装

安装通过 composer 完成:composer require tzachi/phalcon-repository:0.1.x-dev

用法

假设您有一个名为 User 的模型(它扩展了 Phalcon 的 Model),您可以使用仓库如下

use MyProject\Model\User;
use TZachi\PhalconRepository\ModelWrapper;
use TZachi\PhalconRepository\Repository;
use TZachi\PhalconRepository\Resolver\QueryParameter;

$userRepository = new Repository(new ModelWrapper(User::class));

// Simple usage, finds a user model by its id.
$user = $userRepository->findFirst(1);

// Finds a user model by its name, ordering by name ASC.
$user = $userRepository->findFirstBy('name', 'Test User Name', ['name' => 'ASC']);

// A bit more of complexity here, this will find a user using the following condition:
// id BETWEEN 15 AND 21 OR id IN (1, 2, 3) OR (name = 'Timo Zachi' AND created_at > '2019-01-01')
// ordering the results by id DESC.
$user = $userRepository->findFirstWhere(
    [
        '@type' => QueryParameter::TYPE_OR,
        [
            '@operator' => 'BETWEEN',
            'id' => [15, 21], // Between operator.
        ],
        'id' => [1, 2, 3], // In operator.
        [
            // Type AND is default, doesn't need to be specified. It's explicit here for sample purposes.
            '@type' => QueryParameter::TYPE_AND,
            'name' => 'Timo Zachi', // Equals operator (default).
            [
                '@operator' => '>',
                'createdAt' => '2019-01-01',
            ],
        ],
    ],
    ['id' => 'DESC'] // Order by.
);

// You can use the same parameters to query for multiple records, that will return a result set.
// You need only to use one of the methods 'findWhere' or 'findBy'. Notice that there is also a
// limit and offset parameter.
$resultSet = $userRepository->findWhere(
    ['email' => 'timo.zachi@timoteo.me'], // Conditions.
    ['id' => 'DESC'], // Order by.
    10, // Limit.
    5 // Offset.
);

// Aggregation methods

// Count number of users in table
$userRepository->count();

// Get the minimum value of the `createdAt` colum using a where condition
$userRepository->minimum('createdAt', ['id' => [100, 200], '@operator' => 'BETWEEN']);
// Get the maximum value of the `name` column in the entire table
$userRepository->maximum('name');

// Returns the sum of the balance column on users with id 40, 41 and 42
$userRepository->sum('balance', ['id' => [40, 41, 42]]);

// Returns the average of the balance column on users with id 40, 41 and 42
$userRepository->average('balance', ['id' => [40, 41, 42]]);

要在 phalcon 项目内部设置仓库模式,您可以使用 RepositoryFactory

use Phalcon\Annotations\AdapterInterface as AnnotationsAdapterInterface;
use TZachi\PhalconRepository\RepositoryFactory;

// Set the repository service as a shared service
$di->setShared(
    'repositoryManager',
    function (): RepositoryFactory {
        /**
         * @var AnnotationsAdapterInterface $annotations
         */
        $annotations = $this->get('annotations');

        // The repository factory reads the annotations of the Model class to determine which repository it should use,
        // that's why it needs the annotations parser. It falls back to the default repository class if one wasn't
        // specified in the model
        return new RepositoryFactory($annotations);
    }
);

现在,如果您想要为特定的模型创建特定的仓库,首先创建仓库

namespace MyApp\Repository;

use TZachi\PhalconRepository\Repository;

class UserRepository extends Repository
{
    public function findLastUserCreated(): User
    {
        return $this->findFirstWhere([], ['id' => 'DESC']);
    }

然后,在模型中添加注释以指定其仓库应该使用的类

namespace MyApp\Model;

use MyApp\Model\User;
use Phalcon\Mvc\Model;

/**
 * @Repository(MyApp\Repository\UserRepository);
 */
class User extends Model
{
    ...

现在,在项目的任何地方,您都可以轻松地获取模型的仓库

public function userAction($id): void
{
    /**
     * @var \MyApp\Repository\UserRepository $userRepository
     */
    $userRepository = $this->repositoryManager->get(\MyApp\Model\User::class);
    $user = $userRepository->findLastUserCreated($id);
    ...

贡献

欢迎提交拉取请求。代码将经过以下检查的验证

  • 代码将验证编码标准
  • 静态分析工具(phpstan)将分析您的代码
  • 单元测试必须达到 100% 的代码覆盖率(如果您创建了一个新功能,请确保有足够的测试来覆盖它)
  • 对于某些功能,需要功能测试

为了使事情更简单,创建了一个 Makefile。您只需运行

make

make 命令将针对您的代码运行所有可用的检查,并通知您是否有任何失败

如果您想运行特定的检查

# check code style
make cs-check
# static analysis on src directory
make phpstan-src
# static analysis on tests directory
make phpstan-tests
# run unit tests on php 7.3
make test-unit-php73
# run functional tests on php 7.3
make test-functional-php73

要自动修复 cs(并非所有规则都可以自动修复)

make cs-fix

注意

请注意,该项目仍有大量工作要做,并且仍在开发中。希望当完成时,Phalcon 已经发布了 4.0 版本,这样该项目就能使用它了