Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.tool.git.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2026 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
16class ToolGit extends Module
17{
21 const ROOT_CFG_VERSION = 'gitVersion';
22
26 const LOCAL_CFG_EXE = 'gitExe';
30 const LOCAL_CFG_BASH = 'gitBash';
34 const LOCAL_CFG_SCAN_STARTUP = 'gitScanStartup';
35
39 const REPOS_FILE = 'repos.dat';
43 const REPOS_CACHE_FILE = 'reposCache.dat';
44
48 private $reposFile;
56 private $repos;
57
61 private $exe;
65 private $bash;
69 private $scanStartup;
70
77 public function __construct($id, $type)
78 {
79 Log::initClass($this);
80 $this->reload($id, $type);
81 }
82
89 public function reload($id = null, $type = null)
90 {
92 Log::reloadClass($this);
93
94 $this->name = $bearsamppLang->getValue(Lang::GIT);
95 $this->version = $bearsamppConfig->getRaw(self::ROOT_CFG_VERSION);
96 parent::reload($id, $type);
97
98 $this->reposFile = $this->symlinkPath . '/' . self::REPOS_FILE;
99 $this->reposCacheFile = $this->symlinkPath . '/' . self::REPOS_CACHE_FILE;
100
101 if ($this->bearsamppConfRaw !== false) {
102 $this->exe = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
103 $this->bash = $this->symlinkPath . '/' . $this->bearsamppConfRaw[self::LOCAL_CFG_BASH];
104 $this->scanStartup = $this->bearsamppConfRaw[self::LOCAL_CFG_SCAN_STARTUP];
105 }
106
107 if (!$this->enable) {
108 Log::info($this->name . ' is not enabled!');
109
110 return;
111 }
112 if (!is_dir($this->currentPath)) {
113 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->currentPath));
114 }
115 if (!is_dir($this->symlinkPath)) {
116 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_FILE_NOT_FOUND), $this->name . ' ' . $this->version, $this->symlinkPath));
117
118 return;
119 }
120 if (!is_file($this->bearsamppConf)) {
121 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->bearsamppConf));
122 }
123 if (!is_file($this->reposFile)) {
124 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_CONF_NOT_FOUND), $this->name . ' ' . $this->version, $this->reposFile));
125 }
126 if (!is_file($this->exe)) {
127 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->exe));
128 }
129 if (!is_file($this->bash)) {
130 Log::error(sprintf($bearsamppLang->getValue(Lang::ERROR_EXE_NOT_FOUND), $this->name . ' ' . $this->version, $this->bash));
131 }
132
133 if (is_file($this->reposFile)) {
134 $this->repos = explode(PHP_EOL, file_get_contents($this->reposFile));
135 $rebuildRepos = array();
136 foreach ($this->repos as $repo) {
137 $repo = trim($repo);
138 if (stripos($repo, ':') === false) {
139 $repo = Path::getRootPath() . '/' . $repo;
140 }
141 if (is_dir($repo)) {
142 $rebuildRepos[] = Path::formatUnixPath($repo);
143 } else {
144 Log::warning($this->name . ' repository not found: ' . $repo);
145 }
146 }
147 $this->repos = $rebuildRepos;
148 }
149 }
150
160 protected function updateConfig($version = null, $sub = 0, $showWindow = false)
161 {
162 global $bearsamppWinbinder;
163
164 if (!$this->enable) {
165 return true;
166 }
167
168 $version = $version == null ? $this->version : $version;
169 Log::debug(($sub > 0 ? str_repeat(' ', 2 * $sub) : '') . 'Update ' . $this->name . ' ' . $version . ' config');
170
171 if (file_exists(Path::getModuleSymlinkPath($this) . '/post-install.bat')) {
172 $bearsamppWinbinder->exec($this->getBash(), '--no-needs-console --hide --no-cd --command="' . Path::getModuleSymlinkPath($this) . '/post-install.bat"', true);
173 }
174
175 $bearsamppWinbinder->exec($this->getExe(), 'config --global core.autocrlf false', true);
176 $bearsamppWinbinder->exec($this->getExe(), 'config --global core.eol lf', true);
177
178 return true;
179 }
180
188 public function findRepos($cache = true)
189 {
190 $result = array();
191
192 if ($cache) {
193 if (file_exists($this->reposCacheFile)) {
194 $repos = file($this->reposCacheFile);
195 foreach ($repos as $repo) {
196 array_push($result, trim($repo));
197 }
198 }
199 } else {
200 if (!empty($this->repos)) {
201 foreach ($this->repos as $repo) {
202 $foundRepos = Util::findRepos($repo, $repo, '.git/config');
203 if (!empty($foundRepos)) {
204 foreach ($foundRepos as $foundRepo) {
205 array_push($result, $foundRepo);
206 }
207 }
208 }
209 }
210 $strResult = implode(PHP_EOL, $result);
211 file_put_contents($this->reposCacheFile, $strResult);
212 }
213
214 return $result;
215 }
216
222 public function setVersion($version)
223 {
224 global $bearsamppConfig;
225 $this->version = $version;
226 $bearsamppConfig->replace(self::ROOT_CFG_VERSION, $version);
227 $this->reload();
228 }
229
235 public function getRepos()
236 {
237 return $this->repos;
238 }
239
245 public function getExe()
246 {
247 return $this->exe;
248 }
249
255 public function getBash()
256 {
257 return $this->bash;
258 }
259
265 public function isScanStartup()
266 {
267 return $this->scanStartup == Config::ENABLED;
268 }
269
276 {
277 $this->scanStartup = intval($scanStartup) === Config::ENABLED ? Config::ENABLED : Config::DISABLED;
278 Util::replaceInFile($this->bearsamppConf, array(
279 '/^' . self::LOCAL_CFG_SCAN_STARTUP . '/' => self::LOCAL_CFG_SCAN_STARTUP . ' = "' . $this->scanStartup . '"'
280 ));
281 }
282}
$result
global $bearsamppLang
global $bearsamppRoot
const DISABLED
const ENABLED
const ERROR_EXE_NOT_FOUND
const ERROR_CONF_NOT_FOUND
const GIT
const ERROR_FILE_NOT_FOUND
static info($data, $file=null)
static debug($data, $file=null)
static warning($data, $file=null)
static reloadClass($classInstance)
static error($data, $file=null)
static initClass($classInstance)
static getRootPath($aetrayPath=false)
static formatUnixPath($path)
static getModuleSymlinkPath($module)
const LOCAL_CFG_EXE
findRepos($cache=true)
updateConfig($version=null, $sub=0, $showWindow=false)
const REPOS_FILE
setVersion($version)
const REPOS_CACHE_FILE
setScanStartup($scanStartup)
reload($id=null, $type=null)
const LOCAL_CFG_SCAN_STARTUP
const ROOT_CFG_VERSION
__construct($id, $type)
const LOCAL_CFG_BASH
static findRepos($initPath, $startPath, $checkFile, $maxDepth=1)
static replaceInFile($path, $replaceList)
global $bearsamppConfig
Definition homepage.php:46