Skip to content

Commit aa50aba

Browse files
authored
Merge pull request #2 from yokai-php/issue.1-work.with.private.service.in.controller
Work with private services in controller
2 parents d2faba0 + 9fdf73a commit aa50aba

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,17 @@ services:
8989
admin.pull_request:
9090
class: App\Admin\PullRequestAdmin
9191
public: true
92-
arguments: [~, App\Entity\PullRequest, 'Yokai\SonataWorkflow\Controller\WorkflowController']
92+
arguments: [~, App\Entity\PullRequest, Yokai\SonataWorkflow\Controller\WorkflowController]
9393
tags:
9494
- { name: 'sonata.admin', manager_type: orm, label: PullRequest }
9595
admin.extension.workflow:
9696
class: Yokai\SonataWorkflow\Admin\Extension\WorkflowExtension
9797
public: true
9898
arguments:
99-
- "@workflow.registry"
99+
- '@workflow.registry'
100+
Yokai\SonataWorkflow\Controller\WorkflowController:
101+
autowire: true
102+
tags: ['controller.service_arguments']
100103

101104
sonata_admin:
102105
extensions:
@@ -105,6 +108,10 @@ sonata_admin:
105108
- admin.pull_request
106109
```
107110
111+
> **note**: You may noticed that we also registered the controller
112+
`Yokai\SonataWorkflow\Controller\WorkflowController` as a service.
113+
It is important, because it needs the workflow registry service to work.
114+
108115
### More specific extension per admin
109116

110117
But the extension accepts many options if you wish to customize the behavior.
@@ -124,7 +131,7 @@ services:
124131
class: Yokai\SonataWorkflow\Admin\Extension\WorkflowExtension
125132
public: true
126133
arguments:
127-
- "@workflow.registry"
134+
- '@workflow.registry'
128135
- workflow_name: pull_request
129136
no_transition_label: No transition for pull request
130137
no_transition_icon: fa fa-times
@@ -135,6 +142,9 @@ services:
135142
start_review: fa fa-search
136143
merge: fa fa-check
137144
close: fa fa-times
145+
Yokai\SonataWorkflow\Controller\WorkflowController:
146+
autowire: true
147+
tags: ['controller.service_arguments']
138148
139149
sonata_admin:
140150
extensions:
@@ -171,7 +181,7 @@ services:
171181
admin.pull_request:
172182
class: App\Admin\PullRequestAdmin
173183
public: true
174-
arguments: [~, App\Entity\PullRequest, 'App\Admin\Controller\PullRequestController']
184+
arguments: [~, App\Entity\PullRequest, App\Admin\Controller\PullRequestController]
175185
tags:
176186
- { name: 'sonata.admin', manager_type: orm, label: PullRequest }
177187
```

src/Controller/WorkflowControllerTrait.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
use Sonata\AdminBundle\Admin\AdminInterface;
77
use Sonata\AdminBundle\Exception\LockException;
88
use Sonata\AdminBundle\Exception\ModelManagerException;
9+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
910
use Symfony\Component\HttpFoundation\Request;
1011
use Symfony\Component\HttpFoundation\Response;
1112
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1213
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1314
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
1415
use Symfony\Component\Workflow\Exception\LogicException;
16+
use Symfony\Component\Workflow\Registry;
1517
use Symfony\Component\Workflow\Workflow;
1618

1719
/**
@@ -31,6 +33,21 @@
3133
*/
3234
trait WorkflowControllerTrait
3335
{
36+
/**
37+
* @var Registry
38+
*/
39+
private $workflowRegistry;
40+
41+
/**
42+
* @param Registry $workflowRegistry
43+
*
44+
* @required Symfony DI autowiring
45+
*/
46+
public function setWorkflowRegistry(Registry $workflowRegistry)
47+
{
48+
$this->workflowRegistry = $workflowRegistry;
49+
}
50+
3451
/**
3552
* @param Request $request
3653
*
@@ -137,11 +154,25 @@ public function workflowApplyTransitionAction(Request $request)
137154
*/
138155
protected function getWorkflow($object)
139156
{
140-
if (method_exists($this, 'get')) {
141-
return $this->get('workflow.registry')->get($object);
157+
$registry = $this->workflowRegistry;
158+
if ($registry === null) {
159+
try {
160+
if (method_exists($this, 'get')) {
161+
$registry = $this->get('workflow.registry');
162+
} else {
163+
$registry = $this->getContainer()->get('workflow.registry');
164+
}
165+
} catch (ServiceNotFoundException $exception) {
166+
throw new \LogicException('Could not find the "workflow.registry" service. '.
167+
'You should either provide it via setter injection in your controller service definition '.
168+
'or make it public in your project.',
169+
0,
170+
$exception
171+
);
172+
}
142173
}
143174

144-
return $this->getContainer()->get('workflow.registry')->get($object);
175+
return $registry->get($object);
145176
}
146177

147178
/**

tests/Controller/WorkflowControllerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Sonata\AdminBundle\Admin\Pool;
88
use Sonata\AdminBundle\Exception\LockException;
99
use Sonata\AdminBundle\Exception\ModelManagerException;
10+
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
1011
use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
1112
use Symfony\Component\DependencyInjection\ContainerInterface;
1213
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -84,10 +85,12 @@ public function setUp()
8485
$this->container->get('session')->willReturn(new Session(new MockArraySessionStorage(), null, $this->flashBag));
8586
$this->container->get('translator')->willReturn($this->translator);
8687
$this->container->has('logger')->willReturn(false);
88+
$this->container->get('admin.pull_request.template_registry')->willReturn($this->prophesize(TemplateRegistryInterface::class)->reveal());
8789

8890
$this->admin->isChild()->willReturn(false);
8991
$this->admin->setRequest($this->request)->willReturn(null);
9092
$this->admin->getIdParameter()->willReturn('id');
93+
$this->admin->getCode()->willReturn('admin.pull_request');
9194
}
9295

9396
public function tearDown()

0 commit comments

Comments
 (0)