Bearsampp
2026.7.11
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-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
16
class
ToolGit
extends
Module
17
{
18
const
ROOT_CFG_VERSION
=
'gitVersion'
;
19
20
const
LOCAL_CFG_EXE
=
'gitExe'
;
21
const
LOCAL_CFG_BASH
=
'gitBash'
;
22
const
LOCAL_CFG_SCAN_STARTUP
=
'gitScanStartup'
;
23
24
const
REPOS_FILE
=
'repos.dat'
;
25
const
REPOS_CACHE_FILE
=
'reposCache.dat'
;
26
27
private
$reposFile
;
28
private
$reposCacheFile
;
29
private
$repos
;
30
31
private
$exe
;
32
private
$bash
;
33
private
$scanStartup
;
34
41
public
function
__construct
(
$id
,
$type
) {
42
Log::initClass
($this);
43
$this->
reload
(
$id
,
$type
);
44
}
45
52
public
function
reload
(
$id
=
null
,
$type
=
null
) {
53
global
$bearsamppRoot
,
$bearsamppConfig
,
$bearsamppLang
;
54
Log::reloadClass
($this);
55
56
$this->name =
$bearsamppLang
->getValue(
Lang::GIT
);
57
$this->version =
$bearsamppConfig
->getRaw(self::ROOT_CFG_VERSION);
58
parent::reload(
$id
,
$type
);
59
60
$this->reposFile = $this->symlinkPath .
'/'
. self::REPOS_FILE;
61
$this->reposCacheFile = $this->symlinkPath .
'/'
. self::REPOS_CACHE_FILE;
62
63
if
($this->bearsamppConfRaw !==
false
) {
64
$this->exe = $this->symlinkPath .
'/'
. $this->bearsamppConfRaw[self::LOCAL_CFG_EXE];
65
$this->bash = $this->symlinkPath .
'/'
. $this->bearsamppConfRaw[self::LOCAL_CFG_BASH];
66
$this->scanStartup = $this->bearsamppConfRaw[self::LOCAL_CFG_SCAN_STARTUP];
67
}
68
69
if
(!$this->enable) {
70
Log::info
($this->name .
' is not enabled!'
);
71
return
;
72
}
73
if
(!is_dir($this->currentPath)) {
74
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_FILE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->currentPath));
75
}
76
if
(!is_dir($this->symlinkPath)) {
77
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_FILE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->symlinkPath));
78
return
;
79
}
80
if
(!is_file($this->bearsamppConf)) {
81
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_CONF_NOT_FOUND
), $this->name .
' '
. $this->version, $this->bearsamppConf));
82
}
83
if
(!is_file($this->reposFile)) {
84
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_CONF_NOT_FOUND
), $this->name .
' '
. $this->version, $this->reposFile));
85
}
86
if
(!is_file($this->exe)) {
87
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_EXE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->exe));
88
}
89
if
(!is_file($this->bash)) {
90
Log::error
(sprintf(
$bearsamppLang
->getValue(
Lang::ERROR_EXE_NOT_FOUND
), $this->name .
' '
. $this->version, $this->bash));
91
}
92
93
if
(is_file($this->reposFile)) {
94
$this->repos = explode(PHP_EOL, file_get_contents($this->reposFile));
95
$rebuildRepos = array();
96
foreach
($this->repos as $repo) {
97
$repo = trim($repo);
98
if
(stripos($repo,
':'
) ===
false
) {
99
$repo =
Path::getRootPath
() .
'/'
. $repo;
100
}
101
if
(is_dir($repo)) {
102
$rebuildRepos[] =
Path::formatUnixPath
($repo);
103
}
else
{
104
Log::warning
($this->name .
' repository not found: '
. $repo);
105
}
106
}
107
$this->repos = $rebuildRepos;
108
}
109
}
110
119
protected
function
updateConfig
(
$version
=
null
, $sub = 0, $showWindow =
false
) {
120
global $bearsamppWinbinder;
121
122
if
(!$this->enable) {
123
return
true
;
124
}
125
126
$version
=
$version
==
null
? $this->version :
$version
;
127
Log::debug
(($sub > 0 ? str_repeat(
' '
, 2 * $sub) :
''
) .
'Update '
. $this->name .
' '
.
$version
.
' config'
);
128
129
if
(file_exists(
Path::getModuleSymlinkPath
($this) .
'/post-install.bat'
)) {
130
$bearsamppWinbinder->exec($this->
getBash
(),
'--no-needs-console --hide --no-cd --command="'
.
Path::getModuleSymlinkPath
($this) .
'/post-install.bat"'
,
true
);
131
}
132
133
$bearsamppWinbinder->exec($this->
getExe
(),
'config --global core.autocrlf false'
,
true
);
134
$bearsamppWinbinder->exec($this->
getExe
(),
'config --global core.eol lf'
,
true
);
135
136
return
true
;
137
}
138
145
public
function
findRepos
($cache =
true
) {
146
$result
= array();
147
148
if
($cache) {
149
if
(file_exists($this->reposCacheFile)) {
150
$repos
= file($this->reposCacheFile);
151
foreach
(
$repos
as $repo) {
152
array_push(
$result
, trim($repo));
153
}
154
}
155
}
else
{
156
if
(!empty($this->repos)) {
157
foreach
($this->repos as $repo) {
158
$foundRepos =
Util::findRepos
($repo, $repo,
'.git/config'
);
159
if
(!empty($foundRepos)) {
160
foreach
($foundRepos as $foundRepo) {
161
array_push(
$result
, $foundRepo);
162
}
163
}
164
}
165
}
166
$strResult = implode(PHP_EOL,
$result
);
167
file_put_contents($this->reposCacheFile, $strResult);
168
}
169
170
return
$result
;
171
}
172
178
public
function
setVersion
(
$version
) {
179
global
$bearsamppConfig
;
180
$this->version =
$version
;
181
$bearsamppConfig
->replace(self::ROOT_CFG_VERSION,
$version
);
182
$this->
reload
();
183
}
184
190
public
function
getRepos
() {
191
return
$this->repos
;
192
}
193
199
public
function
getExe
() {
200
return
$this->exe
;
201
}
202
208
public
function
getBash
() {
209
return
$this->bash
;
210
}
211
217
public
function
isScanStartup
() {
218
return
$this->scanStartup ==
Config::ENABLED
;
219
}
220
226
public
function
setScanStartup
(
$scanStartup
) {
227
$this->scanStartup = intval(
$scanStartup
) ===
Config::ENABLED
?
Config::ENABLED
:
Config::DISABLED
;
228
Util::replaceInFile
($this->bearsamppConf, array(
229
'/^'
. self::LOCAL_CFG_SCAN_STARTUP .
'/'
=> self::LOCAL_CFG_SCAN_STARTUP .
' = "'
. $this->scanStartup .
'"'
230
));
231
}
232
}
$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:154
Lang\ERROR_CONF_NOT_FOUND
const ERROR_CONF_NOT_FOUND
Definition
class.lang.php:153
Lang\GIT
const GIT
Definition
class.lang.php:146
Lang\ERROR_FILE_NOT_FOUND
const ERROR_FILE_NOT_FOUND
Definition
class.lang.php:155
Log\info
static info($data, $file=null)
Definition
class.log.php:627
Log\debug
static debug($data, $file=null)
Definition
class.log.php:616
Log\warning
static warning($data, $file=null)
Definition
class.log.php:638
Log\reloadClass
static reloadClass($classInstance)
Definition
class.log.php:670
Log\error
static error($data, $file=null)
Definition
class.log.php:650
Log\initClass
static initClass($classInstance)
Definition
class.log.php:660
Module
Definition
class.module.php:15
Module\$version
$version
Definition
class.module.php:24
Module\$type
$type
Definition
class.module.php:20
Module\$id
$id
Definition
class.module.php:21
Path\getRootPath
static getRootPath($aetrayPath=false)
Definition
class.path.php:309
Path\formatUnixPath
static formatUnixPath($path)
Definition
class.path.php:156
Path\getModuleSymlinkPath
static getModuleSymlinkPath($module)
Definition
class.path.php:54
ToolGit
Definition
class.tool.git.php:17
ToolGit\getExe
getExe()
Definition
class.tool.git.php:199
ToolGit\LOCAL_CFG_EXE
const LOCAL_CFG_EXE
Definition
class.tool.git.php:20
ToolGit\$scanStartup
$scanStartup
Definition
class.tool.git.php:33
ToolGit\findRepos
findRepos($cache=true)
Definition
class.tool.git.php:145
ToolGit\getRepos
getRepos()
Definition
class.tool.git.php:190
ToolGit\updateConfig
updateConfig($version=null, $sub=0, $showWindow=false)
Definition
class.tool.git.php:119
ToolGit\REPOS_FILE
const REPOS_FILE
Definition
class.tool.git.php:24
ToolGit\setVersion
setVersion($version)
Definition
class.tool.git.php:178
ToolGit\REPOS_CACHE_FILE
const REPOS_CACHE_FILE
Definition
class.tool.git.php:25
ToolGit\$repos
$repos
Definition
class.tool.git.php:29
ToolGit\isScanStartup
isScanStartup()
Definition
class.tool.git.php:217
ToolGit\setScanStartup
setScanStartup($scanStartup)
Definition
class.tool.git.php:226
ToolGit\reload
reload($id=null, $type=null)
Definition
class.tool.git.php:52
ToolGit\getBash
getBash()
Definition
class.tool.git.php:208
ToolGit\LOCAL_CFG_SCAN_STARTUP
const LOCAL_CFG_SCAN_STARTUP
Definition
class.tool.git.php:22
ToolGit\$bash
$bash
Definition
class.tool.git.php:32
ToolGit\ROOT_CFG_VERSION
const ROOT_CFG_VERSION
Definition
class.tool.git.php:18
ToolGit\__construct
__construct($id, $type)
Definition
class.tool.git.php:41
ToolGit\$exe
$exe
Definition
class.tool.git.php:31
ToolGit\$reposCacheFile
$reposCacheFile
Definition
class.tool.git.php:28
ToolGit\LOCAL_CFG_BASH
const LOCAL_CFG_BASH
Definition
class.tool.git.php:21
ToolGit\$reposFile
$reposFile
Definition
class.tool.git.php:27
Util\findRepos
static findRepos($initPath, $startPath, $checkFile, $maxDepth=1)
Definition
class.util.php:442
Util\replaceInFile
static replaceInFile($path, $replaceList)
Definition
class.util.php:292
$bearsamppConfig
global $bearsamppConfig
Definition
homepage.php:41
sandbox
core
classes
tools
class.tool.git.php
Generated by
1.17.0