Bearsampp
2026.9.23
Toggle main menu visibility
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
16
class
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
;
52
private
$reposCacheFile
;
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
{
91
global
$bearsamppRoot
,
$bearsamppConfig
,
$bearsamppLang
;
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
275
public
function
setScanStartup
(
$scanStartup
)
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
$result
Definition
ajax.apache.php:19
$bearsamppLang
global $bearsamppLang
Definition
ajax.apache.php:16
$bearsamppRoot
global $bearsamppRoot
Definition
ajax.apache.php:16
Config\DISABLED
const DISABLED
Definition
class.config.php:36
Config\ENABLED
const ENABLED
Definition
class.config.php:35
Lang\ERROR_EXE_NOT_FOUND
const ERROR_EXE_NOT_FOUND
Definition
class.lang.php:155
Lang\ERROR_CONF_NOT_FOUND
const ERROR_CONF_NOT_FOUND
Definition
class.lang.php:154
Lang\GIT
const GIT
Definition
class.lang.php:147
Lang\ERROR_FILE_NOT_FOUND
const ERROR_FILE_NOT_FOUND
Definition
class.lang.php:156
Log\info
static info($data, $file=null)
Definition
class.log.php:737
Log\debug
static debug($data, $file=null)
Definition
class.log.php:726
Log\warning
static warning($data, $file=null)
Definition
class.log.php:748
Log\reloadClass
static reloadClass($classInstance)
Definition
class.log.php:805
Log\error
static error($data, $file=null)
Definition
class.log.php:760
Log\initClass
static initClass($classInstance)
Definition
class.log.php:784
Module
Definition
class.module.php:15
Module\$version
$version
Definition
class.module.php:29
Module\$type
$type
Definition
class.module.php:20
Module\$id
$id
Definition
class.module.php:23
Path\getRootPath
static getRootPath($aetrayPath=false)
Definition
class.path.php:324
Path\formatUnixPath
static formatUnixPath($path)
Definition
class.path.php:166
Path\getModuleSymlinkPath
static getModuleSymlinkPath($module)
Definition
class.path.php:57
ToolGit
Definition
class.tool.git.php:17
ToolGit\getExe
getExe()
Definition
class.tool.git.php:245
ToolGit\LOCAL_CFG_EXE
const LOCAL_CFG_EXE
Definition
class.tool.git.php:26
ToolGit\$scanStartup
$scanStartup
Definition
class.tool.git.php:69
ToolGit\findRepos
findRepos($cache=true)
Definition
class.tool.git.php:188
ToolGit\getRepos
getRepos()
Definition
class.tool.git.php:235
ToolGit\updateConfig
updateConfig($version=null, $sub=0, $showWindow=false)
Definition
class.tool.git.php:160
ToolGit\REPOS_FILE
const REPOS_FILE
Definition
class.tool.git.php:39
ToolGit\setVersion
setVersion($version)
Definition
class.tool.git.php:222
ToolGit\REPOS_CACHE_FILE
const REPOS_CACHE_FILE
Definition
class.tool.git.php:43
ToolGit\$repos
$repos
Definition
class.tool.git.php:56
ToolGit\isScanStartup
isScanStartup()
Definition
class.tool.git.php:265
ToolGit\setScanStartup
setScanStartup($scanStartup)
Definition
class.tool.git.php:275
ToolGit\reload
reload($id=null, $type=null)
Definition
class.tool.git.php:89
ToolGit\getBash
getBash()
Definition
class.tool.git.php:255
ToolGit\LOCAL_CFG_SCAN_STARTUP
const LOCAL_CFG_SCAN_STARTUP
Definition
class.tool.git.php:34
ToolGit\$bash
$bash
Definition
class.tool.git.php:65
ToolGit\ROOT_CFG_VERSION
const ROOT_CFG_VERSION
Definition
class.tool.git.php:21
ToolGit\__construct
__construct($id, $type)
Definition
class.tool.git.php:77
ToolGit\$exe
$exe
Definition
class.tool.git.php:61
ToolGit\$reposCacheFile
$reposCacheFile
Definition
class.tool.git.php:52
ToolGit\LOCAL_CFG_BASH
const LOCAL_CFG_BASH
Definition
class.tool.git.php:30
ToolGit\$reposFile
$reposFile
Definition
class.tool.git.php:48
Util\findRepos
static findRepos($initPath, $startPath, $checkFile, $maxDepth=1)
Definition
class.util.php:450
Util\replaceInFile
static replaceInFile($path, $replaceList)
Definition
class.util.php:300
$bearsamppConfig
global $bearsamppConfig
Definition
homepage.php:46
sandbox
core
classes
tools
class.tool.git.php
Generated by
1.17.0