Completed
Push — master ( f493e8...60bb4c )
by Andrii
03:26
created

AbstractController::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * Abstract controller.
19
 */
20
abstract class AbstractController extends \hidev\base\Controller
21
{
22
    protected $_before = [];
23
    protected $_after  = [];
24
    protected $_make   = ['load', 'save'];
25
26
    /**
27
     * @var array list of performed actions
28
     */
29
    protected $_done = [];
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function options($actionId)
35
    {
36
        return array_merge(parent::options($actionId), array_keys(Helper::getPublicVars(get_called_class())));
37
    }
38
39
    public function actionBefore()
40
    {
41
        return $this->runRequests($this->getBefore());
42
    }
43
44
    public function actionMake()
45
    {
46
        return $this->runActions($this->getMake());
47
    }
48
49
    public function actionAfter()
50
    {
51
        return $this->runRequests($this->getAfter());
52
    }
53
54
    public function actionLoad()
55
    {
56
        Yii::trace("Loading nothing for '$this->id'");
57
    }
58
59
    public function actionSave()
60
    {
61
        Yii::trace("Saving nothing for '$this->id'");
62
    }
63
64
    public function setBefore($requests)
65
    {
66
        $this->_before = array_merge($this->getBefore(), $this->normalizeTasks($requests));
67
    }
68
69
    public function getBefore()
70
    {
71
        return $this->_before;
72
    }
73
74
    public function setMake($requests)
75
    {
76
        $this->_make = array_merge($this->getMake(), $this->normalizeTasks($requests));
77
    }
78
79
    public function getMake()
80
    {
81
        return $this->_make;
82
    }
83
84
    public function setAfter($requests)
85
    {
86
        $this->_after = array_merge($this->getAfter(), $this->normalizeTasks($requests));
87
    }
88
89
    public function getAfter()
90
    {
91
        return $this->_after;
92
    }
93
94
    public function normalizeTasks($tasks)
95
    {
96
        if (!$tasks) {
97
            return [];
98
        } elseif (!is_array($tasks)) {
99
            $tasks = [(string) $tasks => 1];
100
        }
101
        $res = [];
102
        foreach ($tasks as $dep => $enabled) {
103
            $res[(string) (is_int($dep) ? $enabled : $dep)] = (bool) (is_int($dep) ? 1 : $enabled);
104
        }
105
106
        return $res;
107
    }
108
109
    /**
110
     * Runs array of requests. Stops on failure and returns exit code.
111
     * @param null|string|array $requests
112
     * @return int|Response exit code
113
     */
114 View Code Duplication
    public function runRequests($requests)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        foreach ($this->normalizeTasks($requests) as $request => $enabled) {
117
            if ($enabled) {
118
                $res = $this->runRequest($request);
119
                if (static::isNotOk($res)) {
120
                    return $res;
121
                }
122
            }
123
        }
124
125
        return 0;
126
    }
127
128
    public function runRequest($request)
129
    {
130
        return $request === null ? null : $this->module->runRequest($request);
131
    }
132
133
    public static function isNotOk($res)
134
    {
135
        return is_object($res) ? $res->exitStatus : $res;
136
    }
137
138
    /**
139
     * Runs list of actions.
140
     * TODO: think to redo with runRequests.
141
     * @param null|string|array $actions
142
     * @return int|Response exit code
143
     */
144 View Code Duplication
    public function runActions($actions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        foreach ($this->normalizeTasks($actions) as $action => $enabled) {
147
            if ($enabled) {
148
                $res = $this->runAction($action);
149
                if (static::isNotOk($res)) {
150
                    return $res;
151
                }
152
            }
153
        }
154
155
        return 0;
156
    }
157
158
    public function runAction($id, $params = [])
159
    {
160
        if ($this->isDone($id)) {
161
            return;
162
        }
163
        $result = parent::runAction($id, $params);
164
        $this->markDone($id);
165
166
        return $result;
167
    }
168
169
    public function isDone($action, $timestamp = null)
0 ignored issues
show
Unused Code introduced by
The parameter $timestamp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
    {
171
        if ($this->_done[$action]) {
172
            Yii::trace("Already done: '$this->id/$action'");
173
174
            return true;
175
        }
176
177
        return false;
178
    }
179
180
    /**
181
     * Mark action as already done.
182
     *
183
     * @param string $action action id
184
     * @param int $time microtime when action was done, false for action was not done
185
     */
186
    public function markDone($action, $time = null)
187
    {
188
        $this->_done[$action] = ($time === null || $time === true) ? microtime(1) : $time;
189
    }
190
191
    /**
192
     * Runs given binary with given arguments. Returns exit code.
193
     * @param string $name
194
     * @param string $args
195
     * @return int exit code
196
     */
197
    public function passthru($name, $args = '')
198
    {
199
        return $this->takeGoal('binaries')->passthru($name, $args);
200
    }
201
202
    /**
203
     * Runs given binary with given arguments. Returns stdout array.
204
     * @param string $name
205
     * @param string $args
206
     * @return array stdout
207
     */
208
    public function exec($name, $args = '')
209
    {
210
        return $this->takeGoal('binaries')->exec($name, $args);
211
    }
212
213
    public function takeGoal($id)
214
    {
215
        return Yii::$app->get('config')->getGoal($id);
216
    }
217
218
    public function takeConfig()
219
    {
220
        return Yii::$app->get('config');
221
    }
222
223
    public function takeVendor()
224
    {
225
        return $this->takeGoal('vendor');
226
    }
227
228
    public function takePackage()
229
    {
230
        return $this->takeGoal('package');
231
    }
232
233
    public function takeVcs()
234
    {
235
        return $this->takeConfig()->getVcs();
236
    }
237
}
238