public function __construct(Table $table, ActivityLog $activityLog = null, Inflector $inflector = null) { $this->table = $table; $this->inflector = $inflector ?: Pimple::getResource('inflector'); $tableName = $table->getTableName(); $inflectedName = $this->inflector->singularize($this->inflector->hyphenize($tableName)); if (!$tableName) { $className = get_class($table); throw new Exception("Cannot create activity log handle for {$className} because no table name is set."); } $this->setActivityLog($activityLog ?: Pimple::getResource('activity-log'))->setName($inflectedName)->setModel($table)->addAlias($tableName); parent::__construct(); }
/** * Write metadata files for all the listed tables. * * @param string $path * @param DbAdapter $db * @param array $tables * @return void */ protected function writeMetadataFiles($path, DbAdapter $db, array $tables) { $inflector = new Inflector(); foreach ($tables as $table) { $columns = $db->describeTable($table); $references = $db->listForeignKeyReferences($table); $uniqueConstraints = $db->listUniqueConstraints($table); $title = $inflector->titleize($table); $replacements = array('{{singular}}' => $inflector->singularize($title), '{{plural}}' => $inflector->pluralize($title), '{{columns}}' => var_export($columns, true), '{{references}}' => var_export($references, true), '{{uniqueConstraints}}' => var_export($uniqueConstraints, true)); file_put_contents("{$path}/{$table}.php", str_replace(array_keys($replacements), $replacements, file_get_contents(__DIR__ . '/db-metadata/template.tpl'))); } }
/** * Generate the model class and dbdeploy delta and then output the path * to each so that they can easily be found for editing. * * @return void */ public function execute() { $inflector = new Inflector(); if (null === $this->modelClass) { $this->modelClass = $inflector->classify($this->name); } $modelFile = $this->paths->getModels() . '/' . $this->modelClass . '.php'; $dbdeployFile = $this->paths->getDb() . '/' . $this->getDbRevision() . '-add-' . $this->name . '.sql'; if ($this->modelAlreadyExists($modelFile)) { return $this->abort("There is a already a model file named \"{$this->modelClass}.php\""); } if ($this->dbdeployFileAlreadyExists($dbdeployFile)) { return $this->abort("There is already a dbdeploy file at \"{$dbdeployFile}\""); } $templateReplacements = array('{{modelClass}}' => $this->modelClass, '{{tableName}}' => $this->name); $this->writeFile($modelFile, str_replace(array_keys($templateReplacements), $templateReplacements, file_get_contents(__DIR__ . '/gen-templates/db-table/ModelClass.tpl'))); $templateReplacements = array('{{tableName}}' => $this->name, '{{primaryKey}}' => $inflector->singularize($this->name) . '_id'); $this->writeFile($dbdeployFile, str_replace(array_keys($templateReplacements), $templateReplacements, file_get_contents(__DIR__ . '/gen-templates/db-table/dbdeploy-delta.sql'))); $this->renderSuccessMessage($dbdeployFile, $modelFile); }
/** * Generate the model class and dbdeploy delta and then output the path * to each so that they can easily be found for editing. * * @return void */ public function execute() { $this->db = $this->runner->connectDb(); $inflector = new Inflector(); $dbdeployFile = $this->paths->getDb() . '/' . $this->getDbRevision() . '-add-' . $this->tableName . '-eav.sql'; if ($this->dbdeployFileAlreadyExists($dbdeployFile)) { return $this->abort("There is already a dbdeploy file at \"{$dbdeployFile}\""); } $templateReplacements = array('{{tableName}}' => $this->tableName, '{{primaryKey}}' => $inflector->singularize($this->tableName) . '_id', '{{primaryKeyColumns}}' => $this->generatePkeyColumnContent(), '{{multiColumnPrimaryKeyIndexes}}' => $this->generateMultiColumnPkeyIndexContent(), '{{primaryKeyForeignKeys}}' => $this->generatePkeyForeignKeyContent(), '{{primaryKeyColumnList}}' => $this->generatePkeyColumnListContent()); $this->writeFile($dbdeployFile, str_replace(array_keys($templateReplacements), $templateReplacements, file_get_contents(__DIR__ . '/gen-templates/eav/eav-tables.sql'))); $this->renderSuccessMessage($dbdeployFile, $modelFile); }
public function testSimpleSingularize() { $this->assertEquals('Fruit', $this->inflector->singularize('Fruits')); }