carrooi/labels
Composer 安装命令:
composer require carrooi/labels
包简介
Labels module for Nette
README 文档
README
Labels module for Nette framework.
Now any doctrine entity can be turned to "labelable" entity (imagine your gmail mails with labels).
Installation
$ composer require carrooi/labels
$ composer update
Configuration
extensions: labels: Carrooi\Labels\DI\LabelsExtension labels: ownerClass: App\Model\Entities\User labelItemClass: App\Model\Entities\LabelItem entities: App\Model\Entities\Mail: mail App\Model\Entities\Article: article
- ownerClass: Most probably your
Userentity - labelItemClass: Entity with associations between your labels and labelable entities
- entities: List of labelable entities with name of column in your
LabelItementity
Owner entity
Owner entity must implement Carrooi\Labels\Model\Entities\ILabelOwner interface with only one method:
getId(): returns identifier
namespace App\Model\Entities; use Carrooi\Labels\Model\Entities\ILabelOwner; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @author David Kudera */ class User implements ILabelOwner { // ... /** * @return int */ public function getId() { return $this->id; } }
LabelItem entity
Entity which must implement Carrooi\Labels\Model\Entities\ILabelItem interface with these methods:
getId(): returns identifiergetLabel(): returnsCarrooi\Labels\Model\Entities\LabelentitysetLabel(): setsCarrooi\Labels\Model\Entities\Labelentity
Instead of implementing last two methods on your own, you can use prepared Carrooi\Labels\Model\Entities\TLabelItem trait.
namespace App\Model\Entities; use Carrooi\Labels\Model\Entities\ILabelItem; use Carrooi\Labels\Model\Entities\TLabelItem; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @author David Kudera */ class LabelItem implements ILabelItem { use TLabelItem; /** * @ORM\Id * @ORM\Column(type="integer") * @var int */ private $id; /** * @ORM\ManyToOne(targetEntity="\App\Model\Entities\Mail") * @var \App\Model\Entities\Mail */ private $mail; /** * @ORM\ManyToOne(targetEntity="\App\Model\Entities\Article") * @var \App\Model\Entities\Article */ private $article; /** * @return int */ public function getId() { return $this->id; } // ... other getters and setters for mail and article fields }
Labelable entity
Last thing is to update your labelable entities, eg. our Mail entity.
Each labelable entity must implement Carrooi\Labels\Model\Entities\ILabelableEntity interface with these methods:
getId(): returns identifiergetLabelItems(): returns array ofCarrooi\Labels\Model\Entities\ILabelItementitiesaddLabelItem(): adds nowCarrooi\Labels\Model\Entities\ILabelItementity to label items collectionremoveLabelItem(): removesCarrooi\Labels\Model\Entities\ILabelItementity from label items collection
Again you don't need to implement these methods (except for getId()) on your own, but simply use Carrooi\Labels\Model\Entities\TLabelable trait.
namespace CarrooiTests\LabelsApp\Model\Entities; use Carrooi\Labels\Model\Entities\ILabelableEntity; use Carrooi\Labels\Model\Entities\TLabelable; use Kdyby\Doctrine\Entities\Attributes\Identifier; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @author David Kudera */ class Article implements ILabelableEntity { use TLabelable; // ... your own fields /** * @return int */ public function getId() { return $this->id; } // ... some other getters and setters }
Usage
The main idea is that you have more label namespaces in which you (or your users) can create new labels. Of course if you want, you can have just one main namespace and all labels in that namespace. Example of this aproach is eg. GitHub where your labels are shared between issues and pull requests.
These namespaces will be probably "hard-defined" in your project and database.
Label namespaces facade
For working with namespaces you can use registered service Carrooi\Labels\Model\Facades\LabelNamespacesFacade.
Create new namespace:
$namespace = $namespaces->addNamespace('articles');
Find namespace:
$namespace = $namespaces->findNamespace('articles');
Get all namespaces:
foreach ($namespaces->getNamespaces() as $namespace) { // ... }
Rename namespace:
$namespaces->renameNamespace($namespace, 'mails');
Remove namespace:
$namespaces->removeNamespace($namespace);
Labels facade
Now you can let your users create own labels. There is Carrooi\Labels\Model\Facades\LabelsFacade service for that.
Creating label:
$label = $labels->addLabel($namespace, $owner, 'Best articles', 'best');
Last argument is for "system name" of label and is not required. This can come in handy if you have some default labels and you want to work with them later.
Get all labels:
foreach ($labels->getLabels($namespace, $owner) as $label) { // ... }
Find label by system name:
$label = $labels->findLabel($namespace, $owner, 'best');
Find label by id:
$label = $labels->findLabelById($id);
Rename label:
$labels->renameLabel($label, 'Super articles', 'super');
You can replace 2nd and 3rd arguments with nulls.
Remove label:
$labels->removeLabel($label);
Label items facade
Last facade class is Carrooi\Labels\Model\Facades\LabelItemsFacade which is again registered as service in DI container.
With this service you can manage actual labelable entities in created labels.
Add labelable item to label:
$labelItems->addItemToLabel($article, $label);
Find label item entity by labelable item:
$labelItem = $labelItems->findItem($article, $label);
Is labelable item in label?:
$labelItems->isItemInLabel($article, $label);
Get all label items in label:
foreach ($labelItems->getItems($label) as $labelItem) { // ... }
Get labelable items by type:
foreach ($labelItems->getItemsByType($label, Article::class) as $article) { // ... }
Remove labelable item from label:
$labelItems->removeItemFromLabel($label, $item);
Changelog
- 1.0.0
- Initial version
carrooi/labels 适用场景与选型建议
carrooi/labels 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nette」 「labels」 「carrooi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 carrooi/labels 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 carrooi/labels 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 carrooi/labels 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP library for integrating 54grad.de's vehicle labeling services.
Nette Framework adapter for I18n package
Build forms from schema
Asynchronous MQTT client built on React
Modular security system for Nette framework
PDF text extractor
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-05