2024.8.23
Loading...
Searching...
No Matches
class.action.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 Action handles the execution of various actions based on command line arguments.
12 */
13class Action
14{
15 // Constants for different actions
16 const ABOUT = 'about';
17 const ADD_ALIAS = 'addAlias';
18 const ADD_VHOST = 'addVhost';
19 const CHANGE_BROWSER = 'changeBrowser';
20 const CHANGE_DB_ROOT_PWD = 'changeDbRootPwd';
21 const CHANGE_PORT = 'changePort';
22 const CHECK_PORT = 'checkPort';
23 const CHECK_VERSION = 'checkVersion';
24 const CLEAR_FOLDERS = 'clearFolders';
25 const DEBUG_APACHE = 'debugApache';
26 const DEBUG_MARIADB = 'debugMariadb';
27 const DEBUG_MYSQL = 'debugMysql';
28 const DEBUG_POSTGRESQL = 'debugPostgresql';
29 const EDIT_ALIAS = 'editAlias';
30 const EDIT_VHOST = 'editVhost';
31 const ENABLE = 'enable';
32 const EXEC = 'exec';
33 const GEN_SSL_CERTIFICATE = 'genSslCertificate';
34 const LAUNCH_STARTUP = 'launchStartup';
35 const MANUAL_RESTART = 'manualRestart';
36 const LOADING = 'loading';
37 const QUIT = 'quit';
38 const REBUILD_INI = 'rebuildIni';
39 const REFRESH_REPOS = 'refreshRepos';
40 const REFRESH_REPOS_STARTUP = 'refreshReposStartup';
41 const RELOAD = 'reload';
42 const RESTART = 'restart';
43 const SERVICE = 'service';
44 const STARTUP = 'startup';
45 const SWITCH_APACHE_MODULE = 'switchApacheModule';
46 const SWITCH_LANG = 'switchLang';
47 const SWITCH_LOGS_VERBOSE = 'switchLogsVerbose';
48 const SWITCH_PHP_EXTENSION = 'switchPhpExtension';
49 const SWITCH_PHP_PARAM = 'switchPhpParam';
50 const SWITCH_ONLINE = 'switchOnline';
51 const SWITCH_VERSION = 'switchVersion';
52
53 const EXT = 'ext';
54
55 /**
56 * @var mixed Holds the current action instance.
57 */
58 private $current;
59
60 /**
61 * Constructor for the Action class.
62 * Initializes the Action object.
63 */
64 public function __construct()
65 {
66 // Initialization code can be added here if needed
67 }
68
69 /**
70 * Processes the action based on command line arguments.
71 *
72 * This method checks if an action exists in the command line arguments,
73 * cleans the argument, constructs the action class name, and then
74 * initializes the action class with the provided arguments.
75 *
76 * @return void
77 */
78 public function process()
79 {
80 if ($this->exists()) {
81 $action = Util::cleanArgv(1);
82 $actionClass = 'Action' . ucfirst($action);
83
84 $args = array();
85 foreach ($_SERVER['argv'] as $key => $arg) {
86 if ($key > 1) {
87 $args[] = $action == self::EXT ? $arg : base64_decode($arg);
88 }
89 }
90
91 $this->current = null;
92 if (class_exists($actionClass)) {
93 Util::logDebug('Start ' . $actionClass);
94 $this->current = new $actionClass($args);
95 }
96 }
97 }
98
99 /**
100 * Calls a specific action by name with optional arguments.
101 *
102 * This method constructs the action class name from the provided action name,
103 * checks if the class exists, and then initializes the action class with the
104 * provided arguments.
105 *
106 * @param string $actionName The name of the action to call.
107 * @param mixed $actionArgs Optional arguments for the action.
108 * @return void
109 */
110 public function call($actionName, $actionArgs = null)
111 {
112 $actionClass = 'Action' . ucfirst($actionName);
113 if (class_exists($actionClass)) {
114 Util::logDebug('Start ' . $actionClass);
115 new $actionClass($actionArgs);
116 }
117 }
118
119 /**
120 * Checks if the action exists in the command line arguments.
121 *
122 * This method verifies if the command line arguments contain an action
123 * by checking the presence and non-emptiness of the second argument.
124 *
125 * @return bool Returns true if the action exists, false otherwise.
126 */
127 public function exists()
128 {
129 return isset($_SERVER['argv'])
130 && isset($_SERVER['argv'][1])
131 && !empty($_SERVER['argv'][1]);
132 }
133}
const SWITCH_LANG
const CLEAR_FOLDERS
const DEBUG_MARIADB
const REFRESH_REPOS_STARTUP
const CHANGE_PORT
const LOADING
const ADD_VHOST
const STARTUP
const ABOUT
const DEBUG_MYSQL
const SWITCH_PHP_PARAM
const EDIT_VHOST
const CHECK_PORT
const ADD_ALIAS
const CHANGE_BROWSER
const QUIT
const ENABLE
const EXEC
call($actionName, $actionArgs=null)
const DEBUG_POSTGRESQL
const MANUAL_RESTART
const SWITCH_VERSION
const SWITCH_PHP_EXTENSION
const EDIT_ALIAS
const RESTART
const GEN_SSL_CERTIFICATE
const DEBUG_APACHE
const SERVICE
const CHANGE_DB_ROOT_PWD
const RELOAD
const REBUILD_INI
const SWITCH_ONLINE
const LAUNCH_STARTUP
const SWITCH_LOGS_VERBOSE
const CHECK_VERSION
const SWITCH_APACHE_MODULE
const REFRESH_REPOS
const EXT
static logDebug($data, $file=null)
static cleanArgv($name, $type='text')