1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2024 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupGateway\GatewayBundle\Container; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use Psr\Container\ContainerInterface; |
23
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ! This should be a temporary construction ! |
27
|
|
|
* |
28
|
|
|
* The container controller is a means to allow access to the service container |
29
|
|
|
* in the controllers. Allowing the controller actions to ->get services from |
30
|
|
|
* within the controller actions. |
31
|
|
|
* |
32
|
|
|
* This is highly discouraged, but we needed it to get a working Gateway with |
33
|
|
|
* SF6 support. See: https://www.pivotaltracker.com/story/show/187475121 |
34
|
|
|
*/ |
35
|
|
|
class ContainerController extends AbstractController |
36
|
|
|
{ |
37
|
|
|
/** @var ContainerInterface */ |
38
|
|
|
protected $container; |
39
|
|
|
|
40
|
|
|
/** @var ContainerInterface */ |
41
|
|
|
protected $serviceContainer; |
42
|
|
|
|
43
|
|
|
public function setContainer(ContainerInterface $container): ?ContainerInterface |
44
|
|
|
{ |
45
|
|
|
$previous = $this->container ?? null; |
46
|
|
|
$this->container = $container; |
47
|
|
|
|
48
|
|
|
return $previous; |
49
|
|
|
} |
50
|
|
|
public function setServiceContainer(ContainerInterface $container): void |
51
|
|
|
{ |
52
|
|
|
$this->serviceContainer = $container; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function get(string $serviceName): mixed |
56
|
|
|
{ |
57
|
|
|
$logger = $this->serviceContainer->get('logger'); |
58
|
|
|
$logger->info( |
59
|
|
|
sprintf( |
60
|
|
|
'Reading the "%s" service from the container (temporary ContainerController solution)', |
61
|
|
|
$serviceName |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
try { |
65
|
|
|
$service = $this->container->get($serviceName); |
66
|
|
|
} catch (Exception) { |
67
|
|
|
$service = $this->serviceContainer->get($serviceName); |
68
|
|
|
} |
69
|
|
|
return $service; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|