Skip to content

Commit 0da6e33

Browse files
committed
Merge pull request #105 from omero/changes-controller-command
Changes controller command
2 parents 13dafaf + 33433f2 commit 0da6e33

File tree

7 files changed

+88
-47
lines changed

7 files changed

+88
-47
lines changed

Tests/Command/GeneratorControllerCommandTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class GeneratorControllerCommandTest extends GenerateCommandTest
1515
*/
1616
public function testInteractive($options, $expected, $input)
1717
{
18-
list($module, $class_name, $test, $services, $routing_update) = $expected;
18+
list($module, $class_name, $method_name, $route, $test, $services) = $expected;
1919

2020
$generator = $this->getGenerator();
2121
$generator
2222
->expects($this->once())
2323
->method('generate')
24-
->with($module, $class_name, $test, $services, $routing_update)
24+
->with($module, $class_name, $method_name, $route, $test, $services)
2525
;
2626

2727
$command = $this->getCommand($generator,$input);
@@ -46,27 +46,27 @@ public function getInteractiveData()
4646
// Inline options
4747
[],
4848
// Expected options
49-
['foo', 'FooController', true, $services, true],
49+
['foo', 'FooController', 'index', 'foo/index', true, $services],
5050
// User input options
51-
"foo\nFooController\nyes\nyes\ntwig\n\nyes\n",
51+
"foo\nFooController\nindex\nfoo/index\nyes\n\ntwig\nyes\n",
5252
],
5353
// case two
5454
[
5555
// Inline options
5656
['--module'=>'foo'],
5757
// Expected options
58-
['foo', 'FooController', true, null, true],
58+
['foo', 'FooController', 'index', 'foo/index', true, null],
5959
// User input options
60-
"FooController\nyes\nno\nyes\n",
60+
"FooController\nindex\nfoo/index\nyes\nno\n",
6161
],
6262
// case three
6363
[
6464
// Inline options
6565
['--module'=>'foo'],
6666
// Expected options
67-
['foo', 'FooController', false, null, false],
67+
['foo', 'FooController', 'index', 'foo/index', false, null],
6868
// User input options
69-
"FooController\nno\nno\nno\n",
69+
"FooController\nindex\nfoo/index\nno\nno\n",
7070
],
7171
];
7272
}

src/Command/GeneratorControllerCommand.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ protected function configure()
2424
->setDefinition(array(
2525
new InputOption('module','',InputOption::VALUE_REQUIRED, 'The name of the module'),
2626
new InputOption('class-name','',InputOption::VALUE_OPTIONAL, 'Controller name'),
27+
new InputOption('method-name','',InputOption::VALUE_OPTIONAL, 'The method name'),
28+
new InputOption('route','',InputOption::VALUE_OPTIONAL, 'The route path'),
2729
new InputOption('services','',InputOption::VALUE_OPTIONAL, 'Load services'),
28-
new InputOption('routing', '', InputOption::VALUE_NONE, 'Update routing'),
2930
new InputOption('test', '', InputOption::VALUE_NONE, 'Generate test'),
3031
))
3132
->setDescription('Generate controller')
@@ -49,15 +50,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
4950

5051
$module = $input->getOption('module');
5152
$class_name = $input->getOption('class-name');
53+
$method_name = $input->getOption('method-name');
54+
$route = $input->getOption('route');
5255
$test = $input->getOption('test');
5356
$services = $input->getOption('services');
54-
$update_routing = $input->getOption('routing');
5557

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

5961
$this->getGenerator()
60-
->generate($module, $class_name, $test, $build_services, $update_routing);
62+
->generate($module, $class_name, $method_name, $route, $test, $build_services);
6163

6264
$errors = '';
6365
$dialog->writeGeneratorSummary($output, $errors);
@@ -82,13 +84,40 @@ protected function interact(InputInterface $input, OutputInterface $output)
8284
// --class-name option
8385
$class_name = $input->getOption('class-name');
8486
if (!$class_name) {
85-
$name = $dialog->ask(
87+
$class_name = $dialog->ask(
8688
$output,
8789
$dialog->getQuestion('Enter the controller name', 'DefaultController'),
8890
'DefaultController'
8991
);
9092
}
91-
$input->setOption('class-name', $name);
93+
$input->setOption('class-name', $class_name);
94+
95+
// --method-name option & --route option
96+
if($class_name != 'DefaultController'){
97+
$method_name = $input->getOption('method-name');
98+
if (!$method_name) {
99+
$method_name = $dialog->ask(
100+
$output,
101+
$dialog->getQuestion('Enter the method name', 'index'),
102+
'index'
103+
);
104+
}
105+
106+
$route = $input->getOption('route');
107+
if (!$route) {
108+
$route = $dialog->ask(
109+
$output,
110+
$dialog->getQuestion('Enter the route path', $method_name.'/index'),
111+
$method_name.'/index'
112+
);
113+
}
114+
}
115+
else{
116+
$method_name = 'hello';
117+
$route = '/hello/{name}';
118+
}
119+
$input->setOption('method-name', $method_name);
120+
$input->setOption('route', $route);
92121

93122
// --test option
94123
$test = $input->getOption('test');
@@ -105,17 +134,6 @@ protected function interact(InputInterface $input, OutputInterface $output)
105134
// @see use Drupal\AppConsole\Command\Helper\ServicesTrait::servicesQuestion
106135
$services_collection = $this->servicesQuestion($input, $output, $dialog);
107136
$input->setOption('services', $services_collection);
108-
109-
// --routing option
110-
$routing = $input->getOption('routing');
111-
if (!$routing && $dialog->askConfirmation(
112-
$output,
113-
$dialog->getQuestion('Update routing file?', 'yes', '?'),
114-
true
115-
)) {
116-
$routing = true;
117-
}
118-
$input->setOption('routing', $routing);
119137
}
120138

