forked from michelsalib/BCCResqueBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContainerAwareJob.php
59 lines (49 loc) · 1.41 KB
/
ContainerAwareJob.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace BCC\ResqueBundle;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ContainerAwareJob extends Job
{
/**
* @var KernelInterface
*/
private $kernel = null;
/**
* @return ContainerInterface
*/
protected function getContainer()
{
if ($this->kernel === null) {
$this->kernel = $this->createKernel();
$this->kernel->boot();
}
return $this->kernel->getContainer();
}
public function setKernelOptions(array $kernelOptions)
{
$this->args = \array_merge($this->args, $kernelOptions);
}
/**
* @return KernelInterface
*/
protected function createKernel()
{
$finder = new Finder();
$finder->name('*Kernel.php')->depth(0)->in($this->args['kernel.root_dir']);
$results = iterator_to_array($finder);
$file = current($results);
$class = $file->getBasename('.php');
require_once $file;
return new $class(
isset($this->args['kernel.environment']) ? $this->args['kernel.environment'] : 'dev',
isset($this->args['kernel.debug']) ? $this->args['kernel.debug'] : true
);
}
public function tearDown()
{
if ($this->kernel) {
$this->kernel->shutdown();
}
}
}