Skip to content

Commit

Permalink
Merge pull request #105 from omero/changes-controller-command
Browse files Browse the repository at this point in the history
Changes controller command
  • Loading branch information
dmouse committed Jun 29, 2014
2 parents 13dafaf + 33433f2 commit 0da6e33
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 47 deletions.
16 changes: 8 additions & 8 deletions Tests/Command/GeneratorControllerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class GeneratorControllerCommandTest extends GenerateCommandTest
*/
public function testInteractive($options, $expected, $input)
{
list($module, $class_name, $test, $services, $routing_update) = $expected;
list($module, $class_name, $method_name, $route, $test, $services) = $expected;

$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($module, $class_name, $test, $services, $routing_update)
->with($module, $class_name, $method_name, $route, $test, $services)
;

$command = $this->getCommand($generator,$input);
Expand All @@ -46,27 +46,27 @@ public function getInteractiveData()
// Inline options
[],
// Expected options
['foo', 'FooController', true, $services, true],
['foo', 'FooController', 'index', 'foo/index', true, $services],
// User input options
"foo\nFooController\nyes\nyes\ntwig\n\nyes\n",
"foo\nFooController\nindex\nfoo/index\nyes\n\ntwig\nyes\n",
],
// case two
[
// Inline options
['--module'=>'foo'],
// Expected options
['foo', 'FooController', true, null, true],
['foo', 'FooController', 'index', 'foo/index', true, null],
// User input options
"FooController\nyes\nno\nyes\n",
"FooController\nindex\nfoo/index\nyes\nno\n",
],
// case three
[
// Inline options
['--module'=>'foo'],
// Expected options
['foo', 'FooController', false, null, false],
['foo', 'FooController', 'index', 'foo/index', false, null],
// User input options
"FooController\nno\nno\nno\n",
"FooController\nindex\nfoo/index\nno\nno\n",
],
];
}
Expand Down
50 changes: 34 additions & 16 deletions src/Command/GeneratorControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ protected function configure()
->setDefinition(array(
new InputOption('module','',InputOption::VALUE_REQUIRED, 'The name of the module'),
new InputOption('class-name','',InputOption::VALUE_OPTIONAL, 'Controller name'),
new InputOption('method-name','',InputOption::VALUE_OPTIONAL, 'The method name'),
new InputOption('route','',InputOption::VALUE_OPTIONAL, 'The route path'),
new InputOption('services','',InputOption::VALUE_OPTIONAL, 'Load services'),
new InputOption('routing', '', InputOption::VALUE_NONE, 'Update routing'),
new InputOption('test', '', InputOption::VALUE_NONE, 'Generate test'),
))
->setDescription('Generate controller')
Expand All @@ -49,15 +50,16 @@ protected function execute(InputInterface $input, OutputInterface $output)

$module = $input->getOption('module');
$class_name = $input->getOption('class-name');
$method_name = $input->getOption('method-name');
$route = $input->getOption('route');
$test = $input->getOption('test');
$services = $input->getOption('services');
$update_routing = $input->getOption('routing');

// @see use Drupal\AppConsole\Command\Helper\ServicesTrait::buildServices
$build_services = $this->buildServices($services);

$this->getGenerator()
->generate($module, $class_name, $test, $build_services, $update_routing);
->generate($module, $class_name, $method_name, $route, $test, $build_services);

$errors = '';
$dialog->writeGeneratorSummary($output, $errors);
Expand All @@ -82,13 +84,40 @@ protected function interact(InputInterface $input, OutputInterface $output)
// --class-name option
$class_name = $input->getOption('class-name');
if (!$class_name) {
$name = $dialog->ask(
$class_name = $dialog->ask(
$output,
$dialog->getQuestion('Enter the controller name', 'DefaultController'),
'DefaultController'
);
}
$input->setOption('class-name', $name);
$input->setOption('class-name', $class_name);

// --method-name option & --route option
if($class_name != 'DefaultController'){
$method_name = $input->getOption('method-name');
if (!$method_name) {
$method_name = $dialog->ask(
$output,
$dialog->getQuestion('Enter the method name', 'index'),
'index'
);
}

$route = $input->getOption('route');
if (!$route) {
$route = $dialog->ask(
$output,
$dialog->getQuestion('Enter the route path', $method_name.'/index'),
$method_name.'/index'
);
}
}
else{
$method_name = 'hello';
$route = '/hello/{name}';
}
$input->setOption('method-name', $method_name);
$input->setOption('route', $route);

// --test option
$test = $input->getOption('test');
Expand All @@ -105,17 +134,6 @@ protected function interact(InputInterface $input, OutputInterface $output)
// @see use Drupal\AppConsole\Command\Helper\ServicesTrait::servicesQuestion
$services_collection = $this->servicesQuestion($input, $output, $dialog);
$input->setOption('services', $services_collection);

// --routing option
$routing = $input->getOption('routing');
if (!$routing && $dialog->askConfirmation(
$output,
$dialog->getQuestion('Update routing file?', 'yes', '?'),
true
)) {
$routing = true;
}
$input->setOption('routing', $routing);
}

