Ubiquity  2.0.3
php rapid development framework
GitTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
16 
23 class GitTrait{
24 
25  abstract public function _getAdminData();
26 
27  abstract public function _getAdminViewer();
28 
29  abstract public function _getAdminFiles();
30 
31  abstract public function loadView($viewName, $pData = NULL, $asString = false);
32 
33  abstract public function git();
34 
35  abstract protected function showConfMessage($content, $type, $itle,$url, $responseElement, $data, $attributes = NULL): HtmlMessage;
36 
37  abstract protected function showSimpleMessage($content, $type, $title=null,$icon = "info", $timeout = NULL, $staticName = null): HtmlMessage;
38 
39  public function gitRefresh() {
40  echo $this->_git ();
41  echo $this->jquery->compile ( $this->view );
42  }
43 
44  public function _git() {
45  }
46 
47  protected function _getRepo($getfiles = true) {
48  $gitRepo = RepositoryGit::init ( $getfiles );
49  return $gitRepo;
50  }
51 
52  public function gitInit() {
53  $this->_getRepo ();
54  $appDir=Startup::getApplicationDir ();
55  try{
57  $gitignoreFile=$appDir. DS . ".gitignore";
58  if(!file_exists($gitignoreFile)){
59  UFileSystem::openReplaceWriteFromTemplateFile(Startup::getFrameworkDir() . "/admin/templates/gitignore.tpl", $gitignoreFile, []);
60  }
61  $this->git ();
62  }catch(GitException $ge){
63  echo $this->showSimpleMessage ( $ge->getMessage(), "negative", "Push","upload", null, "init-message" );
64  echo $this->jquery->compile ( $this->view );
65  }
66  }
67 
68  public function frmSettings() {
69  $gitRepo = $this->_getRepo ();
70  $this->_getAdminViewer ()->gitFrmSettings ( $gitRepo );
71  $this->jquery->execOn ( "click", "#validate-btn", '$("#frmGitSettings").form("submit");' );
72  $this->jquery->execOn ( "click", "#cancel-btn", '$("#frm").html("");' );
73  $this->jquery->renderView ( $this->_getAdminFiles ()->getViewGitSettings () );
74  }
75 
76  public function updateGitParams() {
77  $gitRepo = $this->_getRepo ( false );
78  $activeRemoteUrl = $gitRepo->getRemoteUrl ();
79  $newRemoteUrl = URequest::post ( "remoteUrl" );
80  if (UString::isNull ( $activeRemoteUrl )) {
81  $gitRepo->getRepository ()->addRemote ( "origin", $newRemoteUrl );
82  } elseif ($activeRemoteUrl != $newRemoteUrl) {
83  $gitRepo->getRepository ()->setRemoteUrl ( "origin", $newRemoteUrl );
84  }
85  CacheManager::$cache->store ( RepositoryGit::$GIT_SETTINGS, "return " . UArray::asPhpArray ( $_POST, "array" ) . ";", true );
86  $this->git ();
87  }
88 
89  public function commit() {
90  $filesToCommit = URequest::post ( "files-to-commit", [ ] );
91  if (sizeof ( $filesToCommit ) > 0) {
92  $messages = [ ];
93  $countFilesToAdd = 0;
94  $countFilesUpdated = 0;
95  $countFilesIgnored = 0;
96  $gitRepo = $this->_getRepo ( true );
97  $repo = $gitRepo->getRepository ();
98  $filesToAdd = [ ];
99  $allFiles = $gitRepo->getFiles ();
100  foreach ( $allFiles as $filename => $uFile ) {
101  if (in_array ( $filename, $filesToCommit )) {
102  $filesToAdd [] = $filename;
103  if ($uFile->getStatus () == GitFileStatus::$UNTRACKED) {
104  $countFilesToAdd ++;
105  } else {
106  $countFilesUpdated ++;
107  }
108  } else {
109  if ($uFile->getStatus () != GitFileStatus::$UNTRACKED) {
110  $countFilesIgnored ++;
111  }
112  }
113  }
114 
115  $repo->addFile ( $filesToAdd );
116  if ($countFilesToAdd > 0) {
117  $messages [] = $countFilesToAdd . " new file(s) added";
118  }
119  if ($countFilesIgnored > 0) {
120  $messages [] = $countFilesIgnored . " ignored file(s).";
121  }
122  if ($countFilesUpdated > 0) {
123  $messages [] = $countFilesUpdated . " updated file(s).";
124  }
125 
126  $message = URequest::post ( "summary", "No summary" );
127  if (UString::isNotNull ( URequest::post ( "description", "" ) ))
128  $message = [ $message,URequest::post ( "description" ) ];
129  $repo->commit ( $message );
130  $msg = $this->showSimpleMessage ( "Commit successfully completed!", "positive","Commit", "check square", null, "init-message" );
131  $msg->addList ( $messages );
132  $this->_refreshParts ();
133  } else {
134  $msg = $this->showSimpleMessage ( "Nothing to commit!", "", "Commit","warning circle", null, "init-message" );
135  }
136  echo $msg;
137  echo $this->jquery->compile ( $this->view );
138  }
139 
140  protected function _refreshParts() {
141  $this->jquery->exec ( '$(".to-clear").html("");$(".to-clear-value").val("");', true );
142  $this->jquery->get ( $this->_getAdminFiles ()->getAdminBaseRoute () . "/refreshFiles", "#dtGitFiles", [ "attr" => "","jqueryDone" => "replaceWith","hasLoader" => false ] );
143  $this->jquery->get ( $this->_getAdminFiles ()->getAdminBaseRoute () . "/refreshCommits", "#dtCommits", [ "attr" => "","jqueryDone" => "replaceWith","hasLoader" => false ] );
144  }
145 
146  public function gitPush() {
147  $gitRepo = $this->_getRepo ( false );
148  try {
149  if ($gitRepo->setRepoRemoteUrl ()) {
150  $repo = $gitRepo->getRepository ();
151  $repo->push ( "origin master", [ "--set-upstream" ] );
152  $msg = $this->showSimpleMessage ( "Push successfully completed!", "positive", "Push","upload", null, "init-message" );
153  $this->_refreshParts ();
154  } else {
155  $msg = $this->showSimpleMessage ( "Check your github settings before pushing! (user name, password or remote url)", "negative","Push", "upload", null, "init-message" );
156  }
157  } catch ( GitException $ge ) {
158  $msg = $this->showSimpleMessage ( $ge->getMessage(), "negative", "Push","upload", null, "init-message" );
159  }
160  echo $msg;
161  echo $this->jquery->compile ( $this->view );
162  }
163 
164  public function gitPull() {
165  $gitRepo = $this->_getRepo ( false );
166  $repo = $gitRepo->getRepository ();
167  $repo->pull ();
168  $msg = $this->showSimpleMessage ( "Pull successfully completed!", "positive","Pull", "download", null, "init-message" );
169  $this->_refreshParts ();
170  echo $msg;
171  echo $this->jquery->compile ( $this->view );
172  }
173 
174  public function gitIgnoreEdit() {
175  $this->jquery->postFormOnClick ( "#validate-btn", $this->_getAdminFiles ()->getAdminBaseRoute () . "/gitIgnoreValidate", "gitignore-frm", "#frm" );
176  $this->jquery->execOn ( "click", "#cancel-btn", '$("#frm").html("");' );
177  $content = UFileSystem::load ( Startup::getApplicationDir () . DS . ".gitignore" );
178  if ($content === false) {
179  $content = "#gitignorefile\n";
180  }
181  $this->jquery->renderView ( $this->_getAdminFiles ()->getViewGitIgnore (), [ "content" => $content ] );
182  }
183 
184  public function gitIgnoreValidate() {
185  if (URequest::isPost ()) {
186  $content = URequest::post ( "content" );
187  if (UFileSystem::save ( Startup::getApplicationDir () . DS . ".gitignore", $content )) {
188  $this->jquery->get ( $this->_getAdminFiles ()->getAdminBaseRoute () . "/refreshFiles", "#dtGitFiles", [ "attr" => "","jqueryDone" => "replaceWith","hasLoader" => false ] );
189  $message = $this->showSimpleMessage ( "<b>.gitignore</b> file saved !", "positive", "gitignore","git" );
190  } else {
191  $message = $this->showSimpleMessage ( "<b>.gitignore</b> file not saved !", "warning", "gitignore","git" );
192  }
193  }
194  echo $message;
195  echo $this->jquery->compile ( $this->view );
196  }
197 
198  public function refreshFiles() {
199  $gitRepo = $this->_getRepo ();
200  $files=$gitRepo->getFiles ();
201  echo $this->_getAdminViewer ()->getGitFilesDataTable ( $files );
202  $this->jquery->exec('$("#lbl-changed").toggle('.((sizeof($files)>0)?"true":"false").');',true);
203  echo $this->jquery->compile ( $this->view );
204  }
205 
206  public function refreshCommits() {
207  $gitRepo = $this->_getRepo ( false );
208  echo $this->_getAdminViewer ()->getGitCommitsDataTable ( $gitRepo->getCommits () );
209  echo $this->jquery->compile ( $this->view );
210  }
211 
212  public function changesInfiles(...$filenameParts) {
213  $filename = implode ( DS, $filenameParts );
214  $gitRepo = $this->_getRepo ( false );
215  $changes = $gitRepo->getRepository ()->getChangesInFile ( $filename );
216  if (UString::isNull ( $changes )) {
217  $changes = str_replace ( PHP_EOL, " ", UFileSystem::load ( Startup::getApplicationDir () . DS . $filename ) );
218  }
219  $this->jquery->exec ( 'var value=\'' . htmlentities ( str_replace ( "'", "\\'", str_replace ( "\u", "\\\u", $changes ) ) ) . '\';$("#changes-in-file").html(Diff2Html.getPrettyHtml($("<div/>").html(value).text()),{inputFormat: "diff", showFiles: true, matching: "lines"});', true );
220  echo '<div id="changes-in-file"></div>';
221  echo $this->jquery->compile ( $this->view );
222  }
223 
224  public function changesInCommit($commitHash) {
225  $gitRepo = $this->_getRepo ( false );
226  $changes = $gitRepo->getRepository ()->getChangesInCommit ( $commitHash );
227  if (UString::isNull ( $changes )) {
228  $changes = "No change";
229  }
230  $this->jquery->exec ( 'var value=\'' . htmlentities ( str_replace ( "'", "\\'", str_replace ( "\u", "\\\u", $changes ) ) ) . '\';var diff2htmlUi = new Diff2HtmlUI({diff: $("<div/>").html(value).text()});diff2htmlUi.draw("#changes-in-commit", {inputFormat: "diff", showFiles: true, matching: "lines"});diff2htmlUi.fileListCloseable("#changes-in-commit", true);', true );
231  echo '<div id="changes-in-commit"></div>';
232  echo $this->jquery->compile ( $this->view );
233  }
234 }
static init($directory, array $params=NULL)
Init repo in directory.
static isPost()
Returns true if the request is sent by the POST method.
Definition: URequest.php:109
loadView($viewName, $pData=NULL, $asString=false)
static post($key, $default=NULL)
Returns the value of the $key variable passed by the post method or $default if the $key variable doe...
Definition: URequest.php:146
static openReplaceWriteFromTemplateFile($source, $destination, $keyAndValues)
Definition: UFileSystem.php:73
showConfMessage($content, $type, $itle, $url, $responseElement, $data, $attributes=NULL)
showSimpleMessage($content, $type, $title=null, $icon="info", $timeout=NULL, $staticName=null)
static asPhpArray($array, $prefix="", $depth=1, $format=false)
Definition: UArray.php:53
static save($filename, $content, $flags=LOCK_EX)