This repository has been archived by the owner on Nov 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListGProjects.php
72 lines (58 loc) · 1.71 KB
/
ListGProjects.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
class ListGProjects {
private $projectRoot;
/**
* @param String $root: Root folder
*/
public function __construct($root) {
$this->setProjectRoot($this->loadGPackage($root));
$this->search($this->projectRoot);
}
public function getProjectRoot() {
return $this->projectRoot;
}
private function setProjectRoot(GProject $project) {
$this->projectRoot = $project;
}
/*********************************************
/* Control
/*********************************************/
/**
* @param GProject $projectParent : current project has be finding child projects
*/
private function search(GProject $projectParent) {
$currentDir = scandir($projectParent->getAddress());
foreach ($currentDir as $dir) {
if ($dir == "." || $dir == "..") { continue;}
$dir = $projectParent->getAddress() . "\\" . $dir;
if (!is_dir($dir)) { continue; }
try {
$project = $this->loadGPackage($dir);
} catch (GProjectException $e) {
continue;
}
$projectParent->addProject($project);
$this->search($project);
}
}
/**
* Loads a package located in $address and return a relative GProject
*
* @throws GProjectException - Package not found if this is not in $address
*/
private function loadGPackage($address) {
// Verificando existência
$fileAddress = $address . "\\" . "package.json";
if (!file_exists($fileAddress)) {
throw new GProjectException('<strong>package.json</strong> not found in given address: ' . $address);
}
// Carregando
$file = file($fileAddress);
// Formatando
$json = "";
foreach ($file as $line) { $json .= $line; }
$json = json_decode($json, true);
// Creating a GProject
return new GProject($json['name'], $json['description'], $address);
}
}