Skip to content

Commit

Permalink
DI, Command: removed realpath
Browse files Browse the repository at this point in the history
Code: reformated tabs to 4 spaces etc
  • Loading branch information
castamir committed Sep 1, 2015
1 parent fd5ae4f commit 3b42dd6
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 312 deletions.
168 changes: 90 additions & 78 deletions src/Joseki/FileTemplate/Console/Command/ControlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,82 +12,94 @@

class ControlCommand extends Command
{
/** @var Schema[] */
private $schemaList;

private $selectedSchema;

private $selectedDir;

/** @var */
private $rootDir;

/**
* ControlCommand constructor.
* @param array $schemaList
*/
public function __construct(array $schemaList, $rootDir)
{
parent::__construct();
$this->schemaList = $schemaList;
$this->rootDir = rtrim($rootDir, '/\\ ');
}

/**
* @return \Joseki\FileTemplate\Schema[]
*/
public function getSchemaList()
{
return $this->schemaList;
}

protected function configure()
{
$this->setName('joseki:fileTemplate');
$this->setDescription('FileTemplate generator');

$this->addArgument(
'name',
InputArgument::REQUIRED,
'Which command (set of templates) are you going to create?'
);

$this->addArgument(
'dir',
InputArgument::REQUIRED,
'Directory for new files?'
);
}

protected function interact(InputInterface $input, OutputInterface $output)
{

$this->selectedSchema = $input->getArgument('name');
$this->selectedDir = trim($input->getArgument('dir'), '/\\');

$helper = $this->getHelper('question');

if (!array_key_exists($this->selectedSchema, $this->schemaList)) {
throw new InvalidArgumentException("FileTemplate name '{$this->selectedSchema}' not found.");
}

$schema = $this->schemaList[$this->selectedSchema];
foreach ($schema->getUndefinedVariables() as $var) {
$question = new Question("Please enter value for variable $var:");
$answer = $helper->ask($input, $output, $question);
$schema->setVariable($var, $answer);
}

}

protected function execute(InputInterface $input, OutputInterface $output)
{
$schema = $this->schemaList[$this->selectedSchema];
foreach ($schema->getFiles() as $var => $templatePath) {
$fileName = $this->rootDir . '/' . $this->selectedDir . '/' . $schema->getVariable($var);
$content = $schema->translate(file_get_contents($templatePath));
file_put_contents($fileName, $content);
$output->writeln('Created file: ' . realpath($fileName));
}
}
/** @var Schema[] */
private $schemaList;

private $selectedSchema;

private $selectedDir;

/** @var */
private $rootDir;



/**
* ControlCommand constructor.
* @param array $schemaList
*/
public function __construct(array $schemaList, $rootDir)
{
parent::__construct();
$this->schemaList = $schemaList;
$this->rootDir = rtrim($rootDir, '/\\ ');
}



/**
* @return \Joseki\FileTemplate\Schema[]
*/
public function getSchemaList()
{
return $this->schemaList;
}



protected function configure()
{
$this->setName('joseki:fileTemplate');
$this->setDescription('FileTemplate generator');

$this->addArgument(
'name',
InputArgument::REQUIRED,
'Which command (set of templates) are you going to create?'
);

$this->addArgument(
'dir',
InputArgument::REQUIRED,
'Directory for new files?'
);
}



protected function interact(InputInterface $input, OutputInterface $output)
{

$this->selectedSchema = $input->getArgument('name');
$this->selectedDir = trim($input->getArgument('dir'), '/\\');

$helper = $this->getHelper('question');

if (!array_key_exists($this->selectedSchema, $this->schemaList)) {
throw new InvalidArgumentException("FileTemplate name '{$this->selectedSchema}' not found.");
}

$schema = $this->schemaList[$this->selectedSchema];
foreach ($schema->getUndefinedVariables() as $var) {
$question = new Question("Please enter value for variable $var:");
$answer = $helper->ask($input, $output, $question);
$schema->setVariable($var, $answer);
}

}



protected function execute(InputInterface $input, OutputInterface $output)
{
$schema = $this->schemaList[$this->selectedSchema];
$dir = $this->rootDir . '/' . $this->selectedDir;
@mkdir($dir);
foreach ($schema->getFiles() as $var => $templatePath) {
$fileName = $dir . '/' . $schema->getVariable($var);
$content = $schema->translate(file_get_contents($templatePath));
file_put_contents($fileName, $content);
$output->writeln('Created file: ' . $fileName);
}
}
}
134 changes: 68 additions & 66 deletions src/Joseki/FileTemplate/DI/FileTemplateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,73 @@
class FileTemplateExtension extends CompilerExtension
{

const TAG_JOSEKI_COMMAND = 'joseki.console.command';
const TAG_KDYBY_COMMAND = 'kdyby.console.command';

public $defaults = [
'commands' => [],// prikaz pro generovani - obsahuje pole cest k sablonam
'rootDir' => null,
];

public function loadConfiguration()
{
$container = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);

Validators::assert($config['commands'], 'array');

$schemaList = [];
foreach ($config['commands'] as $name => $details) {
$schemaList[$name] = $this->createSchema($name, $details);
}

if (!$config['rootDir']) {
$config['rootDir'] = $container->expand('%appDir%');
}

$container->addDefinition($this->prefix('controlCommand'))
->setClass('Joseki\FileTemplate\Console\Command\ControlCommand', [$schemaList, $config['rootDir']])
->addTag('joseki.console.command')
->addTag('kdyby.console.command');

}

