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
|
4 |
|
public function __construct(Registry $doctrine) |
22
|
|
|
{ |
23
|
4 |
|
$this->doctrine = $doctrine; |
24
|
4 |
|
} |
25
|
|
|
|
26
|
|
|
private function getDoctrine() |
27
|
|
|
{ |
28
|
|
|
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
|
|
|
public function executeReal(Action $action, $source, $arguments) |
38
|
|
|
{ |
39
|
|
|
return $this->execute($action, $source, $arguments, false); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function execute(Action $action, $source = 'internal', array $changeSet = [], $virtual = false) |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
//TODO: Find and run executor here |
46
|
|
|
|
47
|
|
|
if (!$virtual) { |
48
|
|
|
list($executor, $method) = explode(':', $action->getExecutor()); |
49
|
|
|
|
50
|
|
|
$executor = 'AppBundle\Action\Executor\\' . ucfirst($executor); |
51
|
|
|
|
52
|
|
|
if (!class_exists($executor)) { |
53
|
|
|
throw new \Exception('Unknown executor: ' . $executor); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @var ExecutorInterface $executor */ |
57
|
|
|
$executor = new $executor(); |
58
|
|
|
$executor->setDoctrine($this->getDoctrine()); |
59
|
|
|
|
60
|
|
|
if (isset($changeSet['container'])) { |
61
|
|
|
$executor->setContainer($changeSet['container']); |
62
|
|
|
unset($changeSet['container']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!method_exists($executor, $method)) { |
66
|
|
|
throw new \Exception('Unknown executor method: ' . $action->getExecutor().'()'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$executor->setParameters($changeSet); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$result = $executor->{$method}($action); |
73
|
|
|
} catch (\Exception $exception) { |
74
|
|
|
$result = $exception->getMessage(); |
75
|
|
|
} |
76
|
|
|
$changeSet['result']=$result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$state = new ActionHistory(); |
80
|
|
|
$state->setAction($action); |
81
|
|
|
$state->setTime(new \DateTime()); |
82
|
|
|
$state->setSource($source); |
83
|
|
|
$state->setPerformed(!$virtual); |
84
|
|
|
$state->setChangeSet(json_encode($changeSet)); |
85
|
|
|
|
86
|
|
|
$device = $action->getDevice()->setState($changeSet); |
87
|
|
|
|
88
|
|
|
$this->getDoctrine()->getManagerForClass('AppBundle:ActionHistory')->persist($state); |
89
|
|
|
$this->getDoctrine()->getManagerForClass('AppBundle:Device')->persist($device); |
90
|
|
|
|
91
|
|
|
$this->getDoctrine()->getManager()->flush(); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function runAction($actionName, array $parameters) |
97
|
|
|
{ |
98
|
|
|
$action = $this->doctrine->getRepository('AppBundle:Action')->findOneBy(['name'=>$actionName]); |
99
|
|
|
if (!$action) { |
100
|
|
|
throw new \Exception("Action ".$actionName." not found"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
if (isset($parameter['container'])) { |
|
|
|
|
105
|
|
|
$args['container'] = $parameter['container']; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
$this->executeReal($action, "internalcall", $parameters); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.