|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: chrl |
|
5
|
|
|
* Date: 04/03/17 |
|
6
|
|
|
* Time: 17:49 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace AppBundle\Action; |
|
10
|
|
|
|
|
11
|
|
|
use AppBundle\Action\Executor\ExecutorInterface; |
|
12
|
|
|
use AppBundle\Entity\Action; |
|
13
|
|
|
use AppBundle\Entity\ActionHistory; |
|
14
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
15
|
|
|
|
|
16
|
|
|
class Service |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
private $doctrine; |
|
20
|
|
|
|
|
21
|
5 |
|
public function __construct(Registry $doctrine) |
|
22
|
|
|
{ |
|
23
|
5 |
|
$this->doctrine = $doctrine; |
|
24
|
5 |
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
private function getDoctrine() |
|
27
|
|
|
{ |
|
28
|
1 |
|
return $this->doctrine; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
public function executeVirtual(Action $action, $source, array $changeSet) |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->execute($action, $source, $changeSet, true); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $source |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function executeReal(Action $action, $source, $arguments) |
|
41
|
|
|
{ |
|
42
|
1 |
|
return $this->execute($action, $source, $arguments, false); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function execute(Action $action, $source = 'internal', array $changeSet = [], $virtual = false) |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
//TODO: Find and run executor here |
|
49
|
|
|
|
|
50
|
1 |
|
if (!$virtual) { |
|
51
|
1 |
|
list($executor, $method) = explode(':', $action->getExecutor()); |
|
52
|
|
|
|
|
53
|
1 |
|
$executor = 'AppBundle\Action\Executor\\'.ucfirst($executor); |
|
54
|
|
|
|
|
55
|
1 |
|
if (!class_exists($executor)) { |
|
56
|
|
|
throw new \Exception('Unknown executor: '.$executor); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @var ExecutorInterface $executor */ |
|
60
|
1 |
|
$executor = new $executor(); |
|
61
|
1 |
|
$executor->setDoctrine($this->getDoctrine()); |
|
62
|
|
|
|
|
63
|
1 |
|
if (isset($changeSet['container'])) { |
|
64
|
|
|
$executor->setContainer($changeSet['container']); |
|
65
|
|
|
unset($changeSet['container']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
if (!method_exists($executor, $method)) { |
|
69
|
|
|
throw new \Exception('Unknown executor method: '.$action->getExecutor().'()'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
$executor->setParameters($changeSet); |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
1 |
|
$result = $executor->{$method}($action); |
|
76
|
|
|
} catch (\Exception $exception) { |
|
77
|
|
|
$result = $exception->getMessage(); |
|
78
|
|
|
} |
|
79
|
1 |
|
$changeSet['result'] = $result; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$state = new ActionHistory(); |
|
83
|
1 |
|
$state->setAction($action); |
|
84
|
1 |
|
$state->setTime(new \DateTime()); |
|
85
|
1 |
|
$state->setSource($source); |
|
86
|
1 |
|
$state->setPerformed(!$virtual); |
|
87
|
1 |
|
$state->setChangeSet(json_encode($changeSet)); |
|
88
|
|
|
|
|
89
|
1 |
|
$device = $action->getDevice()->setState($changeSet); |
|
90
|
|
|
|
|
91
|
1 |
|
$this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state); |
|
92
|
1 |
|
$this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device); |
|
93
|
|
|
|
|
94
|
1 |
|
$this->getDoctrine()->getManager()->flush(); |
|
95
|
|
|
|
|
96
|
1 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function runAction($actionName, array $parameters) |
|
100
|
|
|
{ |
|
101
|
|
|
$action = $this->doctrine->getRepository('AppBundle:Action')->findOneBy(['name'=>$actionName]); |
|
102
|
|
|
if (!$action) { |
|
103
|
|
|
throw new \Exception("Action ".$actionName." not found"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
if (isset($parameter['container'])) { |
|
|
|
|
|
|
108
|
|
|
$args['container'] = $parameter['container']; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
$this->executeReal($action, "internalcall", $parameters); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.