121139
/**

src/Generator/ControllerGenerator.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
class ControllerGenerator extends Generator
1010
{
1111

12-
public function generate($module, $class_name, $test, $services, $update_routing)
12+
public function generate($module, $class_name, $method_name, $route, $test, $services)
1313
{
1414
$path = DRUPAL_ROOT.'/'.drupal_get_path('module', $module);
1515

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

1818
$parameters = array(
19-
'name' => $class_name,
19+
'class_name' => $class_name,
2020
'services' => $services,
21-
'module' => $module
21+
'module' => $module,
22+
'method_name' => $method_name,
23+
'route' => $route,
2224
);
2325

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

30-
if ($update_routing) {
31-
$this->renderFile('module/controller-routing.yml.twig',
32-
DRUPAL_ROOT.'/modules/'.$module.'/'.$module.'.routing.yml',
33-
$parameters,
34-
FILE_APPEND
35-
);
36-
}
32+
$this->renderFile(
33+
'module/controller-routing.yml.twig',
34+
$path.'/'.$module.'.routing.yml',
35+
$parameters,
36+
FILE_APPEND
37+
);
3738

3839
if ($test) {
3940
$this->renderFile(

src/Generator/Generator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected function render($template, $parameters)
4141

4242
$twig->addFunction($this->getServiceAsParamater());
4343
$twig->addFunction($this->getServiceAsParamaterKeys());
44+
$twig->addFunction($this->getArgumentsFromRoute());
4445

4546
return $twig->render($template, $parameters);
4647
}
@@ -87,4 +88,20 @@ public function getServiceAsParamaterKeys()
8788
return $servicesAsParametersKeys;
8889
}
8990

91+
public function getArgumentsFromRoute()
92+
{
93+
$argumentsFromRoute = new \Twig_SimpleFunction('argumentsFromRoute', function ($route){
94+
$parameters = [];
95+
$parameters = array_filter(explode("/", $route), function($value){
96+
return (strpos($value, "}") > 0) ? : false;
97+
});
98+
$parameters = array_map(function ($value){
99+
return "$".substr($value, 1, -1);
100+
}, $parameters);
101+
102+
return $parameters;
103+
});
104+
105+
return $argumentsFromRoute;
106+
}
90107
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
{% if name is defined %}
2-
{{ module }}.hello:
3-
path: '/{{module}}/hello/{name}'
1+
{% if class_name is defined %}
2+
{{ module }}.{{ method_name }}:
3+
path: '{{ route }}'
44
defaults:
5-
_content: '\Drupal\{{ module }}\Controller\{{ name }}::hello'
5+
_content: '\Drupal\{{ module }}\Controller\{{ class_name }}::{{ method_name }}'
66
_title: '{{module}} Title'
77
requirements:
88
_permission: 'access content'
99
{% endif %}
10+

src/Resources/skeleton/module/module.controller.php.twig

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
88
{% include 'skeleton/module/shared/services-use-operator.php.twig' %}
99
{% endif %}
1010
11-
class {{ name }} extends ControllerBase {% if services is defined and services is not empty %}implements ContainerInjectionInterface {% endif %}{{"\n"}}{
11+
class {{ class_name }} extends ControllerBase {% if services is defined and services is not empty %}implements ContainerInjectionInterface {% endif %}{{"\n"}}{
1212
{% if services is defined and services is not empty %}
1313
{% include 'skeleton/module/shared/services-class-properties-declaration.php.twig' %}
1414
@@ -26,11 +26,15 @@ class {{ name }} extends ControllerBase {% if services is defined and services i
2626
{% endif %}
2727
2828
/**
29-
* hello
30-
* @param string $name
29+
* {{method_name}}
3130
* @return string
3231
*/
33-
public function hello($name) {
34-
return "Hello " . $name . "!";
32+
public function {{method_name}}({{ argumentsFromRoute(route)|join(', ') }})
33+
{
34+
{% if class_name == "DefaultController" %}
35+
return "Hello ".$name." !";
36+
{% else %}
37+
return "Implements {{method_name}}";
38+
{% endif %}
3539
}
3640
}

src/Resources/skeleton/module/module.test.twig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use Drupal\simpletest\WebTestBase;
1212
/**
1313
* Provides automated tests for the {{module}} module.
1414
*/
15-
class {{name}}Test extends WebTestBase {
15+
class {{class_name}}Test extends WebTestBase {
1616
1717
public static function getInfo() {
1818
return array(
19-
'name' => "{{module}} {{name}}'s controller functionality",
20-
'description' => 'Test Unit for module {{module}} and controller {{name}}.',
19+
'name' => "{{module}} {{class_name}}'s controller functionality",
20+
'description' => 'Test Unit for module {{module}} and controller {{class_name}}.',
2121
'group' => 'Other',
2222
);
2323
}
@@ -29,7 +29,7 @@ class {{name}}Test extends WebTestBase {
2929
/**
3030
* Tests {{module}} functionality.
3131
*/
32-
function test{{name}}() {
32+
function test{{class_name}}() {
3333
//Check that the basic functions of module {{module}}.
3434
$this->assertEqual(TRUE, TRUE, 'Test Unit Generated via App Console.');
3535
}

0 commit comments

Comments
 (0)