Skip to content

Commit f1da037

Browse files
committed
Add DI to TwigExtension generator
1 parent bef5602 commit f1da037

File tree

7 files changed

+32
-34
lines changed

7 files changed

+32
-34
lines changed

scripts/run-sut-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ if [ $TARGET_TEST = all -o $TARGET_TEST = service ]; then
193193
$DCG d8:service:param-converter -d $MODULE_DIR -a '{"name":"Zippo","machine_name":"zippo","parameter_type":"example","class":"ExampleParamConverter"}'
194194
$DCG d8:service:route-subscriber -d $MODULE_DIR -a '{"name":"Zippo","machine_name":"zippo"}'
195195
$DCG d8:service:theme-negotiator -d $MODULE_DIR -a '{"name":"Zippo","machine_name":"zippo","class":"ZippoThemeNegotiator"}'
196-
$DCG d8:service:twig-extension -d $MODULE_DIR -a '{"name":"Zippo","machine_name":"zippo","class":"ZippoTwigExtension","di":"Yes"}'
196+
$DCG d8:service:twig-extension -d $MODULE_DIR -a '{"name":"Zippo","machine_name":"zippo","class":"ZippoTwigExtension","di":"No"}'
197197
$DCG d8:service:path-processor -d $MODULE_DIR -a '{"name":"Zippo","machine_name":"zippo","class":"PathProcessorZippo"}'
198198
$DCG d8:service:request-policy -d $MODULE_DIR -a '{"name":"Zippo","machine_name":"zippo","class":"Example"}'
199199
$DCG d8:service:response-policy -d $MODULE_DIR -a '{"name":"Zippo","machine_name":"zippo","class":"ExampleResponsePolicy"}'

src/Command/Drupal_8/Service/TwigExtension.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ class TwigExtension extends BaseGenerator {
2222
* {@inheritdoc}
2323
*/
2424
protected function interact(InputInterface $input, OutputInterface $output) {
25-
$questions = Utils::defaultQuestions();
25+
$questions = Utils::moduleQuestions();
2626
$default_class = function ($vars) {
2727
return Utils::camelize($vars['name']) . 'TwigExtension';
2828
};
2929
$questions['class'] = new Question('Class', $default_class);
30-
$questions['di'] = new ConfirmationQuestion('Would you like to inject dependencies?', FALSE);
31-
3230
$this->collectVars($input, $output, $questions);
3331

32+
$di_question = new ConfirmationQuestion('Would you like to inject dependencies?');
33+
if ($this->ask($input, $output, $di_question)) {
34+
$this->collectServices($input, $output);
35+
}
36+
3437
$this->addFile()
3538
->path('src/{class}.php')
3639
->template('d8/service/twig-extension.twig');
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
{% import 'lib/di.twig' as di %}
12
services:
23
{{ machine_name }}.twig_extension:
34
class: Drupal\{{ machine_name }}\{{ class }}
4-
{% if di %}
5-
arguments: ['@example']
5+
{% if services %}
6+
arguments: [{{ di.arguments(services) }}]
67
{% endif %}
78
tags:
89
- { name: twig.extension }

templates/d8/service/twig-extension.twig

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
1+
{% import 'lib/di.twig' as di %}
12
<?php
23
34
namespace Drupal\{{ machine_name }};
45
5-
{% if di %}
6-
use Drupal\example\ExampleInterface;
7-
6+
{% if services %}
7+
{{ di.use(services) }}
88
{% endif %}
99
/**
1010
* Twig extension.
1111
*/
1212
class {{ class }} extends \Twig_Extension {
1313
14-
{% if di %}
15-
/**
16-
* The example service.
17-
*
18-
* @var \Drupal\example\ExampleInterface
19-
*/
20-
protected $example;
14+
{% if services %}
15+
{{ di.properties(services) }}
2116
2217
/**
23-
* Constructs a new {{ class }} instance.
18+
* Constructs a new {{ class }} object.
2419
*
25-
* @param \Drupal\example\ExampleInterface $example
26-
* The example service.
20+
{{ di.annotation(services) }}
2721
*/
28-
public function __construct(ExampleInterface $example) {
29-
$this->example = $example;
22+
public function __construct({{ di.signature(services) }}) {
23+
{{ di.assignment(services) }}
3024
}
3125
3226
{% endif %}

tests/dcg/Generator/Drupal_8/Service/TwigExtensionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class TwigExtensionTest extends GeneratorBaseTest {
1515
'Module name [%default_name%]:' => 'Example',
1616
'Module machine name [example]:' => 'example',
1717
'Class [ExampleTwigExtension]:' => 'ExampleTwigExtension',
18-
'Would you like to inject dependencies? [No]:' => 'Yes',
18+
'Would you like to inject dependencies? [Yes]:' => 'Yes',
19+
'<1> Type the service name or use arrows up/down. Press enter to continue:' => 'entity_type.manager',
20+
'<2> Type the service name or use arrows up/down. Press enter to continue:' => "\n",
1921
];
2022

2123
protected $fixtures = [

tests/dcg/Generator/Drupal_8/Service/_twig_extension.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22

33
namespace Drupal\example;
44

5-
use Drupal\example\ExampleInterface;
5+
use Drupal\Core\Entity\EntityTypeManagerInterface;
66

77
/**
88
* Twig extension.
99
*/
1010
class ExampleTwigExtension extends \Twig_Extension {
1111

1212
/**
13-
* The example service.
13+
* The entity type manager.
1414
*
15-
* @var \Drupal\example\ExampleInterface
15+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
1616
*/
17-
protected $example;
17+
protected $entityTypeManager;
1818

1919
/**
20-
* Constructs a new ExampleTwigExtension instance.
20+
* Constructs a new ExampleTwigExtension object.
2121
*
22-
* @param \Drupal\example\ExampleInterface $example
23-
* The example service.
22+
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
23+
* The entity type manager.
2424
*/
25-
public function __construct(ExampleInterface $example) {
26-
$this->example = $example;
25+
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
26+
$this->entityTypeManager = $entity_type_manager;
2727
}
2828

2929
/**
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
services:
22
example.twig_extension:
33
class: Drupal\example\ExampleTwigExtension
4-
arguments: ['@example']
5-
tags:
6-
- { name: twig.extension }
4+
arguments: ['@entity_type.manager']

0 commit comments

Comments
 (0)