public function modelsCommand($services = [], $namespace = 'App\Models', $optimized = false, $sample = 1000, $db = [])
    {
        if (strpos($namespace, '\\') === false) {
            $namespace = 'App\\' . ucfirst($namespace) . '\\Models';
        }

        foreach ($this->_getServices($services) as $service) {
            /**
             * @var \ManaPHP\Mongodb $mongodb
             */
            $mongodb = $this->_di->getShared($service);

            $defaultDb = $mongodb->getDefaultDb();
            foreach ($defaultDb ? [$defaultDb] : $mongodb->listDatabases() as $cdb) {
                if (in_array($cdb, ['admin', 'local'], true) || ($db && !in_array($cdb, $db, true))) {
                    continue;
                }

                foreach ($mongodb->listCollections($cdb) as $collection) {
                    if (strpos($collection, '.')) {
                        continue;
                    }

                    if (!$docs = $mongodb->aggregate("$cdb.$collection", [['$sample' => ['size' => $sample]]])) {
                        continue;
                    }

                    $plainClass = Text::camelize($collection);
                    $fileName = "@tmp/mongodb_models/$plainClass.php";

                    $this->console->progress(['`:collection` processing...', 'collection' => $collection], '');

                    $fieldTypes = $this->_inferFieldTypes($docs);
                    $modelClass = $namespace . '\\' . $plainClass;
                    $model = $this->_renderModel($fieldTypes, $modelClass, $service, $defaultDb ? $collection : "$cdb.$collection", $optimized);
                    $this->filesystem->filePut($fileName, $model);

                    $this->console->progress([
                        ' `:namespace` collection saved to `:file`',
                        'namespace' => "$cdb.$collection",
                        'file' => $fileName]);

                    $pending_fields = [];
                    foreach ($fieldTypes as $field => $type) {
                        if ($type === '' || strpos($type, '|') !== false) {
                            $pending_fields[] = $field;
                        }
                    }

                    if ($pending_fields) {
                        $this->console->warn(['`:collection` has pending fields: :fields',
                            'collection' => $collection,
                            'fields' => implode(', ', $pending_fields)]);
                    }
                }
            }
        }
    }
public function modelsCommand($services = [], $namespace = 'App\Models', $optimized = false, $sample = 1000, $db = [])
    {
        if (strpos($namespace, '\\') === false) {
            $namespace = 'App\\' . ucfirst($namespace) . '\\Models';
        }

        foreach ($this->_getServices($services) as $service) {
            /**
             * @var \ManaPHP\Mongodb $mongodb
             */
            $mongodb = $this->_di->getShared($service);

            $defaultDb = $mongodb->getDefaultDb();
            foreach ($defaultDb ? [$defaultDb] : $mongodb->listDatabases() as $cdb) {
                if (in_array($cdb, ['admin', 'local'], true) || ($db && !in_array($cdb, $db, true))) {
                    continue;
                }

                foreach ($mongodb->listCollections($cdb) as $collection) {
                    if (strpos($collection, '.')) {
                        continue;
                    }

                    if (!$docs = $mongodb->aggregate("$cdb.$collection", [['$sample' => ['size' => $sample]]])) {
                        continue;
                    }

                    $plainClass = Text::camelize($collection);
                    $fileName = "@tmp/mongodb_models/$plainClass.php";

                    $this->console->progress(['`:collection` processing...', 'collection' => $collection], '');

                    $fieldTypes = $this->_inferFieldTypes($docs);
                    $modelClass = $namespace . '\\' . $plainClass;
                    $model = $this->_renderModel($fieldTypes, $modelClass, $service, $defaultDb ? $collection : "$cdb.$collection", $optimized);
                    $this->filesystem->filePut($fileName, $model);

                    $this->console->progress([
                        ' `:namespace` collection saved to `:file`',
                        'namespace' => "$cdb.$collection",
                        'file' => $fileName]);

                    $pending_fields = [];
                    foreach ($fieldTypes as $field => $type) {
                        if ($type === '' || strpos($type, '|') !== false) {
                            $pending_fields[] = $field;
                        }
                    }

                    if ($pending_fields) {
                        $this->console->warn(['`:collection` has pending fields: :fields',
                            'collection' => $collection,
                            'fields' => implode(', ', $pending_fields)]);
                    }
                }
            }
        }
    }
public function modelsCommand ( $ services = [ ] , $ namespace = 'App\Models' , $ optimized = false , $ sample = 1000 , $ db = [ ] ) { if ( strpos ( $ namespace , '\\' ) === false ) { $ namespace = 'App\\' . ucfirst ( $ namespace ) . '\\Models' ; } foreach ( $ this -> _getServices ( $ services ) as $ service ) { /**
             * @var \ManaPHP\Mongodb $mongodb
             */ $ mongodb = $ this -> _di -> getShared ( $ service ) ; $ defaultDb = $ mongodb -> getDefaultDb ( ) ; foreach ( $ defaultDb ? [ $ defaultDb ] : $ mongodb -> listDatabases ( ) as $ cdb ) { if ( in_array ( $ cdb , [ 'admin' , 'local' ] , true ) || ( $ db && ! in_array ( $ cdb , $ db , true ) ) ) { continue ; } foreach ( $ mongodb -> listCollections ( $ cdb ) as $ collection ) { if ( strpos ( $ collection , '.' ) ) { continue ; } if ( ! $ docs = $ mongodb -> aggregate ( "$cdb.$collection" , [ [ '$sample' => [ 'size' => $ sample ] ] ] ) ) { continue ; } $ plainClass = Text :: camelize ( $ collection ) ; $ fileName = "@tmp/mongodb_models/$plainClass.php" ; $ this -> console -> progress ( [ '`:collection` processing...' , 'collection' => $ collection ] , '' ) ; $ fieldTypes = $ this -> _inferFieldTypes ( $ docs ) ; $ modelClass = $ namespace . '\\' . $ plainClass ; $ model = $ this -> _renderModel ( $ fieldTypes , $ modelClass , $ service , $ defaultDb ? $ collection : "$cdb.$collection" , $ optimized ) ; $ this -> filesystem -> filePut ( $ fileName , $ model ) ; $ this -> console -> progress ( [ ' `:namespace` collection saved to `:file`' , 'namespace' => "$cdb.$collection" , 'file' => $ fileName ] ) ; $ pending_fields = [ ] ; foreach ( $ fieldTypes as $ field => $ type ) { if ( $ type === '' || strpos ( $ type , '|' ) !== false ) { $ pending_fields [ ] = $ field ; } } if ( $ pending_fields ) { $ this -> console -> warn ( [ '`:collection` has pending fields: :fields' , 'collection' => $ collection , 'fields' => implode ( ', ' , $ pending_fields ) ] ) ; } } } } }
generate models file from data files or online data

@param array  $services explicit the mongodb service name
@param string $namespace namespaces of models
@param bool   $optimized output as more methods as possible
@param int    $sample sample size
@param array  $db db name list

@throws \ManaPHP\Mongodb\Exception
generate models file from data files or online data