Skip to content

Commit 2109010

Browse files
harm-lessharm-less
harm-less
authored and
harm-less
committed
Addition features and bugfixes
- Removing directories by id, index and a Dir instance - Get the full absolute/base path from a child
1 parent e6ca609 commit 2109010

File tree

10 files changed

+340
-37
lines changed

10 files changed

+340
-37
lines changed

src/FQ/Files.php

+76-26
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ class Files {
1818
* @var RootDirCollection Container with all root directories
1919
*
2020
*/
21-
private $_rootDirs;
21+
private $_rootDirCollection;
2222

2323
/**
2424
* @var ChildDirCollection Container with all child directories
2525
*
2626
*/
27-
private $_childDirs;
27+
private $_childDirCollection;
2828

2929
/**
3030
* @var FilesQuery Query object for each Files instance
@@ -35,16 +35,16 @@ class Files {
3535
const DEFAULT_EXTENSION = 'php';
3636

3737
function __construct() {
38-
$this->_rootDirs = new RootDirCollection();
39-
$this->_childDirs = new ChildDirCollection();
38+
$this->_rootDirCollection = new RootDirCollection();
39+
$this->_childDirCollection = new ChildDirCollection();
4040
$this->_query = new FilesQuery($this);
4141
}
4242

4343
/**
4444
* @return RootDirCollection
4545
*/
46-
protected function _rootDirs() {
47-
return $this->_rootDirs;
46+
protected function _rootCollection() {
47+
return $this->_rootDirCollection;
4848
}
4949

5050
/**
@@ -53,70 +53,95 @@ protected function _rootDirs() {
5353
* @return RootDir|false
5454
*/
5555
public function addRootDir(RootDir $rootDir, $index = null) {
56-
$rootDir = $this->_rootDirs()->addRootDir($rootDir, $index);
56+
$rootDir = $this->_rootCollection()->addRootDir($rootDir, $index);
5757
return $this->isValid() === true ? $rootDir : false;
5858
}
5959

60+
/**
61+
* @param RootDir $rootDir
62+
* @return bool
63+
*/
64+
public function removeRootDir(RootDir $rootDir) {
65+
return $this->_rootCollection()->removeDir($rootDir);
66+
}
67+
/**
68+
* @param string $id
69+
* @return bool
70+
*/
71+
public function removeRootDirById($id) {
72+
return $this->_rootCollection()->removeDirById($id);
73+
}
74+
/**
75+
* @param int $index
76+
* @return bool
77+
*/
78+
public function removeRootDirAtIndex($index) {
79+
return $this->_rootCollection()->removeDirAtIndex($index);
80+
}
81+
public function removeAllRootDirs() {
82+
$this->_rootCollection()->removeAllDirs();
83+
}
84+
6085
/**
6186
* @return RootDir[]
6287
*/
6388
public function rootDirs() {
64-
return $this->_rootDirs()->dirs();
89+
return $this->_rootCollection()->dirs();
6590
}
6691

6792
/**
6893
* @param RootDir $rootDir RootDir that will be checked
6994
* @return bool Returns true if RootDir is part of this files instance
7095
*/
7196
public function containsRootDir(RootDir $rootDir) {
72-
return $this->_rootDirs()->isInCollection($rootDir);
97+
return $this->_rootCollection()->isInCollection($rootDir);
7398
}
7499

75100
/**
76101
* @return int Total root directories
77102
*/
78103
public function totalRootDirs() {
79-
return $this->_rootDirs()->totalDirs();
104+
return $this->_rootCollection()->totalDirs();
80105
}
81106

82107
/**
83108
* @param string|RootDir $rootDir
84109
* @return RootDir|null
85110
*/
86111
public function getRootDir($rootDir) {
87-
return $this->_rootDirs()->getDir($rootDir);
112+
return $this->_rootCollection()->getDir($rootDir);
88113
}
89114

90115
/**
91116
* @param string $id
92117
* @return null|RootDir
93118
*/
94119
public function getRootDirById($id) {
95-
return $this->_rootDirs()->getDirById($id);
120+
return $this->_rootCollection()->getDirById($id);
96121
}
97122

98123
/**
99124
* @param int $index
100125
* @return RootDir|false
101126
*/
102127
public function getRootDirByIndex($index) {
103-
return $this->_rootDirs()->getDirByIndex($index);
128+
return $this->_rootCollection()->getDirByIndex($index);
104129
}
105130

106131
/**
107132
* @return string[]
108133
*/
109134
public function getRootPaths() {
110-
return $this->_rootDirs()->getPaths();
135+
return $this->_rootCollection()->getPaths();
111136
}
112137

113138

114139

115140
/**
116141
* @return ChildDirCollection
117142
*/
118-
protected function _childDirs() {
119-
return $this->_childDirs;
143+
protected function _childCollection() {
144+
return $this->_childDirCollection;
120145
}
121146

122147
/**
@@ -125,61 +150,86 @@ protected function _childDirs() {
125150
* @return ChildDir|false
126151
*/
127152
public function addChildDir(ChildDir $childDir, $index = null) {
128-
$childDir = $this->_childDirs()->addChildDir($childDir, $index);
153+
$childDir = $this->_childCollection()->addChildDir($childDir, $index);
129154
return $this->isValid() === true ? $childDir : false;
130155
}
131156

157+
/**
158+
* @param ChildDir $childDir
159+
* @return bool
160+
*/
161+
public function removeChildDir(ChildDir $childDir) {
162+
return $this->_childCollection()->removeDir($childDir);
163+
}
164+
/**
165+
* @param string $id
166+
* @return bool
167+
*/
168+
public function removeChildDirById($id) {
169+
return $this->_childCollection()->removeDirById($id);
170+
}
171+
/**
172+
* @param int $index
173+
* @return bool
174+
*/
175+
public function removeChildDirAtIndex($index) {
176+
return $this->_childCollection()->removeDirAtIndex($index);
177+
}
178+
public function removeAllChildDirs() {
179+
$this->_childCollection()->removeAllDirs();
180+
}
181+
132182
/**
133183
* @return ChildDir[]
134184
*/
135185
public function childDirs() {
136-
return $this->_childDirs()->dirs();
186+
return $this->_childCollection()->dirs();
137187
}
138188

139189
/**
140190
* @param ChildDir $childDir Dir that will be checked
141191
* @return bool Returns true if dir is part of this files instance
142192
*/
143193
public function containsChildDir(ChildDir $childDir) {
144-
return $this->_childDirs()->isInCollection($childDir);
194+
return $this->_childCollection()->isInCollection($childDir);
145195
}
146196

147197
/**
148198
* @return int Total child directories
149199
*/
150200
public function totalChildDirs() {
151-
return $this->_childDirs()->totalDirs();
201+
return $this->_childCollection()->totalDirs();
152202
}
153203

154204
/**
155205
* @param string|ChildDir $childDir
156206
* @return null|ChildDir
157207
*/
158208
public function getChildDir($childDir) {
159-
return $this->_childDirs()->getDir($childDir);
209+
return $this->_childCollection()->getDir($childDir);
160210
}
161211

162212
/**
163213
* @param string $id
164214
* @return null|ChildDir
165215
*/
166216
public function getChildDirById($id) {
167-
return $this->_childDirs()->getDirById($id);
217+
return $this->_childCollection()->getDirById($id);
168218
}
169219

170220
/**
171221
* @param int $index
172222
* @return ChildDir|false
173223
*/
174224
public function getChildDirByIndex($index) {
175-
return $this->_childDirs()->getDirByIndex($index);
225+
return $this->_childCollection()->getDirByIndex($index);
176226
}
177227

178228
/**
179229
* @return string[]
180230
*/
181231
public function getChildPaths() {
182-
return $this->_childDirs()->getPaths();
232+
return $this->_childCollection()->getPaths();
183233
}
184234

185235
/**
@@ -192,7 +242,7 @@ protected function isValid() {
192242
if ($rootDir->isRequired()) {
193243
$rootDirPath = $rootDir->dir();
194244
if (!is_dir($rootDirPath)) {
195-
throw new FilesException(sprintf('Root directory "%s" does not exist but is required. Please create this directory or turn this requirement off.', $rootDirPath));
245+
throw new FilesException(sprintf('Absolute root directory "%s" does not exist but is required. Please create this directory or turn this requirement off.', $rootDirPath));
196246
}
197247
foreach ($this->childDirs() as $childDir) {
198248
if ($childDir->isRequired()) {
@@ -221,7 +271,7 @@ public function getFullPath($rootDir, $childDir) {
221271
$childDirId = $childObj !== null ? $childObj->id() : '-';
222272
throw new FilesException(sprintf('Cannot build a full path because either the root directory, the child directory or both are not defined. Root directory id "%s". Child directory id "%s"', $rootDirId, $childDirId));
223273
}
224-
return $rootDirObj->dir() . '/' . $childObj->dir();
274+
return $childObj->fullAbsolutePath($rootDirObj);
225275
}
226276

227277
/**

src/FQ/collections/DirCollection.php

+43
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,49 @@ public function getDirByIndex($index) {
7373
return $dirs[$index];
7474
}
7575

76+
/**
77+
* @param Dir $dir
78+
* @return bool
79+
*/
80+
public function removeDir(Dir $dir) {
81+
if (($index = array_search($dir, $this->_dirs)) !== false) {
82+
array_splice($this->_dirs, $index, 1);
83+
return $dir;
84+
}
85+
return false;
86+
}
87+
/**
88+
* @param string $id
89+
* @return bool
90+
*/
91+
public function removeDirById($id) {
92+
foreach ($this->_dirs as $index => $dir) {
93+
if ($dir->id() === $id) {
94+
array_splice($this->_dirs, $index, 1);
95+
return true;
96+
}
97+
}
98+
return false;
99+
}
100+
/**
101+
* @param int $index
102+
* @return bool
103+
*/
104+
public function removeDirAtIndex($index) {
105+
if (count($this->_dirs) > $index) {
106+
array_splice($this->_dirs, $index, 1);
107+
return true;
108+
}
109+
return false;
110+
}
111+
112+
/**
113+
*
114+
*/
115+
public function removeAllDirs() {
116+
$this->_dirs = array();
117+
}
118+
76119
/**
77120
* @return string[]
78121
*/

src/FQ/dirs/ChildDir.php

+16
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,20 @@ public function defaultFileExtension($extension = null) {
2828
}
2929
return $this->defaultFileExtension;
3030
}
31+
32+
/**
33+
* @param RootDir $rootDir
34+
* @return string
35+
*/
36+
public function fullAbsolutePath(RootDir $rootDir) {
37+
return $rootDir->dir() . '/' . $this->dir();
38+
}
39+
40+
/**
41+
* @param RootDir $rootDir
42+
* @return string
43+
*/
44+
public function fullBasePath(RootDir $rootDir) {
45+
return $rootDir->basePath() . '/' . $this->dir();
46+
}
3147
}

src/FQ/query/FilesQuery.php

+25
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public function reset() {
109109
$this->_currentQueryChildren = array();
110110

111111
$this->_cachedPaths = null;
112+
$this->_cachedPathsSimple = null;
112113
$this->_cachedRawPaths = null;
114+
$this->_cachedRawPathsSimple = null;
113115

114116
$this->requirements()->removeAll();
115117
$this->filters(self::FILTER_EXISTING, false);
@@ -161,6 +163,9 @@ public function filters($filters = null, $mergeWithExistingFilters = true) {
161163
}
162164
return $this->_filters;
163165
}
166+
public function queryHasFilter($filter) {
167+
return in_array($filter, $this->filters());
168+
}
164169

165170
public function setRootDirSelection(RootSelection $selection) {
166171
$this->_rootDirSelection = $selection;
@@ -195,6 +200,7 @@ public function queriedFileName() {
195200
}
196201

197202
/**
203+
* @param bool $reversed
198204
* @return FilesQueryChild[]
199205
*/
200206
public function queryChildDirs($reversed = false) {
@@ -233,6 +239,10 @@ protected function _hasRunCheck() {
233239
* @return null|string[]
234240
*/
235241
public function run($fileName) {
242+
if ($this->files()->totalRootDirs() === 0) {
243+
throw new FileQueryException(sprintf('Query is trying to run with file "%s" but no root directories are configured. Make sure sure you have added at least one root directory with Files::addRootDir() before you run a query', $fileName));
244+
}
245+
236246
$this->_queriedFileName = $fileName;
237247
$rootDirsSelection = $this->_getCachedRootDirSelection();
238248

@@ -248,6 +258,21 @@ public function run($fileName) {
248258
return $this->_queryError === null;
249259
}
250260

261+
public function load() {
262+
$this->_hasRunCheck();
263+
264+
if ($this->queryHasFilter(FilesQuery::FILTER_EXISTING)) {
265+
foreach ($this->listPathsSimple() as $path) {
266+
/** @noinspection PhpIncludeInspection */
267+
require_once $path;
268+
}
269+
return true;
270+
}
271+
else {
272+
throw new FileQueryException('Loading files requires the filter "existing"');
273+
}
274+
}
275+
251276
/**
252277
* @return RootDir[]
253278
*/

0 commit comments

Comments
 (0)