/** * Prepares the field to have a sequence. If $value is zero or not defined * a new auto-increment number will be "generated" for the collection of * the schema. The sequence generation is done by the SequenceService. * * @param int|null $value Value that will be evaluated. * * @return int */ public function sequence(int $value = null) { if ($value) { return $value; } return Ioc::make(SequenceService::class)->getNextValue($this->collection ?: $this->entityClass); }
/** * Constructs a new Mongolid connection. It uses the same constructor * parameters as the original MongoDB\Client constructor. * * @see http://php.net/manual/en/mongodb-driver-manager.construct.php * * @param string $server The specified connection string. * @param array $options The mongodb client options. * @param array $driver_options The mongodb driver options when opening a connection. */ public function __construct(string $server = 'mongodb://localhost:27017', array $options = ['connect' => true], array $driver_options = []) { // In order to work with PHP arrays instead of with objects $driver_options['typeMap'] = ['array' => 'array', 'document' => 'array']; $this->findDefaultDatabase($server); $parameters = [$server, $options, $driver_options]; $this->rawManager = Ioc::make(Manager::class, $parameters); $this->rawConnection = Ioc::make(Client::class, $parameters); }
public function testShouldRegisterConnectorWithUsernameAndPassword() { // Set $provider = new MongolidServiceProvider($this->app); config(['database.mongodb.default.database' => 'databaseName', 'database.mongodb.default.username' => 'us3r', 'database.mongodb.default.password' => 'p455']); // Actions $provider->registerConnector(); $pool = Ioc::make(Pool::class); // Assertions $this->assertEquals('databaseName', $pool->getConnection()->defaultDatabase); }
/** * Removes a document _id reference from an attribute. It will remove the * _id of the given $obj from inside the given $field. * * @param string $field Field where the reference is stored. * @param mixed $obj Document, model instance or _id that have been referenced by $field. * * @return void */ public function detach(string $field, &$obj) { $embedder = Ioc::make(DocumentEmbedder::class); $embedder->detach($this, $field, $obj); }
/** * Iterator interface current. Return a model object * with cursor document. (used in foreach). * * @return mixed */ public function current() { if (!$this->valid()) { return; } $document = $this->items[$this->position]; if ($document instanceof $this->entityClass) { return $document; } $schema = $this->getSchemaForEntity(); $entityAssembler = Ioc::make(EntityAssembler::class, [$schema]); return $entityAssembler->assemble($document, $schema); }
/** * Unserializes this object. Re-creating the database connection. * * @param mixed $serialized Serialized cursor. * * @return void */ public function unserialize($serialized) { $attributes = unserialize($serialized); $conn = Ioc::make(Pool::class)->getConnection(); $db = $conn->defaultDatabase; $collectionObject = $conn->getRawConnection()->{$db}->{$attributes['collection']}; foreach ($attributes as $key => $value) { $this->{$key} = $value; } $this->collection = $collectionObject; }
/** * Returns the a valid instance from Ioc. * * @throws NoCollectionNameException Throws exception when has no collection filled. * * @return mixed */ private static function getDataMapperInstance() { $instance = Ioc::make(get_called_class()); if (!$instance->getCollectionName()) { throw new NoCollectionNameException(); } return $instance->getDataMapper(); }
/** * Assembly multiple documents for the given $schemaClass recursively. * * @param mixed $value A value of an embeded field containing entity data to be assembled. * @param string $schemaClass The schemaClass to be used when assembling the entities within $value. * * @return mixed */ protected function assembleDocumentsRecursively($value, string $schemaClass) { $value = (array) $value; if (empty($value)) { return; } $schema = Ioc::make($schemaClass); $assembler = Ioc::make(self::class); if (!isset($value[0])) { $value = [$value]; } foreach ($value as $key => $subValue) { $value[$key] = $assembler->assemble($subValue, $schema); } return $value; }
/** * Execute the BulkWrite, using a connection from the Pool. * The collection is inferred from entity's collection name. * * @param int $writeConcern * * @return \MongoDB\Driver\WriteResult */ public function execute($writeConcern = 1) { $connection = Ioc::make(Pool::class)->getConnection(); $manager = $connection->getRawManager(); $namespace = $connection->defaultDatabase . '.' . $this->schema->collection; return $manager->executeBulkWrite($namespace, $this->getBulkWrite(), new WriteConcern($writeConcern)); }
/** * Constructs a connection pool. */ public function __construct() { $this->connections = Ioc::make('SplQueue'); }
/** * Create a new instance of the model. * * @return \MongolidLaravel\MongoLidModel */ protected function createModel() { $class = '\\' . ltrim($this->model, '\\'); return Ioc::make($class); }
/** * Retrieves a DataMapper for the given $entityClass. This can only be done * if the Schema for that entity has been previously registered with * registerSchema() method. * * @param string $entityClass Class of the entity that needs to be mapped. * * @return DataMapper|null DataMapper configured for the $entityClass. */ public function getMapper(string $entityClass) { if (isset($this->schemas[$entityClass])) { $dataMapper = Ioc::make(DataMapper::class); $dataMapper->setSchema($this->schemas[$entityClass] ?? null); return $dataMapper; } }
/** * Triggers an event. May return if that event had success. * * @param string $event Identification of the event. * @param mixed $entity Event payload. * @param bool $halt True if the return of the event handler will be used in a conditional. * * @return mixed Event handler return. */ protected function fireEvent(string $event, $entity, bool $halt = false) { $event = "mongolid.{$event}: " . get_class($entity); $this->eventService ? $this->eventService : ($this->eventService = Ioc::make(EventTriggerService::class)); return $this->eventService->fire($event, $entity, $halt); }
/** * Generates an unique cache key for the cursor in it's current state. * * @return string Cache key to identify the query of the current cursor. */ protected function generateCacheKey() : string { $serializer = Ioc::make(Serializer::class); return sprintf('%s:%s:%s', $this->command, $this->collection->getNamespace(), md5($serializer->serialize($this->params))); }
/** * Instantiate another SchemaMapper with the given $schemaClass and maps * the given $value. * * @param mixed $value Value that will be mapped. * @param string $schemaClass Class that will be passed to the new SchemaMapper constructor. * * @return mixed */ protected function mapToSchema($value, string $schemaClass) { $value = (array) $value; $schema = Ioc::make($schemaClass); $mapper = Ioc::make(self::class, [$schema]); if (!isset($value[0])) { $value = [$value]; } foreach ($value as $key => $subValue) { $value[$key] = $mapper->map($subValue); } return $value; }