public function create($resourceName, $module = null) { parent::create($resourceName . 's', true, $module); $this->_resourceName = $resourceName; $this->_moduleName = $module; $modelProvider = new Zend_Tool_Project_Provider_Model(); $modelProvider->setRegistry($this->_registry); $modelProvider->create($this->_resourceName, $this->_moduleName); $dbTableProvider = new Zend_Tool_Project_Provider_DbTable(); $dbTableProvider->setRegistry($this->_registry); $dbTableProvider->create($this->_resourceName, $this->_resourceName . 's', $this->_moduleName); $formProvider = new Zend_Tool_Project_Provider_Form(); $formProvider->setRegistry($this->_registry); $formProvider->create($this->_resourceName, $this->_moduleName); $response = $this->_registry->getResponse(); try { $controllerResource = self::createResource($this->_loadedProfile, $resourceName, $module); //index created on superclass::create $this->_createAction('new'); $this->_createAction('edit'); $this->_createAction('destroy'); /* $testControllerResource = Zend_Tool_Project_Provider_Test::createApplicationResource( $this->_loadedProfile, $name, 'update', $module); $response->appendContent('Creating a controller test file at ' . $testControllerResource->getContext()->getPath()); $testControllerResource->create();*/ } catch (Exception $e) { $response->setException($e); return; } }
/** * * @param string $table * @param string $adapter * @param string $module * @return void * @throws Zend_Tool_Project_Exception * @throws Exception */ public function create($table, $adapter, $module = null) { $name = ZendT_Tool_ModelTProvider::convertTableNameToClassName($table); $this->_print(' Criando Models '); $this->_print('name: ' . $name . ' | table: ' . $table . ' | adapter : ' . $adapter . ' | module: ' . $module); $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); $applicationConfigResource = $this->_loadedProfile->search('ApplicationConfigFile'); if (!$applicationConfigResource) { throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.'); } $zf = $applicationConfigResource->getAsZendConfig(); $_configDb = $zf->resources->multidb->{$adapter}; if (!$_configDb) { throw new Zend_Tool_Project_Exception('Adapter not found in config application "resources.multidb.' . $adapter . '" .'); } $configDb = array(); $configDb['host'] = $_configDb->host; $configDb['username'] = $_configDb->username; $configDb['password'] = $_configDb->password; $configDb['dbname'] = $_configDb->dbname; $this->_dbAdapter = Zend_Db::factory($_configDb->adapter, $configDb); $sql = "SELECT uccfk.column_name\n ,uccpk.table_name table_name_pk\n ,NULL object_name_pk\n ,uccpk.column_name column_name_pk\n FROM user_cons_columns uccfk\n ,user_constraints uc\n ,user_cons_columns uccpk\n WHERE uccfk.constraint_name = uc.constraint_name\n AND uc.r_constraint_name = uccpk.constraint_name\n AND uc.constraint_type = 'R'\n AND uccfk.table_name = '" . strtoupper($table) . "'"; $referenceMap = $this->_dbAdapter->fetchAll($sql); foreach ($referenceMap as &$reference) { $reference['OBJECT_NAME_PK'] = $this->getObjectName($reference['TABLE_NAME_PK']); } $sql = "SELECT distinct uccfk.TABLE_NAME\n FROM user_cons_columns uccfk\n ,user_constraints uc\n ,user_cons_columns uccpk\n WHERE uccfk.constraint_name = uc.constraint_name\n AND uc.r_constraint_name = uccpk.constraint_name\n AND uc.constraint_type = 'R'\n AND uccpk.table_name = '" . strtoupper($table) . "'"; $dependentTables = $this->_dbAdapter->fetchAll($sql); foreach ($dependentTables as &$dependentTable) { $dependentTable['OBJECT_NAME'] = $this->getObjectName($dependentTable['TABLE_NAME']); } // Check that there is not a dash or underscore, return if doesnt match regex if (preg_match('#[_-]#', $name)) { throw new Zend_Tool_Project_Provider_Exception('DbTable names should be camel cased.'); } $originalName = $name; $name = ucfirst($name); if ($table == '') { throw new Zend_Tool_Project_Provider_Exception('You must provide both the DbTable name as well as the actual db table\'s name.'); } /*if (Zend_Tool_Project_Provider_DbTable::hasResource($this->_loadedProfile, $name, $module)) { throw new Zend_Tool_Project_Provider_Exception('This project already has a DbTable named ' . $name); }*/ // get request/response object $request = $this->_registry->getRequest(); $response = $this->_registry->getResponse(); // alert the user about inline converted names $tense = $request->isPretend() ? 'would be' : 'is'; if ($name !== $originalName) { $response->appendContent('Note: The canonical model name that ' . $tense . ' used with other providers is "' . $name . '";' . ' not "' . $originalName . '" as supplied', array('color' => array('yellow'))); } /** * Cria o DB Table */ try { $tableResource = Zend_Tool_Project_Provider_DbTable::createResource($this->_loadedProfile, $name, $table, $module); } catch (Exception $e) { $response = $this->_registry->getResponse(); $response->setException($e); return; } // do the creation if ($request->isPretend()) { $response->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath()); } else { $response->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath()); $tableResource->create(); $this->_storeProfile(); $fileName = $tableResource->getContext()->getPath(); $dbTableContext = new ZendT_Tool_Context_DbTableModelFile(); $this->_dbAdapter->describeTable($originalName); $data = array(); $data['modelName'] = $name; $data['moduleName'] = $module; $data['tableName'] = $table; $data['adapter'] = $adapter; $data['referenceMap'] = $referenceMap; $data['dependentTables'] = $dependentTables; $dbTableContext->setProperties($data); file_put_contents($fileName, "<?php \n" . $dbTableContext->getContents() . "\n?>"); /** * Configura o nome do objeto dentro dos comentários * da tabela */ $this->setObjectName($table, ZendT_Tool_ModelTProvider::convertTableNameToObjectName($module, $table)); } /** * Cria o Model */ try { $modelResource = Zend_Tool_Project_Provider_Model::createResource($this->_loadedProfile, $name, $module); } catch (Exception $e) { $response->setException($e); return; } // do the creation if ($request->isPretend()) { $response->appendContent('Would create a model at ' . $modelResource->getContext()->getPath()); } else { $response->appendContent('Creating a model at ' . $modelResource->getContext()->getPath()); $modelResource->create(); $this->_storeProfile(); $fileName = $modelResource->getContext()->getPath(); $modelContext = new ZendT_Tool_Context_ModelFile(); $data = array(); $data['modelName'] = $name; $data['moduleName'] = $module; $modelContext->setProperties($data); file_put_contents($fileName, "<?php \n" . $modelContext->getContents() . "\n?>"); } $this->_print(' Finalizado Models '); }
/** * Create a new model * * @param string $name * @param string $module */ public function create($name, $commaSeparatedFields = '', $module = null) { $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); if (!is_dir('spec')) { throw new ProviderException('Please run zf generate phpspec, to create the environment'); } $originalName = $name; $name = UCFirst::apply($name); $tableName = Pluralize::apply($name); $dbTableName = strtolower($tableName); // determine if testing is enabled in the project // Zend_Tool_Project_Provider_Test::isTestingEnabled( // $this->_loadedProfile // ); $testingEnabled = false; $testModelResource = null; // Check that there is not a dash or underscore, // return if doesnt match regex if (preg_match('#[_-]#', $name)) { throw new ProviderException('Model names should be camel cased.'); } if (self::hasResource($this->_loadedProfile, $name, $module)) { throw new ProviderException('This project already has a model named ' . $name); } // get request/response object $request = $this->_registry->getRequest(); $response = $this->_registry->getResponse(); // alert the user about inline converted names $tense = $request->isPretend() ? 'would be' : 'is'; if ($name !== $originalName) { $response->appendContent('Note: The canonical model name that ' . $tense . ' used with other providers is "' . $name . '";' . ' not "' . $originalName . '" as supplied', array('color' => array('yellow'))); } $commaSeparatedFields = trim($commaSeparatedFields); $fields = empty($commaSeparatedFields) ? array() : explode(',', $commaSeparatedFields); try { $modelResource = self::createResource($this->_loadedProfile, $name, $fields, $module); $mapperResource = parent::createResource($this->_loadedProfile, $name . "Mapper", $module); $dbTableResource = DbTableProvider::createResource($this->_loadedProfile, $tableName, strtolower($tableName), $module); } catch (Exception $e) { $response->setException($e); return; } //model spec $modelPath = str_replace(basename($modelResource->getContext()->getPath()), '', $modelResource->getContext()->getPath()); $basePath = realpath($modelPath . '/../..'); $modelSpecPath = realpath($basePath . '/spec/models') . '/' . $name . 'Spec.php'; $specContent = $this->_getSpecContent($name, $fields); // migrations if (!is_dir($basePath . "/db")) { mkdir($basePath . "/db"); } if (!is_dir($basePath . "/db/migrate")) { mkdir($basePath . "/db/migrate"); } $files = glob($basePath . "/db/migrate/*.php"); natsort($files); $nextVersion = empty($files) ? 1 : 1 + (int) substr(basename(array_pop($files)), 0, 3); $migrationClass = "Create{$tableName}Table"; $fileName = sprintf("%1\$03d", $nextVersion, $migrationClass) . "-{$migrationClass}"; $migrationPath = $basePath . "/db/migrate/" . $fileName . ".php"; $migrationContent = $this->_getMigrationContent($migrationClass, $dbTableName, $fields); // do the creation if ($request->isPretend()) { $response->appendContent('Would create a model at ' . $modelResource->getContext()->getPath()); $response->appendContent('Would create a db table at ' . $dbTableResource->getContext()->getPath()); $response->appendContent('Would create a mapper at ' . $mapperResource->getContext()->getPath()); $response->appendContent('Would create a spec at ' . $modelSpecPath); $response->appendContent('Would create migration scripts at ' . $migrationPath); } else { $response->appendContent('Creating a model at ' . $modelResource->getContext()->getPath()); $modelResource->create(); $response->appendContent('Creating a db table at ' . $dbTableResource->getContext()->getPath()); $dbTableResource->create(); $response->appendContent('Creating a mapper at ' . $mapperResource->getContext()->getPath()); $mapperContent = $this->_getMapperContent($name); file_put_contents($mapperResource->getContext()->getPath(), $mapperContent); $response->appendContent('Creating a spec at ' . $modelSpecPath); file_put_contents($modelSpecPath, $specContent); $response->appendContent('Creating migration scripts at ' . $migrationPath); file_put_contents($migrationPath, $migrationContent); $this->_storeProfile(); } }