1 | <?php |
||
56 | class FileAdapterStrategy extends AbstractAdapterStrategy |
||
57 | { |
||
58 | /** |
||
59 | * Adapter strategy type |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | const TYPE = 'file'; |
||
64 | /** |
||
65 | * Configuration |
||
66 | * |
||
67 | * Example |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $config = null; |
||
72 | /** |
||
73 | * Root directory (without trailing directory separator) |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $root = null; |
||
78 | /** |
||
79 | * Configuration directory (including trailing directory separator) |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $configDir = null; |
||
84 | |||
85 | /** |
||
86 | * Adapter strategy constructor |
||
87 | * |
||
88 | * @param array $config Adapter strategy configuration |
||
89 | * @throws InvalidArgumentException If the root directory configuration is empty |
||
90 | * @throws InvalidArgumentException If the root directory configuration is invalid |
||
91 | */ |
||
92 | 12 | public function __construct(array $config) |
|
93 | { |
||
94 | 12 | parent::__construct($config, ['root']); |
|
95 | |||
96 | // If the root directory configuration is empty |
||
97 | 11 | if (empty($this->config['root'])) { |
|
98 | 1 | throw new InvalidArgumentException( |
|
99 | 1 | 'Empty file adapter strategy root', |
|
100 | 1 | InvalidArgumentException::EMTPY_FILE_STRATEGY_ROOT |
|
101 | ); |
||
102 | } |
||
103 | |||
104 | // If the root directory configuration is invalid |
||
105 | 10 | $this->root = realpath($this->config['root']); |
|
106 | 10 | if (empty($this->root) || !@is_dir($this->root)) { |
|
107 | 1 | throw new InvalidArgumentException( |
|
108 | sprintf( |
||
109 | 1 | 'Invalid file adapter strategy root "%s"', |
|
110 | 1 | $this->config['root'] |
|
111 | ), |
||
112 | 1 | InvalidArgumentException::INVALID_FILE_STRATEGY_ROOT |
|
113 | ); |
||
114 | } |
||
115 | |||
116 | 9 | $this->configDir = $this->root.DIRECTORY_SEPARATOR.'.repo'.DIRECTORY_SEPARATOR; |
|
117 | 9 | } |
|
118 | |||
119 | /** |
||
120 | * Find objects by selector |
||
121 | * |
||
122 | * @param Selector|SelectorInterface $selector Object selector |
||
123 | * @param RepositoryInterface $repository Object repository |
||
124 | * @return array[PathInterface] Object paths |
||
|
|||
125 | */ |
||
126 | 7 | public function findObjectPaths(SelectorInterface $selector, RepositoryInterface $repository) |
|
127 | { |
||
128 | 7 | chdir($this->root); |
|
129 | |||
130 | // Build a glob string from the selector |
||
131 | 7 | $glob = ''; |
|
132 | 7 | $globFlags = GLOB_ONLYDIR | GLOB_NOSORT; |
|
133 | |||
134 | 7 | $year = $selector->getYear(); |
|
135 | 7 | if ($year !== null) { |
|
136 | 7 | $glob .= '/'.$year; |
|
137 | } |
||
138 | |||
139 | 7 | $month = $selector->getMonth(); |
|
140 | 7 | if ($month !== null) { |
|
141 | 7 | $glob .= '/'.$month; |
|
142 | } |
||
143 | |||
144 | 7 | $day = $selector->getDay(); |
|
145 | 7 | if ($day !== null) { |
|
146 | 7 | $glob .= '/'.$day; |
|
147 | } |
||
148 | |||
149 | 7 | $hour = $selector->getHour(); |
|
150 | 7 | if ($hour !== null) { |
|
151 | 2 | $glob .= '/'.$hour; |
|
152 | } |
||
153 | |||
154 | 7 | $minute = $selector->getMinute(); |
|
155 | 7 | if ($minute !== null) { |
|
156 | 2 | $glob .= '/'.$minute; |
|
157 | } |
||
158 | |||
159 | 7 | $second = $selector->getSecond(); |
|
160 | 7 | if ($second !== null) { |
|
161 | 2 | $glob .= '/'.$second; |
|
162 | } |
||
163 | |||
164 | 7 | $uid = $selector->getId(); |
|
165 | 7 | $type = $selector->getType(); |
|
166 | 7 | if (($uid !== null) || ($type !== null)) { |
|
167 | 7 | $glob .= '/'.($uid ?: Selector::WILDCARD).'.'.($type ?: Selector::WILDCARD); |
|
168 | |||
169 | 7 | $revision = $selector->getRevision(); |
|
170 | 7 | if ($revision !== null) { |
|
171 | 1 | $glob .= '/'.($uid ?: Selector::WILDCARD).'-'.$revision; |
|
172 | 1 | $globFlags &= ~GLOB_ONLYDIR; |
|
173 | } |
||
174 | } |
||
175 | |||
176 | 7 | return array_map( |
|
177 | 7 | function ($objectPath) use ($repository) { |
|
178 | 7 | return Kernel::create(RepositoryPath::class, [$repository, '/'.$objectPath]); |
|
179 | 7 | }, |
|
180 | 7 | glob(ltrim($glob, '/'), $globFlags) |
|
181 | ); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Find and return an object resource |
||
186 | * |
||
187 | * @param string $resourcePath Repository relative resource path |
||
188 | * @return ResourceInterface Object resource |
||
189 | */ |
||
190 | 6 | public function getObjectResource($resourcePath) |
|
194 | |||
195 | /** |
||
196 | * Return the repository size (number of objects in the repository) |
||
197 | * |
||
198 | * @return int Repository size |
||
199 | */ |
||
200 | public function getRepositorySize() |
||
209 | |||
210 | /** |
||
211 | * Initialize the repository |
||
212 | * |
||
213 | * @return void |
||
214 | * @throws RuntimeException If the repository cannot be initialized |
||
215 | * @throws RuntimeException If the repository size descriptor can not be created |
||
216 | */ |
||
217 | public function initializeRepository() |
||
232 | } |
||
233 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.