Skip to content

Commit

Permalink
Refactor master generator command
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Mar 3, 2014
1 parent 1899b06 commit d82aae1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
7 changes: 0 additions & 7 deletions spec/Way/Generators/GeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ function it_is_initializable()
$this->shouldHaveType('Way\Generators\Generator');
}

function it_creates_a_file_with_given_text($file)
{
$file->make('foo.txt', 'bar')->shouldBeCalled();

$this->generate('foo.txt', 'bar');
}

function it_compiles_a_template(Filesystem $file, TemplateCompiler $compiler)
{
$template = 'class $NAME$ {}';
Expand Down
13 changes: 6 additions & 7 deletions src/Way/Generators/Commands/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,22 @@ protected abstract function getFileGenerationPath();
*/
public function fire()
{
$templateData = $this->getTemplateData();
$filePathToGenerate = $this->getFileGenerationPath();

try
{
// This section is what actually compiles the template, and generates the file
$this->generator->setTemplatePath($this->option('templatePath'));
$compiledTemplate = $this->generator->compile($templateData, new TemplateCompiler);
$this->generator->generate($filePathToGenerate, $compiledTemplate);
$this->generator->make(
$this->option('templatePath'),
$this->getTemplateData(),
$filePathToGenerate
);

// Alert user of file creation
$this->info("Created: {$filePathToGenerate}");
}

catch (FileAlreadyExists $e)
{
return $this->error("The file, {$filePathToGenerate}, already exists! I don't want to overwrite it.");
$this->error("The file, {$filePathToGenerate}, already exists! I don't want to overwrite it.");
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Way/Generators/Commands/MigrationGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function fire()
$this->call('dump-autoload');
}


/**
* The path where the file will be created
*
Expand Down
35 changes: 22 additions & 13 deletions src/Way/Generators/Generator.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php namespace Way\Generators;

use Way\Generators\Filesystem\Filesystem;
use Way\Generators\Filesystem\FileAlreadyExists;
use Way\Generators\Compilers\TemplateCompiler;
use Way\Generators\UndefinedTemplate;

Expand Down Expand Up @@ -45,6 +44,28 @@ public function getTemplatePath()
return $this->templatePath;
}

/**
* Run the generator
*
* @param $templatePath
* @param $templateData
* @param $filePathToGenerate
*/
public function make($templatePath, $templateData, $filePathToGenerate)
{
// We'll begin by setting the location
// of the template for this file generation
$this->setTemplatePath($templatePath);

// Next, we need to compile the template, according
// to the data that we provide it with.
$template = $this->compile($templateData, new TemplateCompiler);

// Now that we have the compiled template,
// we can actually generate the file
$this->file->make($filePathToGenerate, $template);
}

/**
* Compile the file
*
Expand All @@ -62,16 +83,4 @@ public function compile(array $data, TemplateCompiler $compiler)
return $compiler->compile($template, $data);
}

/**
* Generate the file
*
* @param $file
* @param $content
* @throws FileAlreadyExists
*/
public function generate($file, $content)
{
$this->file->make($file, $content);
}

}
3 changes: 0 additions & 3 deletions tests/features/generators.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ Feature: Generators
| command | argument | generatedFilePath |
| model | Order | app/models/Order.php |
| seed | orders | app/database/seeds/OrdersTableSeeder.php |
| migration | create_orders_table | app/database/migrations/CreateOrdersTable.php |
| migration | delete_orders_table | app/database/migrations/DeleteOrdersTable.php |
| migration | add_title_to_orders_table | app/database/migrations/AddTitleToOrdersTable.php |

0 comments on commit d82aae1

Please sign in to comment.