1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JobQueue\Infrastructure; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The following services have to be set in /config/services_{env}.yml : |
13
|
|
|
* |
14
|
|
|
* @property \JobQueue\Domain\Task\Queue $queue |
15
|
|
|
* @property \Psr\Log\LoggerInterface $logger |
16
|
|
|
*/ |
17
|
|
|
final class ServiceContainer |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
* @var self |
22
|
|
|
*/ |
23
|
|
|
protected static $instance; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @var ContainerInterface |
28
|
|
|
*/ |
29
|
|
|
protected $services; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @param ContainerInterface $services |
34
|
|
|
*/ |
35
|
1 |
|
public function __construct(ContainerInterface $services) |
36
|
|
|
{ |
37
|
1 |
|
$this->services = $services; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
* @return ServiceContainer |
43
|
|
|
*/ |
44
|
8 |
|
public static function getInstance(): self |
45
|
|
|
{ |
46
|
8 |
|
if (self::$instance) { |
47
|
7 |
|
return self::$instance; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
if (class_exists('\Composer\Autoload\ClassLoader')) { |
51
|
|
|
// Find config dir using composer ClassLoader class |
52
|
1 |
|
$reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class); |
53
|
1 |
|
$root = dirname(dirname(dirname($reflection->getFileName()))); |
54
|
|
|
|
55
|
|
|
} else { |
56
|
|
|
// If composer has net been user, try to guess the root path |
57
|
|
|
$root = dirname(dirname(__DIR__)); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$cache = sprintf('%s/cache/services_%s.php', $root, Environment::getName()); |
61
|
1 |
|
if (Environment::isProd() and is_readable($cache)) { |
62
|
|
|
// Retrieve services from the cache, if exists... |
63
|
|
|
require_once $cache; |
64
|
|
|
$services = new \ProjectServiceContainer; |
65
|
|
|
|
66
|
|
|
} else { |
67
|
|
|
// ... otherwise compile & cache the configuration |
68
|
1 |
|
$services = new ContainerBuilder; |
69
|
|
|
|
70
|
1 |
|
$loader = new YamlFileLoader($services, new FileLocator); |
71
|
1 |
|
$loader->load(sprintf('%s/config/services_%s.yml', $root, Environment::getName())); |
72
|
|
|
|
73
|
|
|
// Compile and cache production config |
74
|
1 |
|
if (Environment::isProd()) { |
75
|
|
|
$services->compile(true); |
76
|
|
|
|
77
|
|
|
if (!is_dir($cacheDir = dirname($cache))) { |
78
|
|
|
mkdir($cacheDir, 0777); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
file_put_contents($cache, (new PhpDumper($services))->dump()); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return self::$instance = new self($services); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
* @param $service |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
2 |
|
public function __isset($service) |
94
|
|
|
{ |
95
|
2 |
|
return $this->services->has($service); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* |
100
|
|
|
* @param $service |
101
|
|
|
* @return mixed|object |
102
|
|
|
*/ |
103
|
8 |
|
public function __get($service) |
104
|
|
|
{ |
105
|
8 |
|
return $this->services->get($service); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|