/** * Create new Rows * * @param string $entityName Entity Name * @param array $entityConfig Entity Configuration * @param array $foreignData The Foreign/Parent Data */ protected function _rows($entityName, $entityConfig, $foreignData = [], $related = false) { $f = []; $model = zbase_data_get($entityConfig, 'model', null); $tableName = zbase_data_get($entityConfig, 'table.name', null); $primaryKey = zbase_data_get($entityConfig, 'table.primaryKey', null); $modelName = zbase_class_name($model); $columns = zbase_data_get($entityConfig, 'table.columns', []); if (method_exists($modelName, 'tableColumns')) { $columns = $modelName::tableColumns($columns); } $timestamp = zbase_data_get($entityConfig, 'table.timestamp', []); $softDelete = zbase_data_get($entityConfig, 'table.softDelete', []); $alphaId = zbase_data_get($entityConfig, 'table.alphaId', false); $relations = zbase_data_get($entityConfig, 'relations', []); if (method_exists($modelName, 'tableRelations')) { $relations = $modelName::tableRelations($relations); } $now = \Carbon\Carbon::now(); foreach ($columns as $columnName => $column) { if ($columnName != $primaryKey) { $columnModel = zbase_data_column($columnName, $column); $f[$columnName] = $columnModel->faker(); } } if ($timestamp) { $f['created_at'] = $now; $f['updated_at'] = $now; } if ($softDelete) { $f['deleted_at'] = rand(0, 1) == 1 ? $now : null; } if (!empty($foreignData)) { $f = array_replace($f, $foreignData); } $entityModel = zbase_entity($entityName); if (!empty($entityModel)) { $f = $entityModel->fixDataArray($f, 'insert'); } $insertedId = \DB::table($tableName)->insertGetId($f); if (!empty($alphaId)) { \DB::table($tableName)->where($primaryKey, $insertedId)->update(['alpha_id' => zbase_generate_hash([rand(1, 1000), time(), rand(1, 1000)], $entityName)]); } if (!empty($primaryKey)) { $f[$primaryKey] = $insertedId; } if (!empty($relations) && empty($related)) { $this->_relations($relations, $f); } }
/** * Return the Entity Model of a given entityName * * @param string $entityName Entity name * @param array $entityConfig EntityConfiguration * @param boolean|string $newInstance will create new instance. * @return Zbase\Entity\Entity */ public function entity($entityName, $entityConfig = [], $newInstance = true) { if (empty($newInstance)) { if (!empty($this->entityModels[$entityName])) { return $this->entityModels[$entityName]; } } if (empty($entityConfig)) { $entityConfig = zbase_config_get('entity.' . $entityName, []); } if (!empty($entityConfig)) { $modelName = zbase_class_name(!empty($entityConfig['model']) ? $entityConfig['model'] : null); if (!empty($modelName)) { if (!empty($newInstance)) { return new $modelName(); } return $this->entityModels[$entityName] = new $modelName(); } throw new Exceptions\ConfigNotFoundException('Entity "model" configuration for "' . $entityName . '" not found in ' . __CLASS__); } //$value = app()['config']['entity']; //dd($value, zbase_config_get('entity'), $entityName, $entityConfig); throw new Exceptions\ConfigNotFoundException('Entity configuration for "' . $entityName . '" not found in ' . __CLASS__); }
/** * Reverse the migrations. * * @return void */ public function down() { // $migrateCommand = app('command.migrate'); if (!zbase_is_dev()) { // $migrateCommand->info('You are in PRODUCTION Mode. Cannot drop tables.'); return false; } //echo " - Dropping\n"; //$migrateCommand->info('- Dropping'); $entities = zbase_config_get('entity', []); if (!empty($entities)) { \DB::statement('SET FOREIGN_KEY_CHECKS = 0'); foreach ($entities as $entity) { $enable = zbase_data_get($entity, 'enable', false); $model = zbase_data_get($entity, 'model', null); if (is_null($model)) { continue; } $modelName = zbase_class_name($model); $isPostModel = zbase_data_get($entity, 'post', false); if (!empty($isPostModel)) { $postModel = zbase_object_factory($modelName); if ($postModel instanceof \Zbase\Post\PostInterface) { $entity = $postModel->postTableConfigurations($entity); } } else { if (method_exists($modelName, 'entityConfiguration')) { $entity = $modelName::entityConfiguration($entity); } } $tableName = zbase_data_get($entity, 'table.name', null); if (!empty($enable) && !empty($tableName)) { if (Schema::hasTable($tableName)) { Schema::drop($tableName); // echo " -- Dropped " . $tableName . "\n"; //$migrateCommand->info(' -- Dropped ' . $tableName); } } } \DB::statement('SET FOREIGN_KEY_CHECKS = 1'); } }
/** * Create a controller class name based from a given controller name * zbase_controller_create_name(Zbase\Http\Controllers\__FRAMEWORK__\PageController::class); * output: Zbase\Http\Controllers\Laravel\PageController::class * * @param string $name * @return string */ function zbase_controller_create_name($name) { return zbase_class_name($name); }