potfur/stash
Composer 安装命令:
composer require potfur/stash
包简介
Stash: Mongo ODM
README 文档
README
Stash is an object-document mapper for MongoDB written in PHP. It adds a fully transparent persistence layer while still preserving MongoDB's ease of use and way of handling data.
This means that MongoDB can be used almost in the exact same way as it would be used with arrays. The small, but important, difference here is that instead of returning plain arrays, Stash will return objects (entities). And of course, Stash not only returns entities, but it also stores them.
Example
Model definitions:
$models = new \Stash\ModelCollection(); $models->register( new \Stash\Model\Model( '\Order', [ new \Stash\Converter\Type\Id(), new \Stash\Converter\Type\Document('customer'), new \Stash\Converter\Type\ArrayOf('items', Fields::TYPE_DOCUMENT) ] ), 'order' ); $models->register( new \Stash\Model\ '\OrderItem', [ new \Stash\Converter\Type\Scalar('name', Fields::TYPE_STRING), new \Stash\Converter\Type\Scalar('amount', Fields::TYPE_INTEGER), new \Stash\Converter\Type\Scalar('cost', Fields::TYPE_INTEGER) ] ) ); $models->register( new \Stash\Model\ '\Voucher', [ new \Stash\Converter\Type\Scalar('name', Fields::TYPE_STRING), new \Stash\Converter\Type\Scalar('cost', Fields::TYPE_INTEGER) ] ) ); $models->register( new \Stash\Model\ '\Customer', [ new \Stash\Converter\Type\Scalar('name', Fields::TYPE_STRING), new \Stash\Converter\Type\Document('address') ] ) ); $models->register( new \Stash\Model\ '\CustomerAddress', [ new \Stash\Converter\Type\Scalar('address', Fields::TYPE_STRING), new \Stash\Converter\Type\Scalar('city', Fields::TYPE_STRING), new \Stash\Converter\Type\Scalar('zip', Fields::TYPE_STRING) ] ) );
Database connection:
$types = [ new \Stash\Converter\Type\IdType(), new \Stash\Converter\Type\BooleanType(), new \Stash\Converter\Type\IntegerType(), new \Stash\Converter\Type\DecimalType(), new \Stash\Converter\Type\StringType(), new \Stash\Converter\Type\DateType(), new \Stash\Converter\Type\ArrayType(), new \Stash\Converter\Type\DocumentType() ]; $proxyAdapter = new \Stash\ProxyAdapter(new \ProxyManager\Factory\LazyLoadingValueHolderFactory()); $converter = new \Stash\Converter\Converter($types); $referencer = new \Stash\ReferenceResolver($models); $documentConverter = new \Stash\DocumentConverter($converter, $referencer, $models, $proxyAdapter); $eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); $connection = new \Stash\Connection(new \MongoClient(), $documentConverter, $eventDispatcher); $connection->selectDB('test');
Entity creation and storage:
class Order { private $id; private $customer; private $items; public function __construct($customer, $items) { $this->customer = $customer; $this->items = $items; } } class OrderItem { private $name; private $amount; private $cost; public function __construct($name, $amount, $cost) { $this->name = $name; $this->amount = $amount; $this->cost = $cost; } } class Voucher { private $name; private $discount; public function __construct($name, $discount) { $this->name = $name; $this->discount = $discount; } } class Customer { private $name; private $address; public function __construct($name, CustomerAddress $address) { $this->$name = $name; $this->address = $address; } } class CustomerAddress { private $address; private $city; private $zip; public function __construct($address, $city, $zip) { $this->address = $address; $this->city = $city; $this->zip = $zip; } } $order = new Order( new Customer('Joe Doe', new CustomerAddress('Mongo alley', 'Somewhere', '12345')), [ new OrderItem('Foos', 10, 1000), new Voucher('Voucher', 250) ] ); $connection->getCollection('order')->save($order);
And this is the stored MongoDB's semi-JSON representation.
When saving objects (entities), Stash adds the _class field, where it stores the class name
{
"_id" : ObjectId("55746f4f87dee7bc0b000033"),
"_class" : "Order",
"customer" : {
"_class" : "Customer",
"address" : {
"_class" : "CustomerAddress",
"address" : "Mongo alley",
"city" : "Somewhere",
"zip" : "12345"
}
},
"items" : [
{
"_class" : "OrderItem",
"name" : "Foos",
"amount" : 10,
"cost" : 1000
},
{
"_class" : "Voucher",
"name" : "Voucher",
"discount" : 250
}
]
}
Event subscription
Stash uses Symfonys Event Dispatcher for dispatching events.
find.aftertriggered after reading document from database and converting it to entity instancepersist.beforeis triggered before converting entity to array documentpersist.afterafter document was saved (and updated with new_idif needed)remove.beforetriggered before entity is removed from database
Each event is represented with Event entity, similar to Symfony's Event but with two methods getPayload and setPayload to manage subject entity.
$subscriber = new \Fake\EventSubscriber( [ \Stash\Events::FIND_AFTER, \Stash\Events::PERSIST_BEFORE, \Stash\Events::PERSIST_AFTER, \Stash\Events::REMOVE_BEFORE ] ); $eventDispatcher->addSubscriber($subscriber);
Configuring proxy
By default, all required proxy classes are generated at runtime. Generation uses a lot of reflection and it may cause poor performance. To prevent this, proxy generator needs to be configured to reuse generated proxies.
$config = new \ProxyManager\Configuration(); $config->setProxiesTargetDir(__DIR__ . '/generated/proxy/); spl_autoload_register($config->getProxyAutoloader()); $proxyAdapter = new \Stash\ProxyAdapter($config);
potfur/stash 适用场景与选型建议
potfur/stash 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 06 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「persistence」 「document」 「mongo」 「odm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 potfur/stash 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 potfur/stash 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 potfur/stash 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
This bundle provides tools to build persistence-agnostic storage layer.
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-10