|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2017 RunOpenCode. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace RunOpenCode\Bundle\QueryResourcesLoader\Manager; |
|
11
|
|
|
|
|
12
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Contract\ExceptionInterface; |
|
13
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Contract\ExecutorInterface; |
|
14
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Contract\ManagerInterface; |
|
15
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\ExecutionException; |
|
16
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\RuntimeException; |
|
17
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\SourceNotFoundException; |
|
18
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\SyntaxException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class TwigQuerySourceManager |
|
22
|
|
|
* |
|
23
|
|
|
* Twig powered query executor. |
|
24
|
|
|
* |
|
25
|
|
|
* @package RunOpenCode\Bundle\QueryResourcesLoader\Manager |
|
26
|
|
|
*/ |
|
27
|
|
|
class TwigQuerySourceManager implements ManagerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Twig_Environment |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $twig; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ExecutorInterface[] |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $executors; |
|
38
|
|
|
|
|
39
|
10 |
|
public function __construct(\Twig_Environment $twig) |
|
40
|
|
|
{ |
|
41
|
10 |
|
$this->twig = $twig; |
|
42
|
10 |
|
$this->executors = array(); |
|
43
|
10 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
7 |
|
public function get($name, array $args = array()) |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
7 |
|
return $this->twig->render($name, $args); |
|
52
|
3 |
|
} catch (\Twig_Error_Loader $e) { |
|
53
|
1 |
|
throw new SourceNotFoundException(sprintf('Could not find query source: "%s".', $name), 0, $e); |
|
54
|
2 |
|
} catch (\Twig_Error_Syntax $e) { |
|
55
|
1 |
|
throw new SyntaxException(sprintf('Query source "%s" contains Twig syntax error and could not be compiled.', $name), 0, $e); |
|
56
|
1 |
|
} catch (\Exception $e) { |
|
57
|
1 |
|
throw new RuntimeException('Unknown exception occured', 0, $e); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function has($name) |
|
65
|
|
|
{ |
|
66
|
2 |
|
return $this->twig->getLoader()->exists($name); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
* |
|
72
|
|
|
* @throws RuntimeException |
|
73
|
|
|
* @throws ExecutionException |
|
74
|
|
|
*/ |
|
75
|
4 |
|
public function execute($name, array $args = array(), array $types = array(), $executor = 'default') |
|
76
|
|
|
{ |
|
77
|
4 |
|
if (!array_key_exists($executor, $this->executors)) { |
|
78
|
1 |
|
throw new RuntimeException(sprintf('Requested executor "%s" does not exists.', $executor)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
$executor = $this->executors[$executor]; |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
/** |
|
85
|
|
|
* @var ExecutorInterface $executor |
|
86
|
|
|
*/ |
|
87
|
3 |
|
return $executor->execute($this->get($name, $args), $args, $types); |
|
88
|
2 |
|
} catch (\Exception $e) { |
|
89
|
|
|
|
|
90
|
2 |
|
if ($e instanceof ExceptionInterface) { |
|
91
|
1 |
|
throw $e; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
throw new ExecutionException(sprintf('Query "%s" could not be executed.', $name), 0, $e); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Register query executor. |
|
100
|
|
|
* |
|
101
|
|
|
* @param ExecutorInterface $executor |
|
102
|
|
|
* @param string $name |
|
103
|
|
|
*/ |
|
104
|
9 |
|
public function registerExecutor(ExecutorInterface $executor, $name) |
|
105
|
|
|
{ |
|
106
|
9 |
|
$this->executors[$name] = $executor; |
|
107
|
9 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|