/**
Expand Down
21 changes: 11 additions & 10 deletions src/Generator/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
class ControllerGenerator extends Generator
{

public function generate($module, $class_name, $test, $services, $update_routing)
public function generate($module, $class_name, $method_name, $route, $test, $services)
{
$path = DRUPAL_ROOT.'/'.drupal_get_path('module', $module);

$path_controller = $path.'/src/Controller';

$parameters = array(
'name' => $class_name,
'class_name' => $class_name,
'services' => $services,
'module' => $module
'module' => $module,
'method_name' => $method_name,
'route' => $route,
);

$this->renderFile(
Expand All @@ -27,13 +29,12 @@ public function generate($module, $class_name, $test, $services, $update_routing
$parameters
);

if ($update_routing) {
$this->renderFile('module/controller-routing.yml.twig',
DRUPAL_ROOT.'/modules/'.$module.'/'.$module.'.routing.yml',
$parameters,
FILE_APPEND
);
}
$this->renderFile(
'module/controller-routing.yml.twig',
$path.'/'.$module.'.routing.yml',
$parameters,
FILE_APPEND
);

if ($test) {
$this->renderFile(
Expand Down
17 changes: 17 additions & 0 deletions src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected function render($template, $parameters)

$twig->addFunction($this->getServiceAsParamater());
$twig->addFunction($this->getServiceAsParamaterKeys());
$twig->addFunction($this->getArgumentsFromRoute());

return $twig->render($template, $parameters);
}
Expand Down Expand Up @@ -87,4 +88,20 @@ public function getServiceAsParamaterKeys()
return $servicesAsParametersKeys;
}

public function getArgumentsFromRoute()
{
$argumentsFromRoute = new \Twig_SimpleFunction('argumentsFromRoute', function ($route){
$parameters = [];
$parameters = array_filter(explode("/", $route), function($value){
return (strpos($value, "}") > 0) ? : false;
});
$parameters = array_map(function ($value){
return "$".substr($value, 1, -1);
}, $parameters);

return $parameters;
});

return $argumentsFromRoute;
}
}
9 changes: 5 additions & 4 deletions src/Resources/skeleton/module/controller-routing.yml.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% if name is defined %}
{{ module }}.hello:
path: '/{{module}}/hello/{name}'
{% if class_name is defined %}
{{ module }}.{{ method_name }}:
path: '{{ route }}'
defaults:
_content: '\Drupal\{{ module }}\Controller\{{ name }}::hello'
_content: '\Drupal\{{ module }}\Controller\{{ class_name }}::{{ method_name }}'
_title: '{{module}} Title'
requirements:
_permission: 'access content'
{% endif %}

14 changes: 9 additions & 5 deletions src/Resources/skeleton/module/module.controller.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
{% include 'skeleton/module/shared/services-use-operator.php.twig' %}
{% endif %}
class {{ name }} extends ControllerBase {% if services is defined and services is not empty %}implements ContainerInjectionInterface {% endif %}{{"\n"}}{
class {{ class_name }} extends ControllerBase {% if services is defined and services is not empty %}implements ContainerInjectionInterface {% endif %}{{"\n"}}{
{% if services is defined and services is not empty %}
{% include 'skeleton/module/shared/services-class-properties-declaration.php.twig' %}
Expand All @@ -26,11 +26,15 @@ class {{ name }} extends ControllerBase {% if services is defined and services i
{% endif %}
/**
* hello
* @param string $name
* {{method_name}}
* @return string
*/
public function hello($name) {
return "Hello " . $name . "!";
public function {{method_name}}({{ argumentsFromRoute(route)|join(', ') }})
{
{% if class_name == "DefaultController" %}
return "Hello ".$name." !";
{% else %}
return "Implements {{method_name}}";
{% endif %}
}
}
8 changes: 4 additions & 4 deletions src/Resources/skeleton/module/module.test.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use Drupal\simpletest\WebTestBase;
/**
* Provides automated tests for the {{module}} module.
*/
class {{name}}Test extends WebTestBase {
class {{class_name}}Test extends WebTestBase {
public static function getInfo() {
return array(
'name' => "{{module}} {{name}}'s controller functionality",
'description' => 'Test Unit for module {{module}} and controller {{name}}.',
'name' => "{{module}} {{class_name}}'s controller functionality",
'description' => 'Test Unit for module {{module}} and controller {{class_name}}.',
'group' => 'Other',
);
}
Expand All @@ -29,7 +29,7 @@ class {{name}}Test extends WebTestBase {
/**
* Tests {{module}} functionality.
*/
function test{{name}}() {
function test{{class_name}}() {
//Check that the basic functions of module {{module}}.
$this->assertEqual(TRUE, TRUE, 'Test Unit Generated via App Console.');
}
Expand Down

0 comments on commit 0da6e33

Please sign in to comment.