1 : <?php
2 : namespace phMagick\Core;
3 : use phMagick\Core\Exception\SystemException;
4 :
5 : class Runner extends ActionCollection
6 : {
7 : private $logger;
8 : private $debug;
9 :
10 : private $settings = null;
11 :
12 :
13 : public function getLogger()
14 : {
15 0 : return $this->getVar('logger');
16 : }
17 :
18 : public function setLogger(Logger $logger)
19 : {
20 1 : return $this->logger = $logger;
21 : }
22 :
23 : public function getSettings()
24 : {
25 0 : $settings = $this->getVar('settings');
26 0 : return $settings->getInstance();
27 : }
28 :
29 : private function getVar($name) {
30 0 : if (is_null($this->$name)) {
31 0 : $class = 'phMagick\Core\\' . ucfirst($name);
32 0 : $this->$name = new $class();
33 0 : }
34 :
35 0 : return $this->$name;
36 : }
37 :
38 : function isWindows()
39 : {
40 0 : return (!(strstr(PHP_OS, 'WIN') === FALSE));
41 : }
42 :
43 : function debug($value = TRUE)
44 : {
45 1 : $this->debug = $value;
46 1 : return $this;
47 : }
48 :
49 : function debugMode()
50 : {
51 0 : return $this->debug;
52 : }
53 :
54 : private function execute(Command $cmdCls)
55 : {
56 0 : $ret = null;
57 0 : $out = array();
58 0 : $log = $this->getLogger();
59 0 : $cmd = $cmdCls->toString();
60 :
61 0 : if ($this->isWindows()) {
62 0 : $cmd = str_replace('(', '\(', $cmd);
63 0 : $cmd = str_replace(')', '\)', $cmd);
64 0 : }
65 :
66 0 : exec($cmd . ' 2>&1', $out, $ret);
67 0 : $log->append(array('cmd' => $cmd, 'return' => $ret, 'output' => $out));
68 :
69 0 : if ($ret != 0) {
70 0 : $msg = 'Error #' . $ret . ' while executing "' . $cmd . '"';
71 :
72 0 : if ($this->debugMode()) {
73 0 : $msg .= "\n Debug Log: \n" . $log;
74 0 : }
75 :
76 0 : throw new SystemException($msg);
77 : }
78 :
79 0 : return $ret;
80 : }
81 :
82 : private function runAll()
83 : {
84 0 : $items = $this->getAll();
85 0 : $destination = null;
86 :
87 0 : foreach ($items as $action) {
88 0 : if (!is_null($destination)) {
89 0 : $action->setSource($destination);
90 0 : }
91 :
92 0 : $this->execute($action->getShellCommand());
93 0 : $destination = $action->getDestination();
94 0 : }
95 0 : }
96 :
97 : public function run(Action $action = null)
98 : {
99 0 : if (!is_null($action)) {
100 0 : $this->add($action);
101 0 : }
102 :
103 0 : $this->runAll();
104 0 : }
105 : }
|