|
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
|
|
|
* This class may be used to make extra services available to jobs from workers. |
|
13
|
|
|
* |
|
14
|
|
|
* The following services have to be set in /config/services_{env}.yml : |
|
15
|
|
|
* |
|
16
|
|
|
* @property \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher |
|
17
|
|
|
* @property \Psr\Log\LoggerInterface $logger |
|
18
|
|
|
* @property \JobQueue\Domain\Task\Queue $queue |
|
19
|
|
|
*/ |
|
20
|
|
|
class ServiceContainer |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var static |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $instance; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @var ContainerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $services; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @param ContainerInterface $services |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function __construct(ContainerInterface $services) |
|
39
|
|
|
{ |
|
40
|
1 |
|
$this->services = $services; |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
* @return static |
|
46
|
|
|
*/ |
|
47
|
13 |
|
public static function getInstance() |
|
48
|
|
|
{ |
|
49
|
13 |
|
if (self::$instance) { |
|
50
|
12 |
|
return self::$instance; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
$services = self::getServices(); |
|
54
|
|
|
|
|
55
|
1 |
|
return self::$instance = new static($services); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
* @return ContainerInterface |
|
61
|
|
|
*/ |
|
62
|
1 |
|
protected static function getServices(): ContainerInterface |
|
63
|
|
|
{ |
|
64
|
1 |
|
$cache = self::getConfigurationCachePath(); |
|
65
|
|
|
|
|
66
|
1 |
|
if (Environment::isProd() and is_readable($cache)) { |
|
67
|
|
|
// Retrieve services from the cache, if exists... |
|
68
|
|
|
require_once $cache; |
|
69
|
|
|
$services = new \ProjectServiceContainer; |
|
70
|
|
|
|
|
71
|
|
|
} else { |
|
72
|
|
|
// ... otherwise compile & cache the configuration |
|
73
|
1 |
|
$services = new ContainerBuilder; |
|
74
|
|
|
|
|
75
|
1 |
|
$loader = new YamlFileLoader($services, new FileLocator); |
|
76
|
1 |
|
$loader->load(self::getConfigurationFilePath()); |
|
77
|
|
|
|
|
78
|
|
|
// Compile and cache production config |
|
79
|
1 |
|
if (Environment::isProd()) { |
|
80
|
|
|
$services->compile(true); |
|
81
|
|
|
|
|
82
|
|
|
if (!is_dir($cacheDir = dirname($cache))) { |
|
83
|
|
|
mkdir($cacheDir, 0777, true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
file_put_contents($cache, (new PhpDumper($services))->dump()); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $services; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
1 |
|
protected static function getConfigurationFilePath(): string |
|
98
|
|
|
{ |
|
99
|
|
|
// Get path from environment variables |
|
100
|
1 |
|
if ($path = (string) getenv('JOBQUEUE_CONFIG_PATH')) { |
|
101
|
|
|
return $path; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Get dir path depending on how component is used |
|
105
|
1 |
|
if (class_exists('\Composer\Autoload\ClassLoader')) { |
|
106
|
|
|
// Find config dir using composer ClassLoader class |
|
107
|
1 |
|
$reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class); |
|
108
|
1 |
|
$root = dirname(dirname(dirname($reflection->getFileName()))); |
|
109
|
|
|
|
|
110
|
|
|
} else { |
|
111
|
|
|
// If composer has not been user, try to guess the root path |
|
112
|
|
|
$root = dirname(dirname(__DIR__)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
return sprintf('%s/config/services_%s.yml', $root, Environment::getName()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
1 |
|
protected static function getConfigurationCachePath(): string |
|
123
|
|
|
{ |
|
124
|
|
|
// Get dir path from environment variables |
|
125
|
1 |
|
if (!$dir = (string) getenv('JOBQUEUE_CACHE_PATH')) { |
|
126
|
1 |
|
$dir = sys_get_temp_dir(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
return sprintf('%s/jobqueue_services.php', $dir); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* |
|
134
|
|
|
* @param $service |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function __isset($service) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->services->has($service); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* |
|
144
|
|
|
* @param $service |
|
145
|
|
|
* @return mixed|object |
|
146
|
|
|
*/ |
|
147
|
13 |
|
public function __get($service) |
|
148
|
|
|
{ |
|
149
|
13 |
|
return $this->services->get($service); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|