Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
fixed Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ajthinking committed May 27, 2018
1 parent 67f7ca8 commit 995987e
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 171 deletions.
10 changes: 8 additions & 2 deletions app/Stimpack/Manipulators/Support/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

class Attribute
{
public function __construct($name, $migrationStatements, $isHidden, $isFillable)
public function __construct($name, $migrationStatements, $seederStatements, $isHidden, $isFillable)
{
$this->name = $name;
$this->migrationStatements = $migrationStatements;
$this->seederStatements = $seederStatements;
$this->isHidden = $isHidden;
$this->isFillable = $isFillable;
}
Expand All @@ -21,7 +22,12 @@ public function migrationStatements()
{
return $this->migrationStatements;
}


public function seederStatements()
{
return $this->seederStatements;
}

public function isHidden()
{
return $this->isHidden;
Expand Down
79 changes: 4 additions & 75 deletions app/Stimpack/Manipulators/Support/AttributeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Stimpack\Manipulators\Support;

use App\Stimpack\Manipulators\Support\EntityFactory;
use App\Stimpack\Manipulators\Support\MigrationStatements;
use App\Stimpack\Manipulators\Support\SeederStatements;

class AttributeFactory
{
Expand Down Expand Up @@ -36,7 +38,8 @@ public function forEntity($entity)
$attributes = $pseudoAttributes->map(function($name) {
return new Attribute(
$name,
collect([$this->makeMigrationStatements($name)]),
collect([MigrationStatements::make($name)]),
collect([SeederStatements::make($name)]),
$this->isHidden($name),
$this->isFillable($name)
);
Expand All @@ -56,86 +59,12 @@ public function manyToManyRegExp($entity)
return "/^(" . $modelOptions . ")_(" . $modelOptions . ")$/";
}

public function makeMigrationStatements($name)
{
return collect([
$this->overridden($name),
$this->reserved($name),
$this->ruled($name),
$this->default($name)
])->first(function($nameCandidate) {
return $nameCandidate;
});
}

// here we can place certain names to override according to user history/preferences
private function overridden($name)
{
// not implemented
}

// reserved are fields that are used by the laravel frameworks defaults
private function reserved($name)
{
return collect([
"id"=> '$table->increments(' . "'id');",
"timestamps"=> '$table->timestamps();',
"rememberToken"=> '$table->rememberToken();',
"timestamps()"=> '$table->timestamps();',
"created_at"=> '$table->timestamp(' . "'created_at')->nullable();",
"email"=> '$table->string(' . "'email')->unique();"
])->get($name);
}

private function ruled($name)
{
$transformer = $this->rules()->first(function ($transformer, $rule) use($name) {
return preg_match($rule, $name);
});

if($transformer)
{
return $transformer($name);
}

}

private function rules()
{
return collect([
// if attribute ends on _id its assumed to be a One to Many (one to one not supported atm)
// improvement would be to also check if <MODEL>_id actually is a model (exluding current model)
'/_id$/' => function($name) {
$tableName = snake_case(str_plural(
str_replace_last("_id", "", $name)
));
return collect([
'$table->integer(' . "'" . $name . "')->unsigned();",
'$table->foreign(' . "'" . $name . "')->references('id')->on('" . $tableName . "')->onDelete('cascade');"
]);

},
// Time columns
'/(time|date|_at)$/' => function($name) {
return '$table->timestamp(' . "'" . $name . "');";
},
// Boolean
'/^(has_|is_|got_)/' => function($name) {
return '$table->boolean(' . "'" . $name . "')->default(false);";
}
]);
}

private function modelExists($title)
{
return true; // fix later
}

private function default($name)
{
return '$table->string(' . "'" . $name . "');";
}

private function isFillable($name) {
return !collect([
"created_at",
Expand Down
32 changes: 22 additions & 10 deletions app/Stimpack/Manipulators/Support/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use App\Stimpack\Contexts\File;
use App\Stimpack\Manipulators\Support\Attribute;
use Illuminate\Support\Str;
use DateTime;
use DateInterval;

class Entity
{
Expand All @@ -12,15 +14,17 @@ public function __construct($segment)
$this->segment = $segment;
$this->title = $segment->title();
//$this->pseudoAttributes = $this->segment->pseudoAttributes();/*
$this->pseudoAttributes = $this->segment->pseudoAttributes()->concat(
collect([
'id',
'created_at',
'updated_at'
])->filter(function($name) {
return !$this->segment->pseudoAttributes()->contains($name);
})
);
$this->pseudoAttributes = $this->addIfNotExists(
collect(['id','timestamps']),
$this->segment->pseudoAttributes()
);
}

private function addIfNotExists($names, $pseudoAttributes)
{
return $names->filter(function($name) use($pseudoAttributes) {
return !$pseudoAttributes->contains($name);
})->concat($pseudoAttributes);
}

public static function make($segment)
Expand Down Expand Up @@ -55,6 +59,13 @@ public function migrationColumns()
})->flatten()->implode("\n");
}

public function seederColumns()
{
return $this->attributes->map(function($attribute) {
return $attribute->seederStatements();
})->flatten()->implode("\n");
}

// SHOULD BE MOVED TO FILE!
public function replaceOrDestroyLine($marker, $replacements, $content)
{
Expand Down Expand Up @@ -143,7 +154,8 @@ protected function migrationFilePath()

protected function migrationFileName()
{
return date('Y_m_d_His') . "_create_" . $this->pluralSnakeCaseTitle() . "_table.php";
return (new DateTime())->add(new DateInterval('PT' . $this->segment->segmentIndex . 'S'))->format('Y_m_d_His')
. "_create_" . $this->pluralSnakeCaseTitle() . "_table.php";
}

protected function migrationClassName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Stimpack\Manipulators\Support\Entity\Entity;
use App\Stimpack\FilePreview;
use App\Stimpack\Contexts\File;
use DateTime;
use DateInterval;

class ManyToManyRelationshipEntity extends Entity
{
Expand All @@ -24,10 +26,16 @@ public function migrationFilePath()
{
return path(
$this->directives->targetProjectPath,
"database/migrations/" . date('Y_m_d_His') . "_create_" . $this->title() . "_table.php"
"database/migrations/" . $this->migrationFileName()
);
}

protected function migrationFileName()
{
return (new DateTime())->add(new DateInterval('PT' . $this->segment->segmentIndex . 'S'))->format('Y_m_d_His')
. "_create_" . $this->title() . "_table.php";
}

public function migrationFileContent()
{
$content = str_pair_replace(
Expand Down
82 changes: 55 additions & 27 deletions app/Stimpack/Manipulators/Support/Entity/ModelEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,40 @@

class ModelEntity extends Entity
{
public function install() {
return collect([
$this->makeModelFile(),
$this->makeMigrationFile(),
//$this->makeControllerFile(),
//$this->injectRoutes()
]);
}

public function preview() {
return collect([
new FilePreview($this->modelFilePath(), $this->modelFileContent()),
new FilePreview($this->migrationFilePath(), $this->migrationFileContent())
new FilePreview($this->migrationFilePath(), $this->migrationFileContent()),
new FilePreview($this->controllerFilePath(), $this->controllerFileContent()),
// OUCH! this feel apart, did not make it on time.
//new FilePreview($this->seederFilePath(), $this->seederFileContent())
]);
}

private function makeModelFile()
private function controllerFilePath()
{
$file = File::save(
$this->modelFilePath(),
$this->modelFileContent()
);

return "http://stimpack-dev.test/preview/file/at/" . $this->modelFilePath();
return path($this->directives->targetProjectPath, "app/Http/Controllers/" . $this->title . "Controller.php");
}

private function modelFilePath()
private function controllerFileContent()
{
return path($this->directives->targetProjectPath, "app/Models/" . $this->title . ".php");
$content = str_pair_replace(
collect([
"MODEL" => $this->title(),
"CONTROLLER" => $this->title() . "Controller",
"CLASS" => $this->title(),
"INSTANCE" => camel_case($this->title())
]),
$this->directives->stubs->controller
);

return $content;
}

private function modelFilePath()
{
return path($this->directives->targetProjectPath, "app/" . $this->title . ".php");
}

private function modelFileContent()
{
Expand All @@ -61,26 +65,50 @@ private function methodsString()
{
$methods = $this->allRelationships()->filter(function($relationship) {
return $relationship->concerns($this);
})->map(function($relationship) {
})->map(function($relationship) {
return $relationship->renderMethod($this);
});

if($methods->isNotEmpty()) {
return "\n" . $methods->implode("\n\n");
}

return "";
}

private function seederFilePath()
{
return path($this->directives->targetProjectPath, "database/seeds/" . $this->seederFileName());
}

private function makeMigrationFile()
private function seederFileContent()
{
$file = File::save(
$this->migrationFilePath(),
$this->migrationFileContent()
);
$content = str_pair_replace(
collect([
"CLASS_NAME" => $this->seederClassName(),
"TABLE" => $this->pluralSnakeCaseTitle()
]),
File::init()->get(base_path("app/Stimpack/Manipulators/Support/stubs/seeder.stub"))
);

return "http://stimpack-dev.test/preview/file/at/" . $this->migrationFilePath();

$content = str_block_replace(
"SEEDER_STATEMENTS",
$this->seederColumns(),
$content
);

return $content;
}

private function seederFileName()
{
return $this->title() . "Seeder.php";
}

private function seederClassName()
{
return $this->title() . "Seeder";
}

private function renderFillableAttributes()
{
Expand Down
6 changes: 4 additions & 2 deletions app/Stimpack/Manipulators/Support/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public function pureTableEntities()

public function oneToManyRelationships()
{
return $this->allEntities->map(function($entity) {
return $this->allEntities->filter(function($entity) {
return !$this->isManyToManyRelationship($entity->segment);
})->map(function($entity) {
return $entity->attributes->filter(function($attribute) {
return preg_match('/(.*)_id$/', $attribute->name());
})->map(function($attribute) use($entity) {
Expand All @@ -112,7 +114,7 @@ public function manyToManyRelationships()
preg_match($this->manyToManyRegExp(),$entity->title(), $matches);
return collect([
new Relationship($matches[1], "belongsToMany", $matches[2]),
new Relationship($matches[2], "belongsToMany", $matches[1])
new Relationship($matches[2], "belongsToMany", $matches[1])
]);
})->flatten();
}
Expand Down
Loading

0 comments on commit 995987e

Please sign in to comment.