/** * @param $parameters */ public function run($parameters) { $name = $this->getOption(array('name', 1)); $className = Text::camelize(isset($parameters[1]) ? $parameters[1] : $name); $fileName = Text::uncamelize($className); $schema = $this->getOption('schema'); $modelBuilder = new ModelBuilder(array('name' => $name, 'schema' => $schema, 'className' => $className, 'fileName' => $fileName, 'genSettersGetters' => $this->isReceivedOption('get-set'), 'genDocMethods' => $this->isReceivedOption('doc'), 'namespace' => $this->getOption('namespace'), 'directory' => $this->getOption('directory'), 'modelsDir' => $this->getOption('output'), 'extends' => $this->getOption('extends'), 'excludeFields' => $this->getOption('excludefields'), 'force' => $this->isReceivedOption('force'), 'mapColumn' => $this->isReceivedOption('mapcolumn'))); $modelBuilder->build(); }
/** * @return bool * @throws BuilderException */ public function build() { if ($this->options->contains('directory')) { $this->path->setRootPath($this->options->get('directory')); } $name = $this->options->get('name'); $config = $this->getConfig(); if (!isset($config->database->adapter)) { throw new BuilderException('Adapter was not found in the config. Please specify a config variable [database][adapter].'); } $adapter = 'Mysql'; if (isset($config->database->adapter)) { $adapter = ucfirst($config->database->adapter); $this->isSupportedAdapter($adapter); } $di = new FactoryDefault(); $di->set('db', function () use($adapter, $config) { if (is_object($config->database)) { $configArray = $config->database->toArray(); } else { $configArray = $config->database; } $adapterName = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; unset($configArray['adapter']); return new $adapterName($configArray); }); if (!isset($config->application->modelsDir)) { throw new BuilderException('The builder is unable to find the models directory.'); } $modelPath = $config->application->modelsDir; if (false == $this->isAbsolutePath($modelPath)) { $modelPath = $this->path->getRootPath($config->application->modelsDir); } $this->options->offsetSet('modelsDir', rtrim($modelPath, '\\/') . DIRECTORY_SEPARATOR); if (!isset($config->application->controllersDir)) { throw new BuilderException('The builder is unable to find the controllers directory.'); } $controllerPath = $config->application->controllersDir; if (false == $this->isAbsolutePath($controllerPath)) { $controllerPath = $this->path->getRootPath($config->application->controllersDir); } $this->options->offsetSet('controllersDir', rtrim($controllerPath, '\\/') . DIRECTORY_SEPARATOR); if (!isset($config->application->viewsDir)) { throw new BuilderException('The builder is unable to find the views directory.'); } $viewPath = $config->application->viewsDir; if (false == $this->isAbsolutePath($viewPath)) { $viewPath = $this->path->getRootPath($config->application->viewsDir); } $this->options->offsetSet('viewsDir', $viewPath); $this->options->offsetSet('manager', $di->getShared('modelsManager')); $this->options->offsetSet('className', Text::camelize($this->options->get('name'))); $this->options->offsetSet('fileName', Text::uncamelize($this->options->get('className'))); $modelsNamespace = ''; if ($this->options->contains('modelsNamespace') && $this->checkNamespace($this->options->get('modelsNamespace'))) { $modelsNamespace = $this->options->get('modelsNamespace'); } $modelName = Text::camelize($name); if ($modelsNamespace) { $modelClass = '\\' . trim($modelsNamespace, '\\') . '\\' . $modelName; } else { $modelClass = $modelName; } $modelPath = $this->options->get('modelsDir') . $modelName . '.php'; if (!file_exists($modelPath) || $this->options->get('force')) { $modelBuilder = new ModelBuilder(array('name' => $name, 'schema' => $this->options->get('schema'), 'className' => $this->options->get('className'), 'fileName' => $this->options->get('fileName'), 'genSettersGetters' => $this->options->get('genSettersGetters'), 'directory' => $this->options->get('directory'), 'force' => $this->options->get('force'), 'namespace' => $this->options->get('modelsNamespace'))); $modelBuilder->build(); } if (!class_exists($modelClass)) { require $modelPath; } $entity = new $modelClass(); $metaData = $di['modelsMetadata']; $attributes = $metaData->getAttributes($entity); $dataTypes = $metaData->getDataTypes($entity); $identityField = $metaData->getIdentityField($entity); $primaryKeys = $metaData->getPrimaryKeyAttributes($entity); $setParams = array(); $selectDefinition = array(); $relationField = ''; $single = $name; $this->options->offsetSet('name', strtolower(Text::camelize($single))); $this->options->offsetSet('plural', $this->_getPossiblePlural($name)); $this->options->offsetSet('singular', $this->_getPossibleSingular($name)); $this->options->offsetSet('modelClass', $modelClass); $this->options->offsetSet('entity', $entity); $this->options->offsetSet('setParams', $setParams); $this->options->offsetSet('attributes', $attributes); $this->options->offsetSet('dataTypes', $dataTypes); $this->options->offsetSet('primaryKeys', $primaryKeys); $this->options->offsetSet('identityField', $identityField); $this->options->offsetSet('relationField', $relationField); $this->options->offsetSet('selectDefinition', $selectDefinition); $this->options->offsetSet('autocompleteFields', array()); $this->options->offsetSet('belongsToDefinitions', array()); // Build Controller $this->_makeController(); if ($this->options->get('templateEngine') == 'volt') { // View layouts $this->_makeLayoutsVolt(); // View index.phtml $this->makeViewVolt('index'); // View search.phtml $this->_makeViewSearchVolt(); // View new.phtml $this->makeViewVolt('new'); // View edit.phtml $this->makeViewVolt('edit'); } else { // View layouts $this->_makeLayouts(); // View index.phtml $this->makeView('index'); // View search.phtml $this->_makeViewSearch(); // View new.phtml $this->makeView('new'); // View edit.phtml $this->makeView('edit'); } return true; }
public function build() { if ($this->options->contains('directory')) { $this->path->setRootPath($this->options->get('directory')); } $this->options->offsetSet('directory', $this->path->getRootPath()); $config = $this->getConfig(); if (!($modelsDir = $this->options->get('modelsDir'))) { if (!isset($config->application->modelsDir)) { throw new BuilderException("Builder doesn't know where is the models directory."); } $modelsDir = $config->application->modelsDir; } $modelsDir = rtrim($modelsDir, '/\\') . DIRECTORY_SEPARATOR; $modelPath = $modelsDir; if (false == $this->isAbsolutePath($modelsDir)) { $modelPath = $this->path->getRootPath($modelsDir); } $this->options->offsetSet('modelsDir', $modelPath); $forceProcess = $this->options->get('force'); $defineRelations = $this->options->get('defineRelations', false); $defineForeignKeys = $this->options->get('foreignKeys', false); $genSettersGetters = $this->options->get('genSettersGetters', false); $mapColumn = $this->options->get('mapColumn', null); $adapter = $config->database->adapter; $this->isSupportedAdapter($adapter); $adapter = 'Mysql'; if (isset($config->database->adapter)) { $adapter = $config->database->adapter; } if (is_object($config->database)) { $configArray = $config->database->toArray(); } else { $configArray = $config->database; } $adapterName = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; unset($configArray['adapter']); /** * @var $db \Phalcon\Db\Adapter\Pdo */ $db = new $adapterName($configArray); if ($this->options->contains('schema')) { $schema = $this->options->get('schema'); } elseif ($adapter == 'Postgresql') { $schema = 'public'; } else { $schema = isset($config->database->schema) ? $config->database->schema : $config->database->dbname; } $hasMany = array(); $belongsTo = array(); $foreignKeys = array(); if ($defineRelations || $defineForeignKeys) { foreach ($db->listTables($schema) as $name) { if ($defineRelations) { if (!isset($hasMany[$name])) { $hasMany[$name] = array(); } if (!isset($belongsTo[$name])) { $belongsTo[$name] = array(); } } if ($defineForeignKeys) { $foreignKeys[$name] = array(); } $camelCaseName = Utils::camelize($name); $refSchema = $adapter != 'Postgresql' ? $schema : $config->database->dbname; foreach ($db->describeReferences($name, $schema) as $reference) { $columns = $reference->getColumns(); $referencedColumns = $reference->getReferencedColumns(); $referencedModel = Utils::camelize($reference->getReferencedTable()); if ($defineRelations) { if ($reference->getReferencedSchema() == $refSchema) { if (count($columns) == 1) { $belongsTo[$name][] = array('referencedModel' => $referencedModel, 'fields' => $columns[0], 'relationFields' => $referencedColumns[0], 'options' => $defineForeignKeys ? array('foreignKey' => true) : null); $hasMany[$reference->getReferencedTable()][] = array('camelizedName' => $camelCaseName, 'fields' => $referencedColumns[0], 'relationFields' => $columns[0]); } } } } } } else { foreach ($db->listTables($schema) as $name) { if ($defineRelations) { $hasMany[$name] = array(); $belongsTo[$name] = array(); $foreignKeys[$name] = array(); } } } foreach ($db->listTables($schema) as $name) { $className = $this->options->contains('abstract') ? 'Abstract' : ''; $className .= Utils::camelize($name); if (!file_exists($modelPath . $className . '.php') || $forceProcess) { if (isset($hasMany[$name])) { $hasManyModel = $hasMany[$name]; } else { $hasManyModel = array(); } if (isset($belongsTo[$name])) { $belongsToModel = $belongsTo[$name]; } else { $belongsToModel = array(); } if (isset($foreignKeys[$name])) { $foreignKeysModel = $foreignKeys[$name]; } else { $foreignKeysModel = array(); } $modelBuilder = new Model(array('name' => $name, 'schema' => $schema, 'extends' => $this->options->get('extends'), 'namespace' => $this->options->get('namespace'), 'force' => $forceProcess, 'hasMany' => $hasManyModel, 'belongsTo' => $belongsToModel, 'foreignKeys' => $foreignKeysModel, 'genSettersGetters' => $genSettersGetters, 'directory' => $this->options->get('directory'), 'modelsDir' => $this->options->get('modelsDir'), 'mapColumn' => $mapColumn, 'abstract' => $this->options->get('abstract'))); $modelBuilder->build(); } else { if ($this->isConsole()) { print Color::info(sprintf('Skipping model "%s" because it already exist', Utils::camelize($name))); } else { $this->exist[] = $name; } } } }
/** * @return bool * @throws \Phalcon\Builder\BuilderException */ public function build() { $options = $this->_options; $path = ''; if (isset($this->_options['directory'])) { if ($this->_options['directory']) { $path = $this->_options['directory'] . '/'; } } $name = $options['name']; $config = $this->_getConfig($path); if (!isset($config->database->adapter)) { throw new BuilderException("Adapter was not found in the config. Please specify a config variable [database][adapter]"); } $adapter = ucfirst($config->database->adapter); $this->isSupportedAdapter($adapter); $di = new FactoryDefault(); $di->set('db', function () use($adapter, $config) { if (isset($config->database->adapter)) { $adapter = $config->database->adapter; } else { $adapter = 'Mysql'; } if (is_object($config->database)) { $configArray = $config->database->toArray(); } else { $configArray = $config->database; } $adapterName = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter; unset($configArray['adapter']); return new $adapterName($configArray); }); if (isset($config->application->modelsDir)) { $options['modelsDir'] = $path . $config->application->modelsDir; } else { throw new BuilderException("The builder is unable to find the views directory"); } if (isset($config->application->controllersDir)) { $options['controllersDir'] = $path . $config->application->controllersDir; } else { throw new BuilderException("The builder is unable to find the controllers directory"); } if (isset($config->application->viewsDir)) { $options['viewsDir'] = $path . $config->application->viewsDir; } else { throw new BuilderException("The builder is unable to find the views directory"); } $options['manager'] = $di->getShared('modelsManager'); $options['className'] = Text::camelize($options['name']); $options['fileName'] = Text::uncamelize($options['className']); $modelsNamespace = $options['modelsNamespace']; if (isset($modelsNamespace) && substr($modelsNamespace, -1) !== '\\') { $modelsNamespace .= "\\"; } $modelName = Text::camelize($name); $modelClass = $modelsNamespace . $modelName; $modelPath = $config->application->modelsDir . '/' . $modelName . '.php'; if (!file_exists($modelPath)) { $modelBuilder = new ModelBuilder(array('name' => $name, 'schema' => $options['schema'], 'className' => $options['className'], 'fileName' => $options['fileName'], 'genSettersGetters' => $options['genSettersGetters'], 'directory' => $options['directory'], 'force' => $options['force'])); $modelBuilder->build(); } if (!class_exists($modelClass)) { require $modelPath; } $entity = new $modelClass(); $metaData = $di['modelsMetadata']; $attributes = $metaData->getAttributes($entity); $dataTypes = $metaData->getDataTypes($entity); $identityField = $metaData->getIdentityField($entity); $primaryKeys = $metaData->getPrimaryKeyAttributes($entity); $setParams = array(); $selectDefinition = array(); $relationField = ''; $single = $name; $options['name'] = strtolower(Text::camelize($single)); $options['plural'] = $this->_getPossiblePlural($name); $options['singular'] = $this->_getPossibleSingular($name); $options['entity'] = $entity; $options['setParams'] = $setParams; $options['attributes'] = $attributes; $options['dataTypes'] = $dataTypes; $options['primaryKeys'] = $primaryKeys; $options['identityField'] = $identityField; $options['relationField'] = $relationField; $options['selectDefinition'] = $selectDefinition; $options['autocompleteFields'] = array(); $options['belongsToDefinitions'] = array(); //Build Controller $this->_makeController($path, $options); if (isset($options['templateEngine']) && $options['templateEngine'] == 'volt') { //View layouts $this->_makeLayoutsVolt($path, $options); //View index.phtml $this->_makeViewIndexVolt($path, $options); //View search.phtml $this->_makeViewSearchVolt($path, $options); //View new.phtml $this->_makeViewNewVolt($path, $options); //View edit.phtml $this->_makeViewEditVolt($path, $options); } else { //View layouts $this->_makeLayouts($path, $options); //View index.phtml $this->_makeViewIndex($path, $options); //View search.phtml $this->_makeViewSearch($path, $options); //View new.phtml $this->_makeViewNew($path, $options); //View edit.phtml $this->_makeViewEdit($path, $options); } return true; }