PHP code example of peekandpoke / slumber-data

1. Go to this page and download the library: Download peekandpoke/slumber-data library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

peekandpoke / slumber-data example snippets



/**
 *
 * 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;
    
    /* ... */
}



// 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://localhost: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)
    );
});