/**
  * @param string $class
  * @param ODM    $odm
  * @throws SourceException
  */
 public function __construct($class = null, ODM $odm = null)
 {
     if (empty($class)) {
         if (empty(static::DOCUMENT)) {
             throw new SourceException("Unable to create source without associate class.");
         }
         $class = static::DOCUMENT;
     }
     $this->class = $class;
     $this->odm = $this->saturate($odm, ODM::class);
     $this->setSelector($this->odm->selector($this->class));
 }
Esempio n. 2
0
 /**
  * Find document using it's primary key.
  *
  * @param mixed $mongoID   Valid MongoId, string value must be automatically converted to
  *                         MongoId object.
  * @param bool  $keepChain Only same class or child must be returned, parent document must be
  *                         ignored.
  * @return Document|null
  * @throws ODMException
  */
 public static function findByPK($mongoID, $keepChain = true)
 {
     if (!($mongoID = ODM::mongoID($mongoID))) {
         return null;
     }
     return static::findOne(['_id' => $mongoID], [], $keepChain);
 }
 /**
  * {@inheritdoc}
  *
  * @return array|Document
  * @throws ODMException
  * @throws DefinitionException
  */
 public function current()
 {
     $fields = $this->cursor->current();
     if (empty($this->class)) {
         return $fields;
     }
     return $fields ? $this->odm->document($this->class, $fields) : null;
 }
Esempio n. 4
0
 /**
  * @param Debugger           $debugger
  * @param ODM                $odm
  * @param ContainerInterface $container
  * @param ClassLocator       $locator
  */
 public function perform(Debugger $debugger, ODM $odm, ContainerInterface $container, ClassLocator $locator)
 {
     //We don't really need location errors here
     $locator->setLogger(new NullLogger());
     $benchmark = $debugger->benchmark($this, 'update');
     $builder = $odm->schemaBuilder($locator);
     //To make builder available for other commands (in sequence)
     $container->bind(get_class($builder), $builder);
     if (!$this->option('sync')) {
         $this->writeln("<comment>Silent mode on, no mongo indexes to be created.</comment>");
     }
     //Save and syncronize
     $odm->updateSchema($builder, $this->option('sync'));
     $elapsed = number_format($debugger->benchmark($this, $benchmark), 3);
     $countModels = count($builder->getDocuments());
     $this->write("<info>ODM Schema has been updated: <comment>{$elapsed} s</comment>");
     $this->writeln(", found documents: <comment>{$countModels}</comment></info>");
 }
Esempio n. 5
0
 /**
  * Fetch or create instance of document based on specified offset.
  *
  * @param int $offset
  * @return DocumentEntity
  */
 private function getDocument($offset)
 {
     /**
      * @var array|Document $document
      */
     $document = $this->documents[$offset];
     if ($document instanceof CompositableInterface) {
         return $document;
     }
     //Trying to create using ODM
     return $this->documents[$offset] = $this->odm->document($this->class, $document, $this);
 }
Esempio n. 6
0
 /**
  * Create instance of inspector associated with ORM and ODM entities.
  *
  * @param InspectionsConfig $config
  * @param ODM               $odm
  * @param ORM               $orm
  * @return Inspector
  */
 protected function createInspector(InspectionsConfig $config, ODM $odm, ORM $orm)
 {
     if ($this->container->has(\Spiral\ODM\Entities\SchemaBuilder::class)) {
         $odmBuilder = $this->container->get(\Spiral\ODM\Entities\SchemaBuilder::class);
     } else {
         $odmBuilder = $odm->schemaBuilder();
     }
     if ($this->container->has(\Spiral\ORM\Entities\SchemaBuilder::class)) {
         $ormBuilder = $this->container->get(\Spiral\ORM\Entities\SchemaBuilder::class);
     } else {
         $ormBuilder = $orm->schemaBuilder();
     }
     return new Inspector($config, array_merge($odmBuilder->getDocuments(), $ormBuilder->getRecords()));
 }
 /**
  * Create every requested collection index.
  *
  * @throws \MongoException
  */
 public function createIndexes()
 {
     foreach ($this->getCollections() as $collection) {
         if (empty($indexes = $collection->getIndexes())) {
             continue;
         }
         $odmCollection = $this->odm->database($collection->getDatabase())->selectCollection($collection->getName());
         foreach ($indexes as $index) {
             $options = [];
             if (isset($index[DocumentEntity::INDEX_OPTIONS])) {
                 $options = $index[DocumentEntity::INDEX_OPTIONS];
                 unset($index[DocumentEntity::INDEX_OPTIONS]);
             }
             $odmCollection->createIndex($index, $options);
         }
     }
 }
Esempio n. 8
0
 /**
  * Create every requested collection index.
  *
  * @throws \MongoException
  */
 public function createIndexes()
 {
     foreach ($this->getCollections() as $collection) {
         if (empty($indexes = $collection->getIndexes())) {
             continue;
         }
         //We can safely create odm Collection here, as we not going to use functionality requires
         //finalized schema
         $odmCollection = $this->odm->db($collection->getDatabase())->odmCollection($collection->getName());
         foreach ($indexes as $index) {
             $options = [];
             if (isset($index[Document::INDEX_OPTIONS])) {
                 $options = $index[Document::INDEX_OPTIONS];
                 unset($index[Document::INDEX_OPTIONS]);
             }
             $odmCollection->ensureIndex($index, $options);
         }
     }
 }
Esempio n. 9
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $builder = $this->odm->updateSchema(null, true);
     $countModels = count($builder->getDocuments());
     $this->info("<info>ODM Schema has been updated, found documents: <comment>{$countModels}</comment></info>");
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  *
  * Accessor options include field type resolved by DocumentSchema.
  *
  * @throws ODMException
  * @throws DefinitionException
  */
 protected function createAccessor($accessor, $value)
 {
     $options = null;
     if (is_array($accessor)) {
         list($accessor, $options) = $accessor;
     }
     if ($accessor == ODM::CMP_ONE) {
         //Pointing to document instance
         $accessor = $this->odm->document($options, $value, $this);
     } else {
         //Additional options are supplied for CompositableInterface
         $accessor = new $accessor($value, $this, $this->odm, $options);
     }
     return $accessor;
 }
Esempio n. 11
0
 /**
  * MongoDatabase instance.
  *
  * @return MongoDatabase
  */
 protected function mongoDatabase()
 {
     return $this->odm->db($this->database);
 }
Esempio n. 12
0
 /**
  * @param FilesInterface $files
  * @param ODM            $odm
  * @param array          $options
  */
 public function __construct(FilesInterface $files, ODM $odm, array $options)
 {
     parent::__construct($files, $options);
     $this->database = $odm->database($this->options['database']);
 }
Esempio n. 13
0
 /**
  * @param FilesInterface $files
  * @param ODM            $odm
  */
 public function perform(FilesInterface $files, ODM $odm)
 {
     $umlExporter = new UmlExporter($odm->schemaBuilder());
     $files->write($this->argument('filename'), $umlExporter->generate());
     $this->writeln("<info>UML schema has been  successfully exported:</info> {$this->argument('filename')}");
 }