承接 mistralys/application-utils-collections 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mistralys/application-utils-collections

Composer 安装命令:

composer require mistralys/application-utils-collections

包简介

Interfaces, traits and classes for handling static item collections, similar to Enums and with useful getter methods.

README 文档

README

Interfaces, traits and classes for handling item collections, similar to Enums but with useful getter methods.

This is part of the Application Utils ecology.

Feature Overview

The collection classes come in several flavors:

  • Separate collections for records with string and integer IDs.
  • Class loader collections that instantiate classes from one or more folders.
  • Basket-like collections that allow freely adding and removing items.

Basic Example

Let's say you want to create a collection of herbs. A string-based collection is ideal in this case, each herb being an object that can easily be extended with additional properties and methods.

$basil = Herbs::getInstance()->getByID(Herbs::BASIL);

echo $basil->getID(); // basil
echo $basil->getName(); // Basil
foreach(Herbs::getInstance()->getAll() as $herb) {
    echo $herb->getName();
}

The Principle

The basic principle is to have a collection class for a data type, which knows all the possible values for that type, and a record class that represents a single value of that type.

The collections and records are distinguished by the return type of their getID() method (int or string).

Implementation

There are two ways to implement collections:

  1. By extending the abstract base classes (e.g. BaseStringPrimaryCollection)
  2. By implementing the interface and trait (e.g. StringPrimaryCollectionTrait)

The second way is useful when working with classes that already extend another class.

The records have no abstract base class, only an interface that contains the getID() method with the relevant return type, e.g. StringPrimaryRecordInterface.

Usage

There are example implementations of the string and integer collections in the unit test classes:

Items with an integer-based ID

Items with a string-based ID

Removing items from a collection

When extending BaseStringPrimaryCollection or BaseIntegerPrimaryCollection, you can remove registered items from within the subclass using the protected unregisterItem() method. It validates that the ID exists (throwing RecordNotExistsException if not) and returns $this for method chaining.

protected function removeObsoleteItem(string $id): self
{
    return $this->unregisterItem($id);
}

NOTE: unregisterItem() is protected — it is intended for use within your subclass only, not as a public API. For publicly removable items, use a basket instead.

Basket collections

These collections allow adding and removing items freely, similar to a basket. They do not contain a fixed set of items, but rather allow dynamically storing and retrieving items.

These classes can be used without extending them, but can be extended to add custom functionality.

use AppUtils\Baskets\GenericIntegerPrimaryBasket;

$products = GenericIntegerPrimaryBasket::create();

$products->addItem(new Product(289, 'Product A'));
$products->addItem(new Product(290, 'Product B'));

$products->removeItem(290);

Dynamic class loading

This specialized collection loads all classes from a folder that match a given interface or class name, instantiates them, and gives access to the resulting objects.

Fueled by AppUtils' ClassHelper class loading mechanism, it is a powerful, fire-and-forget tool that helps with building dynamic applications.

Events

Items in a collection are initialized on demand. To be able to react to this, the collections offer the onItemsInitialized() method, which can be used to register a listener that is called when the items are initialized.

use AppUtils\Collections\Events\ItemsInitializedEvent;use AppUtilsTestClasses\StringPrimaryCollectionImpl;

$collection = new StringPrimaryCollectionImpl();

// Register listeners before accessing the items
$collection->onItemsInitialized(function(ItemsInitializedEvent $event) : void {
    // Do something
});

$collection->getAll(); // This, for example, will trigger the event

mistralys/application-utils-collections 适用场景与选型建议

mistralys/application-utils-collections 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45.33k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mistralys/application-utils-collections 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mistralys/application-utils-collections 我们能提供哪些服务?
定制开发 / 二次开发

基于 mistralys/application-utils-collections 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 45.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 30
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-16