|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/repository package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Repository\Resource; |
|
13
|
|
|
|
|
14
|
|
|
use Puli\Repository\Api\ResourceNotFoundException; |
|
15
|
|
|
use Puli\Repository\Resource\Collection\FilesystemResourceCollection; |
|
16
|
|
|
use Webmozart\Assert\Assert; |
|
17
|
|
|
use Webmozart\Glob\Iterator\RecursiveDirectoryIterator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Represents a directory on the file system. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 1.0 |
|
23
|
|
|
* |
|
24
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class DirectoryResource extends AbstractFilesystemResource |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
411 |
|
public function __construct($filesystemPath, $path = null) |
|
32
|
|
|
{ |
|
33
|
411 |
|
Assert::directory($filesystemPath); |
|
34
|
|
|
|
|
35
|
408 |
|
parent::__construct($filesystemPath, $path); |
|
36
|
408 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
3 |
|
public function getChild($relPath) |
|
42
|
|
|
{ |
|
43
|
|
|
// Use attached repository if possible |
|
44
|
3 |
|
if ($this->getRepository()) { |
|
45
|
2 |
|
return $this->getRepository()->get($this->getRepositoryPath().'/'.$relPath); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$filesystemPath = $this->getFilesystemPath().'/'.$relPath; |
|
49
|
|
|
|
|
50
|
1 |
|
if (!file_exists($filesystemPath)) { |
|
51
|
|
|
throw ResourceNotFoundException::forPath($this->getPath().'/'.$relPath); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return is_dir($filesystemPath) |
|
55
|
|
|
? new self($filesystemPath) |
|
56
|
1 |
|
: new FileResource($filesystemPath); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function hasChild($relPath) |
|
63
|
|
|
{ |
|
64
|
|
|
// Use attached repository if possible |
|
65
|
3 |
|
if ($this->getRepository()) { |
|
66
|
2 |
|
return $this->getRepository()->contains($this->getRepositoryPath().'/'.$relPath); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return file_exists($this->getFilesystemPath().'/'.$relPath); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
36 |
|
public function hasChildren() |
|
76
|
|
|
{ |
|
77
|
|
|
// Use attached repository if possible |
|
78
|
36 |
|
if ($this->getRepository()) { |
|
79
|
10 |
|
return $this->getRepository()->hasChildren($this->getRepositoryPath()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
26 |
|
$iterator = new RecursiveDirectoryIterator( |
|
83
|
26 |
|
$this->getFilesystemPath(), |
|
84
|
26 |
|
RecursiveDirectoryIterator::SKIP_DOTS |
|
85
|
|
|
); |
|
86
|
26 |
|
$iterator->rewind(); |
|
87
|
|
|
|
|
88
|
26 |
|
return $iterator->valid(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
136 |
|
public function listChildren() |
|
95
|
|
|
{ |
|
96
|
136 |
|
$children = new FilesystemResourceCollection(); |
|
97
|
|
|
|
|
98
|
|
|
// Use attached repository if possible |
|
99
|
136 |
|
if ($this->getRepository()) { |
|
100
|
21 |
|
foreach ($this->getRepository()->listChildren($this->getRepositoryPath()) as $child) { |
|
101
|
17 |
|
$children[$child->getName()] = $child; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
21 |
|
return $children; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
120 |
|
$iterator = new RecursiveDirectoryIterator( |
|
108
|
120 |
|
$this->getFilesystemPath(), |
|
109
|
120 |
|
RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
// We can't use glob() here, because glob() doesn't list files starting |
|
113
|
|
|
// with "." by default |
|
114
|
120 |
|
foreach ($iterator as $path) { |
|
115
|
74 |
|
$children[basename($path)] = is_dir($path) |
|
116
|
68 |
|
? new self($path) |
|
117
|
74 |
|
: new FileResource($path); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
120 |
|
return $children; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|