2024.8.23
Loading...
Searching...
No Matches
class.action.ext.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
10/**
11 * Class ActionExt handles the execution of various extended actions.
12 */
14{
15 // Constants for different actions
16 const START = 'start';
17 const STOP = 'stop';
18 const RELOAD = 'reload';
19 const REFRESH = 'refresh';
20
21 // Constants for status codes
22 const STATUS_ERROR = 2;
23 const STATUS_WARNING = 1;
24 const STATUS_SUCCESS = 0;
25
26 /**
27 * @var int Holds the current status of the action.
28 */
30
31 /**
32 * @var string Holds the logs generated during the action execution.
33 */
34 private $logs = '';
35
36 /**
37 * Constructor for the ActionExt class.
38 *
39 * @param array $args The command line arguments passed to the action.
40 */
41 public function __construct($args)
42 {
43 if (!isset($args[0]) || empty($args[0])) {
44 $this->addLog('No args defined');
45 $this->addLog('Available args:');
46 foreach ($this->getProcs() as $proc) {
47 $this->addLog('- ' . $proc);
48 }
49 $this->setStatus(self::STATUS_ERROR);
50 $this->sendLogs();
51 return;
52 }
53
54 $action = $args[0];
55
56 $newArgs = array();
57 foreach ($args as $key => $arg) {
58 if ($key > 0) {
59 $newArgs[] = $arg;
60 }
61 }
62
63 $method = 'proc' . ucfirst($action);
64 if (!method_exists($this, $method)) {
65 $this->addLog('Unknown arg: ' . $action);
66 $this->addLog('Available args:');
67 foreach ($this->getProcs() as $procName => $procDesc) {
68 $this->addLog('- ' . $procName . ': ' . $procDesc);
69 }
70 $this->setStatus(self::STATUS_ERROR);
71 $this->sendLogs();
72 return;
73 }
74
75 call_user_func(array($this, $method), $newArgs);
76 $this->sendLogs();
77 }
78
79 /**
80 * Retrieves the list of available actions.
81 *
82 * @return array The list of available actions.
83 */
84 private function getProcs()
85 {
86 return array(
87 self::START,
88 self::STOP,
89 self::RELOAD,
90 self::REFRESH
91 );
92 }
93
94 /**
95 * Adds a log entry to the logs.
96 *
97 * @param string $data The log entry to add.
98 */
99 private function addLog($data)
100 {
101 $this->logs .= $data . "\n";
102 }
103
104 /**
105 * Sets the status of the action.
106 *
107 * @param int $status The status code to set.
108 */
109 private function setStatus($status)
110 {
111 $this->status = $status;
112 }
113
114 /**
115 * Sends the logs as a JSON-encoded response.
116 */
117 private function sendLogs()
118 {
119 echo json_encode(array(
120 'status' => $this->status,
121 'response' => $this->logs
122 ));
123 }
124
125 /**
126 * Starts the application.
127 *
128 * @param array $args The command line arguments passed to the action.
129 */
130 private function procStart($args)
131 {
132 global $bearsamppRoot, $bearsamppWinbinder;
133
134 if (!Util::isLaunched()) {
135 $this->addLog('Starting ' . APP_TITLE);
136 $bearsamppWinbinder->exec($bearsamppRoot->getExeFilePath(), null, false);
137 } else {
138 $this->addLog(APP_TITLE . ' already started');
139 $this->setStatus(self::STATUS_WARNING);
140 }
141 }
142
143 /**
144 * Stops the application and removes services.
145 *
146 * @param array $args The command line arguments passed to the action.
147 */
148 private function procStop($args)
149 {
150 global $bearsamppBins;
151
152 if (Util::isLaunched()) {
153 $this->addLog('Remove services');
154 foreach ($bearsamppBins->getServices() as $sName => $service) {
155 if ($service->delete()) {
156 $this->addLog('- ' . $sName . ': OK');
157 } else {
158 $this->addLog('- ' . $sName . ': KO');
159 $this->setStatus(self::STATUS_ERROR);
160 }
161 }
162
163 $this->addLog('Stop ' . APP_TITLE);
164 Batch::exitAppStandalone();
165 } else {
166 $this->addLog(APP_TITLE . ' already stopped');
167 $this->setStatus(self::STATUS_WARNING);
168 }
169 }
170
171 /**
172 * Reloads the application by stopping and starting services.
173 *
174 * @param array $args The command line arguments passed to the action.
175 */
176 private function procReload($args)
177 {
178 global $bearsamppRoot, $bearsamppBins, $bearsamppWinbinder;
179
180 if (!Util::isLaunched()) {
181 $this->addLog(APP_TITLE . ' is not started.');
182 $bearsamppWinbinder->exec($bearsamppRoot->getExeFilePath(), null, false);
183 $this->addLog('Start ' . APP_TITLE);
184 $this->setStatus(self::STATUS_WARNING);
185 return;
186 }
187
188 $this->addLog('Remove services');
189 foreach ($bearsamppBins->getServices() as $sName => $service) {
190 if ($service->delete()) {
191 $this->addLog('- ' . $sName . ': OK');
192 } else {
193 $this->addLog('- ' . $sName . ': KO');
194 $this->setStatus(self::STATUS_ERROR);
195 }
196 }
197
199
200 $this->addLog('Start services');
201 foreach ($bearsamppBins->getServices() as $sName => $service) {
202 $service->create();
203 if ($service->start()) {
204 $this->addLog('- ' . $sName . ': OK');
205 } else {
206 $this->addLog('- ' . $sName . ': KO');
207 $this->setStatus(self::STATUS_ERROR);
208 }
209 }
210 }
211
212 /**
213 * Refreshes the application by calling the reload action.
214 *
215 * @param array $args The command line arguments passed to the action.
216 */
217 private function procRefresh($args)
218 {
219 global $bearsamppAction;
220
221 if (!Util::isLaunched()) {
222 $this->addLog(APP_TITLE . ' is not started.');
223 $this->setStatus(self::STATUS_ERROR);
224 return;
225 }
226
228 }
229}
global $bearsamppBins
global $bearsamppRoot
$proc
Definition ajax.php:43
setStatus($status)
const RELOAD
static killBins($refreshProcs=false)
$bearsamppAction
Definition root.php:32
const APP_TITLE
Definition root.php:12