2024.8.23
Loading...
Searching...
No Matches
class.tpl.app.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 TplApp
12 *
13 * This class provides various methods to generate and manage menu items, actions, and sections
14 * within the Bearsampp application. It includes functionalities for creating and processing
15 * different sections, running actions, and generating menus with various options.
16 */
17class TplApp
18{
19 // Constants for item and section identifiers
20 const ITEM_CAPTION = 0;
21 const ITEM_GLYPH = 1;
22
23 const SECTION_CALL = 0;
24 const SECTION_CONTENT = 1;
25
26 /**
27 * Private constructor to prevent instantiation.
28 */
29 private function __construct()
30 {
31 }
32
33 /**
34 * Processes and generates the main sections of the application.
35 *
36 * This method generates the main sections of the application, including configuration,
37 * services, messages, startup actions, and menu settings.
38 *
39 * @global object $bearsamppCore Provides access to core functionalities and configurations.
40 *
41 * @return string The generated sections as a concatenated string.
42 */
43 public static function process()
44 {
45 global $bearsamppCore;
46
47 return TplAestan::getSectionConfig() . PHP_EOL .
48 self::getSectionServices() . PHP_EOL .
52 TplAestan::getSectionMenuLeftSettings(APP_TITLE . ' ' . $bearsamppCore->getAppVersion()) . PHP_EOL .
53 self::getSectionMenuRight() . PHP_EOL .
54 self::getSectionMenuLeft() . PHP_EOL;
55 }
56
57 /**
58 * Processes and generates a lighter version of the main sections.
59 *
60 * This method generates a lighter version of the main sections, excluding some menu settings.
61 *
62 * @return string The generated sections as a concatenated string.
63 */
64 public static function processLight()
65 {
66 return TplAestan::getSectionConfig() . PHP_EOL .
67 self::getSectionServices() . PHP_EOL .
70 }
71
72 /**
73 * Generates a section name based on the provided name and arguments.
74 *
75 * @param string $name The base name of the section.
76 * @param array $args Optional arguments to include in the section name.
77 *
78 * @return string The generated section name.
79 */
80 public static function getSectionName($name, $args = array())
81 {
82 return ucfirst($name) . (!empty($args) ? '-' . md5(serialize($args)) : '');
83 }
84
85 /**
86 * Generates the content of a section based on the provided name, class, and arguments.
87 *
88 * @param string $name The base name of the section.
89 * @param string $class The class name containing the method to generate the section content.
90 * @param array $args Optional arguments to pass to the method.
91 *
92 * @return string The generated section content.
93 */
94 public static function getSectionContent($name, $class, $args = array())
95 {
96 $baseMethod = 'get' . ucfirst($name);
97 $args = $args == null ? array() : $args;
98 return '[' . self::getSectionName($name, $args) . ']' . PHP_EOL .
99 call_user_func_array($class . '::' . $baseMethod, $args);
100 }
101
102 /**
103 * Generates an action string to run a specific action.
104 *
105 * @param string $action The action to run.
106 * @param array $args Optional arguments for the action.
107 * @param array $item Optional item details for the action.
108 * @param bool $waitUntilTerminated Whether to wait until the action is terminated.
109 *
110 * @global object $bearsamppRoot Provides access to the root directory of the application.
111 * @global object $bearsamppCore Provides access to core functionalities and configurations.
112 *
113 * @return string The generated action string.
114 */
115 public static function getActionRun($action, $args = array(), $item = array(), $waitUntilTerminated = true)
116 {
118 $args = $args == null ? array() : $args;
119
120 $argImp = '';
121 foreach ($args as $arg) {
122 $argImp .= ' ' . base64_encode($arg);
123 }
124
125 $result = 'Action: run; ' .
126 'FileName: "' . $bearsamppCore->getPhpExe(true) . '"; ' .
127 'Parameters: "' . Core::isRoot_FILE . ' ' . $action . $argImp . '"; ' .
128 'WorkingDir: "' . $bearsamppRoot->getCorePath(true) . '"';
129
130 if (!empty($item)) {
131 $result = 'Type: item; ' . $result .
132 '; Caption: "' . $item[self::ITEM_CAPTION] . '"' .
133 (!empty($item[self::ITEM_GLYPH]) ? '; Glyph: "' . $item[self::ITEM_GLYPH] . '"' : '');
134 } elseif ($waitUntilTerminated) {
135 $result .= '; Flags: waituntilterminated';
136 }
137
138 return $result;
139 }
140
141 /**
142 * Generates a multi-action string for a specific action.
143 *
144 * @param string $action The action to run.
145 * @param array $args Optional arguments for the action.
146 * @param array $item Optional item details for the action.
147 * @param bool $disabled Whether the action is disabled.
148 * @param string $class The class name containing the method to generate the section content.
149 *
150 * @return array An array containing the call string and the section content.
151 */
152 public static function getActionMulti($action, $args = array(), $item = array(), $disabled = false, $class = false)
153 {
154 $action = 'action' . ucfirst($action);
155 $args = $args == null ? array() : $args;
156 $sectionName = self::getSectionName($action, $args);
157
158 $call = 'Action: multi; Actions: ' . $sectionName;
159
160 if (!empty($item)) {
161 $call = 'Type: item; ' . $call .
162 '; Caption: "' . $item[self::ITEM_CAPTION] . '"' .
163 (!empty($item[self::ITEM_GLYPH]) ? '; Glyph: "' . $item[self::ITEM_GLYPH] . '"' : '');
164 } else {
165 $call .= '; Flags: waituntilterminated';
166 }
167
168 return array($call, self::getSectionContent($action, $class, $args));
169 }
170
171 /**
172 * Generates an action string to execute a specific action.
173 *
174 * @return string The generated action string.
175 */
176 public static function getActionExec()
177 {
178 return self::getActionRun(Action::EXEC, array(), array(), false);
179 }
180
181 /**
182 * Generates a menu with the specified caption, menu name, and class.
183 *
184 * @param string $caption The caption for the menu.
185 * @param string $menu The name of the menu.
186 * @param string $class The class name containing the method to generate the menu content.
187 *
188 * @return array An array containing the call string and the menu content.
189 */
190 public static function getMenu($caption, $menu, $class)
191 {
192 $menu = 'menu' . ucfirst($menu);
193
194 $call = 'Type: submenu; ' .
195 'Caption: "' . $caption . '"; ' .
196 'SubMenu: ' . self::getSectionName($menu) . '; ' .
198
199 return array($call, self::getSectionContent($menu, $class, null));
200 }
201
202 /**
203 * Generates a menu with the specified caption, menu name, class, and enabled state.
204 *
205 * @param string $caption The caption for the menu.
206 * @param string $menu The name of the menu.
207 * @param string $class The class name containing the method to generate the menu content.
208 * @param bool $enabled Whether the menu is enabled.
209 *
210 * @return array An array containing the call string and the menu content.
211 */
212 public static function getMenuEnable($caption, $menu, $class, $enabled = true)
213 {
214 $menu = 'menu' . ucfirst($menu);
215
216 $call = 'Type: submenu; ' .
217 'Caption: "' . $caption . '"; ' .
218 'SubMenu: ' . self::getSectionName($menu) . '; ' .
220
221 return array($call, self::getSectionContent($menu, $class, null));
222 }
223
224 /**
225 * Generates the services section.
226 *
227 * @global object $bearsamppBins Provides access to system binaries and their configurations.
228 *
229 * @return string The generated services section.
230 */
231 private static function getSectionServices()
232 {
233 global $bearsamppBins;
234
235 $result = '[Services]' . PHP_EOL;
236 foreach ($bearsamppBins->getServices() as $service) {
237 $result .= 'Name: ' . $service->getName() . PHP_EOL;
238 }
239
240 return $result;
241 }
242
243 /**
244 * Generates the startup action section.
245 *
246 * @return string The generated startup action section.
247 */
248 private static function getSectionStartupAction()
249 {
250 return '[StartupAction]' . PHP_EOL .
254 self::getActionExec() . PHP_EOL;
255 }
256
257 /**
258 * Generates the right menu section.
259 *
260 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
261 *
262 * @return string The generated right menu section.
263 */
264 private static function getSectionMenuRight()
265 {
266 global $bearsamppLang;
267
268 $tplReload = TplAppReload::process();
269 $tplBrowser = TplAppBrowser::process();
270 $tplLang = TplAppLang::process();
271 $tplLogsVerbose = TplAppLogsVerbose::process();
272 $tplLaunchStartup = TplAppLaunchStartup::process();
273 $tplExit = TplAppExit::process();
274
275 return
276 // Items
277 '[Menu.Right]' . PHP_EOL .
283 ) . PHP_EOL .
285
286 TplAestan::getItemSeparator() . PHP_EOL .
287 $tplReload[self::SECTION_CALL] . PHP_EOL .
288 TplAppClearFolders::process() . PHP_EOL .
289 TplAppRebuildIni::process() . PHP_EOL .
290 $tplBrowser[self::SECTION_CALL] . PHP_EOL .
291 TplAppEditConf::process() . PHP_EOL .
292
293 TplAestan::getItemSeparator() . PHP_EOL .
294 $tplLang[self::SECTION_CALL] . PHP_EOL .
295 $tplLogsVerbose[self::SECTION_CALL] . PHP_EOL .
296 $tplLaunchStartup[self::SECTION_CALL] . PHP_EOL .
297
298 TplAestan::getItemSeparator() . PHP_EOL .
299 $tplExit[self::SECTION_CALL] . PHP_EOL .
300
301 // Actions
302 PHP_EOL . $tplReload[self::SECTION_CONTENT] . PHP_EOL .
303 PHP_EOL . $tplBrowser[self::SECTION_CONTENT] . PHP_EOL .
304 PHP_EOL . $tplLang[self::SECTION_CONTENT] .
305 PHP_EOL . $tplLogsVerbose[self::SECTION_CONTENT] .
306 PHP_EOL . $tplLaunchStartup[self::SECTION_CONTENT] .
307 PHP_EOL . $tplExit[self::SECTION_CONTENT] . PHP_EOL;
308 }
309
310 /**
311 * Generates the left menu section.
312 *
313 * @global object $bearsamppRoot Provides access to the root directory of the application.
314 * @global object $bearsamppBins Provides access to system binaries and their configurations.
315 * @global object $bearsamppLang Provides language support for retrieving language-specific values.
316 *
317 * @return string The generated left menu section.
318 */
319 private static function getSectionMenuLeft()
320 {
322
323 $tplNodejs = TplAppNodejs::process();
324 $tplApache = TplAppApache::process();
325 $tplPhp = TplAppPhp::process();
326 $tplMysql = TplAppMysql::process();
327 $tplMariadb = TplAppMariadb::process();
328 $tplPostgresql = TplAppPostgresql::process();
329 $tplMailhog = TplAppMailhog::process();
330 $tplMailpit = TplAppMailpit::process();
331 $tplMemcached = TplAppMemcached::process();
332 $tplFilezilla = TplAppFilezilla::process();
333 $tplXlight = TplAppXlight::process();
334
335 $tplLogs = TplAppLogs::process();
336 $tplApps = TplAppApps::process();
337 $tplTools = TplAppTools::process();
338
339 $tplServices = TplAppServices::process();
340
341 $tplOnline = TplAppOnline::process();
342
343 $httpUrl = 'http://localhost' . ($bearsamppBins->getApache()->getPort() != 80 ? ':' . $bearsamppBins->getApache()->getPort() : '');
344 $httpsUrl = 'https://localhost' . ($bearsamppBins->getApache()->getSslPort() != 443 ? ':' . $bearsamppBins->getApache()->getSslPort() : '');
345
346 return
347 // Items
348 '[Menu.Left]' . PHP_EOL .
349 TplAestan::getItemLink($bearsamppLang->getValue(Lang::MENU_LOCALHOST), $httpUrl) . PHP_EOL .
350 TplAestan::getItemLink($bearsamppLang->getValue(Lang::MENU_LOCALHOST) . ' (SSL)', $httpsUrl) . PHP_EOL .
352
353 //// Bins menus
354 TplAestan::getItemSeparator() . PHP_EOL .
355 $tplNodejs[self::SECTION_CALL] . PHP_EOL .
356 $tplApache[self::SECTION_CALL] . PHP_EOL .
357 $tplPhp[self::SECTION_CALL] . PHP_EOL .
358 $tplMysql[self::SECTION_CALL] . PHP_EOL .
359 $tplMariadb[self::SECTION_CALL] . PHP_EOL .
360 $tplPostgresql[self::SECTION_CALL] . PHP_EOL .
361 $tplMailhog[self::SECTION_CALL] . PHP_EOL .
362 $tplMailpit[self::SECTION_CALL] . PHP_EOL .
363 $tplMemcached[self::SECTION_CALL] . PHP_EOL .
364 $tplXlight[self::SECTION_CALL] . PHP_EOL .
365 $tplFilezilla[self::SECTION_CALL] . PHP_EOL .
366
367 //// Stuff menus
368 TplAestan::getItemSeparator() . PHP_EOL .
369 $tplLogs[self::SECTION_CALL] . PHP_EOL .
370 $tplTools[self::SECTION_CALL] . PHP_EOL .
371 $tplApps[self::SECTION_CALL] . PHP_EOL .
372
373 //// Services
374 TplAestan::getItemSeparator() . PHP_EOL .
375 $tplServices[self::SECTION_CALL] .
376
377 //// Put online/offline
378 TplAestan::getItemSeparator() . PHP_EOL .
379 $tplOnline[self::SECTION_CALL] . PHP_EOL .
380
381 // Actions
382 PHP_EOL . $tplNodejs[self::SECTION_CONTENT] .
383 PHP_EOL . $tplApache[self::SECTION_CONTENT] .
384 PHP_EOL . $tplPhp[self::SECTION_CONTENT] .
385 PHP_EOL . $tplMysql[self::SECTION_CONTENT] .
386 PHP_EOL . $tplMariadb[self::SECTION_CONTENT] .
387 PHP_EOL . $tplPostgresql[self::SECTION_CONTENT] .
388 PHP_EOL . $tplMailhog[self::SECTION_CONTENT] .
389 PHP_EOL . $tplMailpit[self::SECTION_CONTENT] .
390 PHP_EOL . $tplMemcached[self::SECTION_CONTENT] .
391 PHP_EOL . $tplFilezilla[self::SECTION_CONTENT] .
392 PHP_EOL . $tplXlight[self::SECTION_CONTENT] .
393 PHP_EOL . $tplLogs[self::SECTION_CONTENT] .
394 PHP_EOL . $tplTools[self::SECTION_CONTENT] .
395 PHP_EOL . $tplApps[self::SECTION_CONTENT] .
396 PHP_EOL . $tplServices[self::SECTION_CONTENT] .
397 PHP_EOL . $tplOnline[self::SECTION_CONTENT];
398 }
399}
$result
global $bearsamppBins
global $bearsamppLang
global $bearsamppRoot
global $bearsamppCore
const STARTUP
const ABOUT
const EXEC
const CHECK_VERSION
const isRoot_FILE
const HELP
const MENU_LOCALHOST
const MENU_WWW_DIRECTORY
const MENU_CHECK_UPDATE
const MENU_ABOUT
const GLYPH_FOLDER_DISABLED
static getSectionMessages()
const GLYPH_FOLDER_CLOSE
static getItemSeparator()
static getSectionMenuLeftSettings($caption)
static getItemLink($caption, $link, $local=false, $glyph=self::GLYPH_WEB_PAGE)
static getSectionConfig()
static getItemExplore($caption, $path)
static getSectionMenuRightSettings()
static getActionMulti($action, $args=array(), $item=array(), $disabled=false, $class=false)
static getActionExec()
static getSectionContent($name, $class, $args=array())
static getSectionServices()
const ITEM_CAPTION
static process()
const SECTION_CALL
static getMenuEnable($caption, $menu, $class, $enabled=true)
static getSectionMenuLeft()
static getSectionName($name, $args=array())
static getSectionMenuRight()
const SECTION_CONTENT
static getActionRun($action, $args=array(), $item=array(), $waitUntilTerminated=true)
static getSectionStartupAction()
static getMenu($caption, $menu, $class)
const ITEM_GLYPH
static processLight()
static getWebsiteUrl($path='', $fragment='', $utmSource=true)
const APP_TITLE
Definition root.php:12