private function createSchema($name, $command)
{
$variables = [];
$defaults = [];

Validators::assert($command['variables'], 'array');
Validators::assert($command['templates'], 'array');
if (isset($command['defaults'])) {
Validators::assert($command['defaults'], 'array');
$defaults = $command['defaults'];
}

foreach ($command['variables'] as $var) {
$variables["$$var$"] = '';
}

foreach ($defaults as $varName => $value) {
// $variables["$$varName$"] = str_replace(array_keys($variables), $variables, $value);
$variables["$$varName$"] = $value;
}

foreach ($command['templates'] as $templateVar => $template) {
$path = realpath($template);
if (!file_exists($path)) {
throw new InvalidArgumentException("Template file '$path' used in FileTemplate command '$name' not found.");
}
if (!array_key_exists("$$templateVar$", $variables)) {
throw new InvalidArgumentException(
"Missing variable '$templateVar' in FileTemplate command '$name'. Templates must be in 'FILE_NAME_VAR: path/to/template' notation."
);
}
}

return new Schema($variables, $command['templates']);
}
const TAG_JOSEKI_COMMAND = 'joseki.console.command';
const TAG_KDYBY_COMMAND = 'kdyby.console.command';

public $defaults = [
'commands' => [],// prikaz pro generovani - obsahuje pole cest k sablonam
'rootDir' => null,
];



public function loadConfiguration()
{
$container = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);

Validators::assert($config['commands'], 'array');

$schemaList = [];
foreach ($config['commands'] as $name => $details) {
$schemaList[$name] = $this->createSchema($name, $details);
}

if (!$config['rootDir']) {
$config['rootDir'] = $container->expand('%appDir%');
}

$container->addDefinition($this->prefix('controlCommand'))
->setClass('Joseki\FileTemplate\Console\Command\ControlCommand', [$schemaList, $config['rootDir']])
->addTag('joseki.console.command')
->addTag('kdyby.console.command');

}



private function createSchema($name, $command)
{
$variables = [];
$defaults = [];

Validators::assert($command['variables'], 'array');
Validators::assert($command['templates'], 'array');
if (isset($command['defaults'])) {
Validators::assert($command['defaults'], 'array');
$defaults = $command['defaults'];
}

foreach ($command['variables'] as $var) {
$variables["$$var$"] = '';
}

foreach ($defaults as $varName => $value) {
$variables["$$varName$"] = $value;
}

foreach ($command['templates'] as $templateVar => $template) {
if (!file_exists($template)) {
throw new InvalidArgumentException("Template file '$template' used in FileTemplate command '$name' not found.");
}
if (!array_key_exists("$$templateVar$", $variables)) {
throw new InvalidArgumentException(
"Missing variable '$templateVar' in FileTemplate command '$name'. Templates must be in 'FILE_NAME_VAR: path/to/template' notation."
);
}
}

return new Schema($variables, $command['templates']);
}

}
Loading

0 comments on commit 3b42dd6

Please sign in to comment.