-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTree.php
More file actions
139 lines (122 loc) · 3.58 KB
/
Tree.php
File metadata and controls
139 lines (122 loc) · 3.58 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
namespace Platformsh\Client\Model\Git;
use GuzzleHttp\ClientInterface;
use Platformsh\Client\Exception\GitObjectTypeException;
use Platformsh\Client\Model\ApiResourceBase;
use Platformsh\Client\Model\Project;
/**
* Git tree resource.
*
* @property-read string $id
* @property-read string $sha
* @property-read array $tree
*/
class Tree extends ApiResourceBase
{
/**
* Get the Tree object for an SHA hash.
*/
public static function fromSha(string $sha, string $baseUrl, ClientInterface $client): false|static
{
$url = Project::getProjectBaseFromUrl($baseUrl) . '/git/trees';
return static::get($sha, $url, $client);
}
/**
* Get an object in this tree.
*
* @param string $path The path to an object in the tree.
*
* @return Blob|Tree|false
* A Blob or Tree object, or false if the object does not exist.
*/
public function getObject(string $path): self|false|Blob|static
{
if ($path === '' || $path === '.') {
return $this;
}
$data = $this->getObjectData($path);
if ($data === false) {
return false;
}
if ($data['type'] === 'blob') {
return Blob::fromSha($data['sha'], $this->getUri(), $this->client);
} elseif ($data['type'] === 'tree') {
return self::fromSha($data['sha'], $this->getUri(), $this->client);
}
throw new \RuntimeException('Unrecognised object type: ' . $data['type']);
}
/**
* Get a Blob (file) inside this tree.
*
* @return Blob|false
* A Blob object, or false if the blob is not found.
*@throws GitObjectTypeException if the path is a directory.
*/
public function getBlob(string $path): false|Blob
{
$object = $this->getObjectRecursive($path);
if ($object === false) {
return false;
}
if ($object instanceof self) {
throw new GitObjectTypeException('The requested file is a directory', $path);
}
return $object;
}
/**
* Get a Tree (directory) inside this tree.
*
* @return Tree|false
* A Tree object or false if the tree is not found.
*@throws GitObjectTypeException if the path is not a directory.
*/
public function getTree(string $path): self|false
{
$object = $this->getObjectRecursive($path);
if ($object === false) {
return false;
}
if ($object instanceof self) {
return $object;
}
throw new GitObjectTypeException('Not a directory', $path);
}
/**
* Find an object definition by its path.
*/
private function getObjectData(string $path): false|array
{
foreach ($this->tree as $objectData) {
if ($objectData['path'] === $path) {
return $objectData;
}
}
return false;
}
/**
* Get an object recursively in this tree.
*/
private function getObjectRecursive(string $path): self|false|Blob
{
$tree = $object = $this;
foreach ($this->splitPath($path) as $part) {
$object = $tree->getObject($part);
if (! $object instanceof self) {
return $object;
}
$tree = $object;
}
return $object;
}
/**
* Split a tree path into parts.
*
* @return string[]
*/
private function splitPath(string $path): array
{
$path = trim(str_replace('\\', '/', $path), '/');
return explode('/', $path);
}
}