|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Application; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use WebHemi\Environment\ServiceInterface as EnvironmentManager; |
|
18
|
|
|
use WebHemi\Session\ServiceInterface as SessionManager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Progress |
|
22
|
|
|
*/ |
|
23
|
|
|
class Progress |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var EnvironmentManager */ |
|
26
|
|
|
private $environmentManager; |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $sessionId; |
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
private $callerName; |
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $progressId; |
|
33
|
|
|
/** @var int */ |
|
34
|
|
|
private $totalSteps; |
|
35
|
|
|
/** @var int */ |
|
36
|
|
|
private $currentStep; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Progress constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param EnvironmentManager $environmentManager |
|
42
|
|
|
* @param SessionManager $sessionManager |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(EnvironmentManager $environmentManager, SessionManager $sessionManager) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->environmentManager = $environmentManager; |
|
47
|
|
|
$this->sessionId = $sessionManager->getSessionId(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Starts the progress. |
|
52
|
|
|
* |
|
53
|
|
|
* @param int $totalSteps |
|
54
|
|
|
* @param string $callerName |
|
55
|
|
|
*/ |
|
56
|
|
|
public function start(int $totalSteps, string $callerName = null) : void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->callerName = $callerName ?? $this->getCallerName(); |
|
59
|
|
|
$this->totalSteps = $totalSteps; |
|
60
|
|
|
$this->currentStep = 0; |
|
61
|
|
|
$this->progressId = md5($this->sessionId).'_'.$this->callerName; |
|
62
|
|
|
|
|
63
|
|
|
$this->next(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Gets the Class name where the progress was called from. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
private function getCallerName() : string |
|
72
|
|
|
{ |
|
73
|
|
|
if (!isset($this->callerName)) { |
|
74
|
|
|
try { |
|
75
|
|
|
throw new Exception('Cet Trace'); |
|
76
|
|
|
} catch (Exception $exception) { |
|
77
|
|
|
$trace = $exception->getTrace()[1]['file'] ?? rand(0, 10000); |
|
78
|
|
|
$trace = explode('/', $trace); |
|
79
|
|
|
$this->callerName = str_replace('.php', '', array_pop($trace)); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->callerName; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Increments the progress and writes to file. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function next() : void |
|
90
|
|
|
{ |
|
91
|
|
|
$handler = fopen( |
|
92
|
|
|
$this->environmentManager->getApplicationRoot().'/data/progress/'.$this->progressId.'.json', |
|
93
|
|
|
'w' |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
if ($handler) { |
|
97
|
|
|
$data = [ |
|
98
|
|
|
'total' => $this->totalSteps, |
|
99
|
|
|
'current' => $this->currentStep |
|
100
|
|
|
]; |
|
101
|
|
|
fwrite($handler, json_encode($data)); |
|
102
|
|
|
fclose($handler); |
|
103
|
|
|
|
|
104
|
|
|
$this->currentStep++; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the progress identifier. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getProgressId() : string |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->progressId; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|