1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Contains RelativeFileSearchTrait Trait. |
5
|
|
|
* |
6
|
|
|
* PHP version 7.0+ |
7
|
|
|
* |
8
|
|
|
* LICENSE: |
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
11
|
|
|
* database. |
12
|
|
|
* Copyright (C) 2015-2017 Michael Cummings |
13
|
|
|
* |
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
17
|
|
|
* option) any later version. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
22
|
|
|
* for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
25
|
|
|
* along with this program. If not, see |
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
27
|
|
|
* |
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
30
|
|
|
* |
31
|
|
|
* @copyright 2015-2017 Michael Cummings |
32
|
|
|
* @license LGPL-3.0+ |
33
|
|
|
* @author Michael Cummings <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
namespace Yapeal\FileSystem; |
36
|
|
|
|
37
|
|
|
use Yapeal\Exception\YapealFileSystemException; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Trait RelativeFileSearchTrait |
41
|
|
|
*/ |
42
|
|
|
trait RelativeFileSearchTrait |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* Fluent interface setter for $relativeBaseDir. |
46
|
|
|
* |
47
|
|
|
* @param string $value MUST have a trailing directory separator and SHOULD be an absolute path. |
48
|
|
|
* |
49
|
|
|
* @return RelativeFileSearchTrait Fluent interface. |
|
|
|
|
50
|
|
|
*/ |
51
|
25 |
|
public function setRelativeBaseDir(string $value): self |
52
|
|
|
{ |
53
|
25 |
|
$this->relativeBaseDir = str_replace('\\', '/', $value); |
54
|
25 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
/** |
57
|
|
|
* Used to find a file relative to the base path using prefix, name, and suffix parts in varies ways. |
58
|
|
|
* |
59
|
|
|
* @param string $prefix Used as subdirectory or as part of file name. |
60
|
|
|
* @param string $name Used as part of file names only. |
61
|
|
|
* @param string $suffix Used as last part of file name or by self as file name. Think file extension without |
62
|
|
|
* leading dot. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
* @throws \DomainException |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
* @throws \LogicException |
68
|
|
|
* @throws \Yapeal\Exception\YapealFileSystemException |
69
|
|
|
*/ |
70
|
21 |
|
protected function findRelativeFileWithPath(string $prefix, string $name, string $suffix): string |
71
|
|
|
{ |
72
|
|
|
// $relDir$prefix/$name.$suffix, $relDir$prefix/$name_$suffix, |
73
|
|
|
// $relDir$prefix.$name.$suffix, $relDir$prefix_$name.$suffix, $relDir$prefix_$name_$suffix, |
74
|
|
|
// $relDir$name.$suffix, $relDir$name_$suffix, |
75
|
|
|
// $relDir$prefix.$suffix, $relDir$prefix_$suffix, |
76
|
|
|
// $relDir'common'.$suffix, $relDir'common'_$suffix, $relDir$suffix |
77
|
|
|
$combinations = '%1$s%2$s/%3$s.%4$s,%1$s%2$s/%3$s_%4$s' |
78
|
|
|
. ',%1$s%2$s.%3$s.%4$s,%1$s%2$s_%3$s.%4$s,%1$s%2$s_%3$s_%4$s' |
79
|
|
|
. ',%1$s%3$s.%4$s,%1$s%3$s_%4$s' |
80
|
|
|
. ',%1$s%2$s.%4$s,%1$s%2$s_%4$s' |
81
|
21 |
|
. ',%1$scommon.%4$s,%1$scommon_%4$s,%1$s%4$s'; |
82
|
21 |
|
$fileNames = explode(',', sprintf($combinations, $this->getRelativeBaseDir(), $prefix, $name, $suffix)); |
83
|
21 |
|
foreach ($fileNames as $fileName) { |
84
|
21 |
|
if (is_readable($fileName) && is_file($fileName)) { |
85
|
21 |
|
return $fileName; |
86
|
|
|
} |
87
|
|
|
} |
88
|
3 |
|
$mess = sprintf('Failed to find accessible file in %1$s using "%1$s", "%2$s", and "%3$s"', |
89
|
3 |
|
$this->getRelativeBaseDir(), |
90
|
|
|
$prefix, |
91
|
|
|
$name, |
92
|
|
|
$suffix); |
93
|
3 |
|
throw new YapealFileSystemException($mess); |
94
|
|
|
} |
95
|
|
|
/** |
96
|
|
|
* Getter for $relativeBaseDir. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
* @throws \LogicException |
100
|
|
|
*/ |
101
|
21 |
|
private function getRelativeBaseDir(): string |
102
|
|
|
{ |
103
|
21 |
|
if (null === $this->relativeBaseDir) { |
104
|
|
|
$mess = 'Tried to use relativeBaseDir before it was set'; |
105
|
|
|
throw new \LogicException($mess); |
106
|
|
|
} |
107
|
21 |
|
return $this->relativeBaseDir; |
108
|
|
|
} |
109
|
|
|
/** |
110
|
|
|
* Holds the path that is prepended for searches. |
111
|
|
|
* |
112
|
|
|
* @var string $relativeBaseDir |
113
|
|
|
*/ |
114
|
|
|
private $relativeBaseDir; |
115
|
|
|
} |
116
|
|
|
|
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.