Ubiquity  2.0.3
php rapid development framework
RepositoryGit.php
Go to the documentation of this file.
1 <?php
3 
11 
13  public static $GIT_SETTINGS="git/settings";
14  private $name;
15  private $initialized;
16  private $files;
17  private $remoteUrl;
18  private $user;
19  private $password;
20  private $commits;
24  private $repository;
25 
29  public function getRepository() {
30  return $this->repository;
31  }
32 
36  public function setRepository($repository) {
37  $this->repository = $repository;
38  }
39 
43  public function getRemoteUrl() {
44  return $this->remoteUrl;
45  }
46 
50  public function getAuthRemoteUrl() {
52  if(UString::isNotNull($this->user) && UString::isNotNull($this->password) && strpos($remoteUrl, "//")!==false)
53  return str_replace("//", "//".$this->user.":".$this->password."@", $remoteUrl);
54  return false;
55  }
56 
57  public function setRepoRemoteUrl(){
58  if($url=$this->getAuthRemoteUrl()){
59  $activeRemoteUrl =$this->repository->getRemoteUrl();
60  if (UString::isNull ( $activeRemoteUrl )) {
61  $this->repository->addRemote ( "origin", $url );
62  } else {
63  $this->repository->setRemoteUrl ( "origin", $url );
64  }
65  }
66  return $url;
67  }
68 
69 
73  public function getUser() {
74  return $this->user;
75  }
76 
80  public function getPassword() {
81  return $this->password;
82  }
83 
87  public function setRemoteUrl($remoteUrl) {
88  $remoteUrl=preg_replace('@\/\/.*?\@@', "//", $remoteUrl);
89  $this->remoteUrl = $remoteUrl;
90  }
91 
95  public function setUser($user) {
96  $this->user = $user;
97  }
98 
102  public function setPassword($password) {
103  $this->password = $password;
104  }
105 
109  public function getFiles() {
110  return $this->files;
111  }
112 
116  public function setFiles($files) {
117  $this->files = $files;
118  }
119 
120  public function addFiles($files){
121  $this->files= array_merge($this->files,$files);
122  }
123 
127  public function getInitialized() {
128  return $this->initialized;
129  }
130 
134  public function setInitialized($initialized) {
135  $this->initialized = $initialized;
136  }
137 
138  public function __construct($name=""){
139  $this->name=$name;
140  $this->files=[];
141  $this->commits=[];
142  }
143 
144 
148  public function getCommits() {
149  return $this->commits;
150  }
151 
152  public function hasCommits(){
153  return sizeof($this->commits)>0;
154  }
155 
159  public function setCommits($commits) {
160  $this->commits = $commits;
161  }
162 
166  public function getName() {
167  return $this->name;
168  }
169 
173  public function setName($name) {
174  $this->name = $name;
175  }
176 
180  public static function init($getFiles=true){
181  $result=new RepositoryGit();
182  $isValid=false;
183  if(CacheManager::$cache->exists(self::$GIT_SETTINGS)){
184  $gitSettings=CacheManager::$cache->fetch(self::$GIT_SETTINGS);
185  $isValid=isset($gitSettings["name"]);
186  }
187  if(!$isValid)
188  $gitSettings["name"]=Startup::getApplicationName();
189 
190  $initialized=false;
191  if(file_exists(Startup::getApplicationDir().DS.".git")){
192  $repo=new UGitRepository(Startup::getApplicationDir().DS.".git");
193  if($getFiles){
194  $result->addFiles(self::loadUntrackedFiles($repo));
195  $result->addFiles(self::loadModifiedFiles($repo));
196  }
197  $result->setRemoteUrl($repo->getRemoteUrl());
198  $result->setRepository($repo);
199  $result->setCommits($repo->getCommits());
200  $initialized=true;
201  }
202  $gitSettings["initialized"]=$initialized;
203  URequest::setValuesToObject($result,$gitSettings);
204  return $result;
205  }
206 
207  public static function loadUntrackedFiles(UGitRepository $gitRepo){
208  $files=$gitRepo->getUntrackedFiles();
209  $result=[];
210  if(isset($files)){
211  foreach ($files as $file){
212  $result[$file]=new GitFile($file,GitFileStatus::$UNTRACKED);
213  }
214  }
215  return $result;
216  }
217 
218  public static function loadModifiedFiles(UGitRepository $gitRepo){
219  $files=$gitRepo->getModifiedFiles();
220  $result=[];
221  if(isset($files)){
222  foreach ($files as $file){
223  switch ($file[0]){
224  case "M":
225  $status=GitFileStatus::$MODIFIED;
226  break;
227  case "D":
228  $status=GitFileStatus::$DELETED;
229  break;
230  default:
231  $status=GitFileStatus::$NONE;
232  }
233  if(isset($file[1]))
234  $result[$file[1]]=new GitFile($file[1],$status);
235  }
236  }
237  return $result;
238  }
239 
240  public function __toString(){
241  $status="";
242  if(!$this->initialized)
243  $status="not initialized";
244  return "{$this->name} [{$status}]";
245  }
246 }
static setValuesToObject($object, $values=null)
Affects member to member the values of the associative array $values to the members of the object $ob...
Definition: URequest.php:21
static loadUntrackedFiles(UGitRepository $gitRepo)
getUntrackedFiles()
Returns list of untracked files in repo.
static loadModifiedFiles(UGitRepository $gitRepo)
getModifiedFiles()
Returns list of modified files in repo.