/** * Migration to drop a table * * @param $timestamp */ public function dropTable(&$timestamp) { $migrationName = "edit_" . $this->model->getTableName() . "_table"; $migrationFile = $this->getMigrationFileName($migrationName, $timestamp); $functionContents = "\t\tSchema::dropIfExists('" . $this->model->getTableName() . "');\n"; $this->currentFileContents = $this->fileCreator->createFunction("up", $functionContents); $functionContents = $this->migrationUp(); $this->currentFileContents .= $this->fileCreator->createFunction("down", $functionContents); $classNameArr = explode("_", $migrationName); $className = ""; foreach ($classNameArr as $class) { $className .= ucfirst($class); } $this->fileCreator->createMigrationClass($migrationFile, $this->currentFileContents, $className); }
/** * Generate the seeds file */ private function createSeeds() { $faker = Factory::create(); $databaseSeeder = $this->configSettings['pathTo']['seeds'] . 'DatabaseSeeder.php'; $databaseSeederContents = \File::get($databaseSeeder); if (preg_match("/faker/", $databaseSeederContents) !== 1) { $contentBefore = substr($databaseSeederContents, 0, strpos($databaseSeederContents, "{")); $contentAfter = substr($databaseSeederContents, strpos($databaseSeederContents, "{") + 1); $databaseSeederContents = $contentBefore; $databaseSeederContents .= "{\n\tprotected \$faker;\n\n"; $functionContents = "\t\tif(empty(\$this->faker)) {\n"; $functionContents .= "\t\t\t\$this->faker = Faker\\Factory::create();\n\t\t}\n\n"; $functionContents .= "\t\treturn \$this->faker;\n"; $databaseSeederContents .= $this->fileCreator->createFunction("getFaker", $functionContents); $databaseSeederContents .= $contentAfter; \File::put($databaseSeeder, $databaseSeederContents); } $functionContent = "\t\t\$faker = \$this->getFaker();\n\n"; $functionContent .= "\t\tfor(\$i = 1; \$i <= 10; \$i++) {\n"; $functionContent .= "\t\t\t\$" . $this->model->lower() . " = array(\n"; foreach ($this->model->getProperties() as $property => $type) { if ($property == "password") { $functionContent .= "\t\t\t\t'{$property}' => \\Hash::make('password'),\n"; } else { $fakerProperty = ""; try { $fakerProperty2 = $faker->getFormatter($property); $fakerProperty = $property; } catch (\InvalidArgumentException $e) { } if (empty($fakerProperty)) { try { $fakerProperty2 = $faker->getFormatter($type); $fakerProperty = $type; } catch (\InvalidArgumentException $e) { } } if (empty($fakerProperty)) { $fakerType = ""; switch ($type) { case "integer": case "biginteger": case "smallinteger": case "tinyinteger": $fakerType = "randomDigitNotNull"; break; case "string": $fakerType = "word"; break; case "float": case "double": $fakerType = "randomFloat"; break; case "mediumtext": case "longtext": case "binary": $fakerType = "text"; break; } $fakerType = $fakerType ? "\$faker->" . $fakerType : "0"; } else { $fakerType = "\$faker->" . $fakerProperty; } $functionContent .= "\t\t\t\t'{$property}' => {$fakerType},\n"; } } foreach ($this->migration->getForeignKeys() as $key) { $functionContent .= "\t\t\t\t'{$key}' => \$i,\n"; } $functionContent .= "\t\t\t);\n"; $namespace = $this->namespace ? "\\" . $this->namespace . "\\" : ""; $functionContent .= "\t\t\t" . $namespace . $this->model->upper() . "::create(\$" . $this->model->lower() . ");\n"; $functionContent .= "\t\t}\n"; $fileContents = $this->fileCreator->createFunction("run", $functionContent); $fileName = $this->configSettings['pathTo']['seeds'] . $this->model->upperPlural() . "TableSeeder.php"; $this->fileCreator->createClass($fileName, $fileContents, array('name' => 'DatabaseSeeder'), array(), array(), "class", false, false); $tableSeederClassName = $this->model->upperPlural() . 'TableSeeder'; $content = \File::get($databaseSeeder); if (preg_match("/{$tableSeederClassName}/", $content) !== 1) { $content = preg_replace("/(run\\(\\).+?)}/us", "\$1\t\$this->call('{$tableSeederClassName}');\n\t}", $content); \File::put($databaseSeeder, $content); } }