peekandpoke/slumber-data

Slumber 序列化库之上的ODM

v0.2.2 2018-02-28 13:55 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:49:40 UTC


README

Code Coverage Scrutinizer Code Quality Build Status

Slumber-Data

Slumber-data 是建立在 Slumber 之上的对象-文档-映射器 (ODM)。它底层使用 Slumber 的序列化/反序列化机制。

目前只实现了 MongoDB 存储驱动程序。
理论上,可以实现其他文档数据库的新驱动程序,例如 CouchDB。

注解

Slumber-Data 使用与 Slumber 相同的注解。以下是一些示例

/**
 *
 * Define a compound index on multiple fields 
 *
 * @Slumber\Store\CompoundIndex(
 *   background = true,
 *   def = { "email" : 1, "type" : 1 },
 *   unique = true,
 *   dropDups = true
 * )
 */

class MyClass {

    /**
     * @Slumber\AsString()
     * @Slumber\Store\Indexed(unique = true)
     */
    private $id;
   
    /**
     * @Slumber\AsString()
     * @Slumber\Store\Indexed(unique = true)
     */
    private $email;

    /**
     * @Slumber\AsString()
     */
    private $type;
    
    /* ... */
}

入门

为了使用 Slumber 存储,在开始时需要设置一些事情

// we need a PSR-11 ContainerInterface ... it must be found somewhere in your application
$di = ...; 

// we need a PSR-3 logger ... probably this is present in your application already
$logger = ...; 

// We need a doctrine cache for caching the annotations
// ... otherwise annotations will need to be parsed on each request, which is slow 
// ... ideally APCU as it is the fastest for our purpose
$cache = new ApcuCache();

// we need a doctrine annotation reader
$annotationReader = new CachedReader(new AnnotationReader(), $cache, true);

// SLUMBER: we need an instance of the entity config reader (the one that reads the Slumber annotations) 
$configReader = new MongoDbEntityConfigReaderCached(
    new MongoDbEntityConfigReaderImpl(
        new AnnotatedEntityConfigReader($di, $annotationReader, new MongoDbPropertyMarkerToMapper())
    ),
    $cache,
    'test',  // the cache-prefix
    true     // debug mode
)

// SLUMBER: we need the codec set for serializing / unserializing
$codecSet = new MongoDbCodecSet($di, $configReader, $pool, $storage, $logger)

// SLUMBER: we need a storage instance
$storage  = new StorageImpl($entityPool, $registry);

// we need a mongo db connection
$dbClient = new MongoDB\Client('mongodb://:27017', ['connect' => false]);
$database = $dbClient->selectDatabase("my-database");


// then we need to register repository providers on the storage

$registry->registerProvider(
  // the name in the registry
  "users",                                    
  // FQCNs of all classes stored in this collection
  "[User::class, AdvancedUser::class],                   
  // callback that creates the repository class
  function () use ($entityPool, $codecSet, $database) {   

    // get the collection from the database
    $collection = $database->selectCollection("users");
    // get a reflection of the main class stored in the collection
    $reflect    = new \ReflectionClass(User::class);

    return new EntityRepository(
      "users", 
      new MongoDbStorageDriver($entityPool, $codecSet, $collection, $reflect)
    );
});

核心概念

实体池

RepositoryRegistry

存储驱动程序

索引构建器