@@ -28,185 +28,185 @@ |
||
| 28 | 28 | class FilesystemLocation implements Location |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Path that this location points to. |
|
| 33 | - * |
|
| 34 | - * @since 0.1.0 |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $path; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Extensions that this location can accept. |
|
| 42 | - * |
|
| 43 | - * @since 0.1.0 |
|
| 44 | - * |
|
| 45 | - * @var Extensions |
|
| 46 | - */ |
|
| 47 | - protected $extensions; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Instantiate a FilesystemLocation object. |
|
| 51 | - * |
|
| 52 | - * @since 0.1.0 |
|
| 53 | - * |
|
| 54 | - * @param string $path Path that this location points to. |
|
| 55 | - * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept. |
|
| 56 | - */ |
|
| 57 | - public function __construct(string $path, $extensions = null) |
|
| 58 | - { |
|
| 59 | - $this->path = $path; |
|
| 60 | - $this->extensions = $this->validateExtensions($extensions); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get the first URI that matches the given criteria. |
|
| 65 | - * |
|
| 66 | - * @since 0.1.0 |
|
| 67 | - * |
|
| 68 | - * @param array $criteria Criteria to match. |
|
| 69 | - * |
|
| 70 | - * @return string|false URI that matches the criteria or false if none found. |
|
| 71 | - */ |
|
| 72 | - public function getURI(array $criteria) |
|
| 73 | - { |
|
| 74 | - $uris = $this->getURIs($criteria); |
|
| 75 | - |
|
| 76 | - return $uris->count() > 0 |
|
| 77 | - ? $uris->first() |
|
| 78 | - : false; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get all URIs that match the given criteria. |
|
| 83 | - * |
|
| 84 | - * @since 0.1.1 |
|
| 85 | - * |
|
| 86 | - * @param array $criteria Criteria to match. |
|
| 87 | - * |
|
| 88 | - * @return URIs URIs that match the criteria or an empty collection if none found. |
|
| 89 | - */ |
|
| 90 | - public function getURIs(array $criteria): URIs |
|
| 91 | - { |
|
| 92 | - $uris = new URIs(); |
|
| 93 | - |
|
| 94 | - foreach ($this->extensions as $extension) { |
|
| 95 | - $finder = new Finder(); |
|
| 96 | - |
|
| 97 | - try { |
|
| 98 | - bdump( $this->getPathPattern($this->getRelativePath($criteria)) ); |
|
| 99 | - $finder->files() |
|
| 100 | - ->name($this->getNamePattern($criteria, $extension)) |
|
| 101 | - ->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
| 102 | - foreach ($finder as $file) { |
|
| 103 | - /** @var SplFileInfo $file */ |
|
| 104 | - $uris->add($file->getPathname()); |
|
| 105 | - } |
|
| 106 | - } catch (Exception $exception) { |
|
| 107 | - // Fail silently; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return $uris; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the name pattern to pass to the file finder. |
|
| 116 | - * |
|
| 117 | - * @since 0.1.3 |
|
| 118 | - * |
|
| 119 | - * @param array $criteria Criteria to match. |
|
| 120 | - * @param string $extension Extension to match. |
|
| 121 | - * |
|
| 122 | - * @return string Name pattern to pass to the file finder. |
|
| 123 | - */ |
|
| 124 | - protected function getNamePattern(array $criteria, string $extension): string |
|
| 125 | - { |
|
| 126 | - $names = []; |
|
| 127 | - |
|
| 128 | - $names[] = array_map(function ($criterion) use ($extension) { |
|
| 129 | - $uriExtension = URIHelper::containsExtension($criterion); |
|
| 130 | - if (! empty($extension)) { |
|
| 131 | - $extension = ltrim($extension, '.'); |
|
| 132 | - |
|
| 133 | - if ($uriExtension === $extension) { |
|
| 134 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
| 135 | - } |
|
| 136 | - } else { |
|
| 137 | - $extension = URIHelper::containsExtension($criterion); |
|
| 138 | - if (!empty($extension)) { |
|
| 139 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
| 144 | - |
|
| 145 | - return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
| 146 | - ? "{$criterion}(?:\..*?)$" |
|
| 147 | - : "{$criterion}\.{$extension}$"; |
|
| 148 | - }, $criteria)[0]; |
|
| 149 | - |
|
| 150 | - return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the path pattern to pass to the file finder. |
|
| 155 | - * |
|
| 156 | - * @since 0.4.7 |
|
| 157 | - * |
|
| 158 | - * @param string $relativePath Relative path that was included in the |
|
| 159 | - * criterion. |
|
| 160 | - * @return string Path pattern to pass to the file finder. |
|
| 161 | - */ |
|
| 162 | - protected function getPathPattern(string $relativePath): string |
|
| 163 | - { |
|
| 164 | - if (empty($relativePath)) { |
|
| 165 | - return $this->path; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/'); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Get the relative path that is included in the criterion. |
|
| 173 | - * |
|
| 174 | - * @since 0.4.7 |
|
| 175 | - * |
|
| 176 | - * @param array $criteria Criteria to extract the relative path from. |
|
| 177 | - * @return string Relative path included in the criterion. |
|
| 178 | - */ |
|
| 179 | - protected function getRelativePath($criteria): string |
|
| 180 | - { |
|
| 181 | - $criterion = current($criteria); |
|
| 182 | - $components = explode('/', $criterion); |
|
| 183 | - |
|
| 184 | - if (count($components) < 2) { |
|
| 185 | - return ''; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return implode('/', array_slice($components, 0, -1)); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Validate the extensions and return a collection. |
|
| 193 | - * |
|
| 194 | - * @since 0.1.1 |
|
| 195 | - * |
|
| 196 | - * @param Extensions|array|string|null $extensions Extensions to validate. |
|
| 197 | - * |
|
| 198 | - * @return Extensions Validated extensions collection. |
|
| 199 | - */ |
|
| 200 | - protected function validateExtensions($extensions): Extensions |
|
| 201 | - { |
|
| 202 | - if (empty($extensions)) { |
|
| 203 | - $extensions = new Extensions(['']); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if (! $extensions instanceof Extensions) { |
|
| 207 | - $extensions = new Extensions((array)$extensions); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - return $extensions; |
|
| 211 | - } |
|
| 31 | + /** |
|
| 32 | + * Path that this location points to. |
|
| 33 | + * |
|
| 34 | + * @since 0.1.0 |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $path; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Extensions that this location can accept. |
|
| 42 | + * |
|
| 43 | + * @since 0.1.0 |
|
| 44 | + * |
|
| 45 | + * @var Extensions |
|
| 46 | + */ |
|
| 47 | + protected $extensions; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Instantiate a FilesystemLocation object. |
|
| 51 | + * |
|
| 52 | + * @since 0.1.0 |
|
| 53 | + * |
|
| 54 | + * @param string $path Path that this location points to. |
|
| 55 | + * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept. |
|
| 56 | + */ |
|
| 57 | + public function __construct(string $path, $extensions = null) |
|
| 58 | + { |
|
| 59 | + $this->path = $path; |
|
| 60 | + $this->extensions = $this->validateExtensions($extensions); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get the first URI that matches the given criteria. |
|
| 65 | + * |
|
| 66 | + * @since 0.1.0 |
|
| 67 | + * |
|
| 68 | + * @param array $criteria Criteria to match. |
|
| 69 | + * |
|
| 70 | + * @return string|false URI that matches the criteria or false if none found. |
|
| 71 | + */ |
|
| 72 | + public function getURI(array $criteria) |
|
| 73 | + { |
|
| 74 | + $uris = $this->getURIs($criteria); |
|
| 75 | + |
|
| 76 | + return $uris->count() > 0 |
|
| 77 | + ? $uris->first() |
|
| 78 | + : false; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get all URIs that match the given criteria. |
|
| 83 | + * |
|
| 84 | + * @since 0.1.1 |
|
| 85 | + * |
|
| 86 | + * @param array $criteria Criteria to match. |
|
| 87 | + * |
|
| 88 | + * @return URIs URIs that match the criteria or an empty collection if none found. |
|
| 89 | + */ |
|
| 90 | + public function getURIs(array $criteria): URIs |
|
| 91 | + { |
|
| 92 | + $uris = new URIs(); |
|
| 93 | + |
|
| 94 | + foreach ($this->extensions as $extension) { |
|
| 95 | + $finder = new Finder(); |
|
| 96 | + |
|
| 97 | + try { |
|
| 98 | + bdump( $this->getPathPattern($this->getRelativePath($criteria)) ); |
|
| 99 | + $finder->files() |
|
| 100 | + ->name($this->getNamePattern($criteria, $extension)) |
|
| 101 | + ->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
| 102 | + foreach ($finder as $file) { |
|
| 103 | + /** @var SplFileInfo $file */ |
|
| 104 | + $uris->add($file->getPathname()); |
|
| 105 | + } |
|
| 106 | + } catch (Exception $exception) { |
|
| 107 | + // Fail silently; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return $uris; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the name pattern to pass to the file finder. |
|
| 116 | + * |
|
| 117 | + * @since 0.1.3 |
|
| 118 | + * |
|
| 119 | + * @param array $criteria Criteria to match. |
|
| 120 | + * @param string $extension Extension to match. |
|
| 121 | + * |
|
| 122 | + * @return string Name pattern to pass to the file finder. |
|
| 123 | + */ |
|
| 124 | + protected function getNamePattern(array $criteria, string $extension): string |
|
| 125 | + { |
|
| 126 | + $names = []; |
|
| 127 | + |
|
| 128 | + $names[] = array_map(function ($criterion) use ($extension) { |
|
| 129 | + $uriExtension = URIHelper::containsExtension($criterion); |
|
| 130 | + if (! empty($extension)) { |
|
| 131 | + $extension = ltrim($extension, '.'); |
|
| 132 | + |
|
| 133 | + if ($uriExtension === $extension) { |
|
| 134 | + $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
| 135 | + } |
|
| 136 | + } else { |
|
| 137 | + $extension = URIHelper::containsExtension($criterion); |
|
| 138 | + if (!empty($extension)) { |
|
| 139 | + $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
| 144 | + |
|
| 145 | + return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
| 146 | + ? "{$criterion}(?:\..*?)$" |
|
| 147 | + : "{$criterion}\.{$extension}$"; |
|
| 148 | + }, $criteria)[0]; |
|
| 149 | + |
|
| 150 | + return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the path pattern to pass to the file finder. |
|
| 155 | + * |
|
| 156 | + * @since 0.4.7 |
|
| 157 | + * |
|
| 158 | + * @param string $relativePath Relative path that was included in the |
|
| 159 | + * criterion. |
|
| 160 | + * @return string Path pattern to pass to the file finder. |
|
| 161 | + */ |
|
| 162 | + protected function getPathPattern(string $relativePath): string |
|
| 163 | + { |
|
| 164 | + if (empty($relativePath)) { |
|
| 165 | + return $this->path; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/'); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Get the relative path that is included in the criterion. |
|
| 173 | + * |
|
| 174 | + * @since 0.4.7 |
|
| 175 | + * |
|
| 176 | + * @param array $criteria Criteria to extract the relative path from. |
|
| 177 | + * @return string Relative path included in the criterion. |
|
| 178 | + */ |
|
| 179 | + protected function getRelativePath($criteria): string |
|
| 180 | + { |
|
| 181 | + $criterion = current($criteria); |
|
| 182 | + $components = explode('/', $criterion); |
|
| 183 | + |
|
| 184 | + if (count($components) < 2) { |
|
| 185 | + return ''; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return implode('/', array_slice($components, 0, -1)); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Validate the extensions and return a collection. |
|
| 193 | + * |
|
| 194 | + * @since 0.1.1 |
|
| 195 | + * |
|
| 196 | + * @param Extensions|array|string|null $extensions Extensions to validate. |
|
| 197 | + * |
|
| 198 | + * @return Extensions Validated extensions collection. |
|
| 199 | + */ |
|
| 200 | + protected function validateExtensions($extensions): Extensions |
|
| 201 | + { |
|
| 202 | + if (empty($extensions)) { |
|
| 203 | + $extensions = new Extensions(['']); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if (! $extensions instanceof Extensions) { |
|
| 207 | + $extensions = new Extensions((array)$extensions); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + return $extensions; |
|
| 211 | + } |
|
| 212 | 212 | } |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | $finder = new Finder(); |
| 96 | 96 | |
| 97 | 97 | try { |
| 98 | - bdump( $this->getPathPattern($this->getRelativePath($criteria)) ); |
|
| 98 | + bdump($this->getPathPattern($this->getRelativePath($criteria))); |
|
| 99 | 99 | $finder->files() |
| 100 | 100 | ->name($this->getNamePattern($criteria, $extension)) |
| 101 | 101 | ->in($this->getPathPattern($this->getRelativePath($criteria))); |
@@ -125,18 +125,18 @@ discard block |
||
| 125 | 125 | { |
| 126 | 126 | $names = []; |
| 127 | 127 | |
| 128 | - $names[] = array_map(function ($criterion) use ($extension) { |
|
| 128 | + $names[] = array_map(function($criterion) use ($extension) { |
|
| 129 | 129 | $uriExtension = URIHelper::containsExtension($criterion); |
| 130 | - if (! empty($extension)) { |
|
| 130 | + if ( ! empty($extension)) { |
|
| 131 | 131 | $extension = ltrim($extension, '.'); |
| 132 | 132 | |
| 133 | 133 | if ($uriExtension === $extension) { |
| 134 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
| 134 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
| 135 | 135 | } |
| 136 | 136 | } else { |
| 137 | 137 | $extension = URIHelper::containsExtension($criterion); |
| 138 | - if (!empty($extension)) { |
|
| 139 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
| 138 | + if ( ! empty($extension)) { |
|
| 139 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | 142 | |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | : "{$criterion}\.{$extension}$"; |
| 148 | 148 | }, $criteria)[0]; |
| 149 | 149 | |
| 150 | - return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
| 150 | + return chr(1).implode('|', array_unique($names)).chr(1); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | return $this->path; |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/'); |
|
| 168 | + return rtrim($this->path, '/').'/'.ltrim($relativePath, '/'); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /** |
@@ -203,8 +203,8 @@ discard block |
||
| 203 | 203 | $extensions = new Extensions(['']); |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - if (! $extensions instanceof Extensions) { |
|
| 207 | - $extensions = new Extensions((array)$extensions); |
|
| 206 | + if ( ! $extensions instanceof Extensions) { |
|
| 207 | + $extensions = new Extensions((array) $extensions); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | return $extensions; |