@@ -24,131 +24,131 @@ |
||
| 24 | 24 | class FilesystemLocation implements LocationInterface |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Path that this location points to. |
|
| 29 | - * |
|
| 30 | - * @since 0.1.0 |
|
| 31 | - * |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - protected $path; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Extensions that this location can accept. |
|
| 38 | - * |
|
| 39 | - * @since 0.1.0 |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $extensions; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Instantiate a FilesystemLocation object. |
|
| 47 | - * |
|
| 48 | - * @since 0.1.0 |
|
| 49 | - * |
|
| 50 | - * @param string $path Path that this location points to. |
|
| 51 | - * @param array $extensions Array of extensions that this location can accept. |
|
| 52 | - */ |
|
| 53 | - public function __construct($path, array $extensions = []) |
|
| 54 | - { |
|
| 55 | - $this->path = $path; |
|
| 56 | - $this->extensions = array_merge($extensions, ['']); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Get the first URI that matches the given criteria. |
|
| 61 | - * |
|
| 62 | - * @since 0.1.0 |
|
| 63 | - * |
|
| 64 | - * @param array $criteria Criteria to match. |
|
| 65 | - * |
|
| 66 | - * @return string|false URI that matches the criteria or false if none found. |
|
| 67 | - */ |
|
| 68 | - public function getURI(array $criteria) |
|
| 69 | - { |
|
| 70 | - foreach ($criteria as $entry) { |
|
| 71 | - if ($uri = $this->transform($entry, true)) { |
|
| 72 | - return $uri; |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Get all URIs that match the given criteria. |
|
| 81 | - * |
|
| 82 | - * @since 0.1.1 |
|
| 83 | - * |
|
| 84 | - * @param array $criteria Criteria to match. |
|
| 85 | - * |
|
| 86 | - * @return array URIs that match the criteria or empty array if none found. |
|
| 87 | - */ |
|
| 88 | - public function getURIs(array $criteria) |
|
| 89 | - { |
|
| 90 | - $uris = []; |
|
| 91 | - |
|
| 92 | - foreach ($criteria as $entry) { |
|
| 93 | - if ($uri = $this->transform($entry, false)) { |
|
| 94 | - $uris = array_merge($uris, (array)$uri); |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - return $uris; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Try to transform the entry into possible URIs. |
|
| 103 | - * |
|
| 104 | - * @since 0.1.0 |
|
| 105 | - * |
|
| 106 | - * @param string $entry Entry to transform. |
|
| 107 | - * @param bool $firstOnly Return the first result only. |
|
| 108 | - * |
|
| 109 | - * @return array|string|false If $firstOnly is true, returns a string with the URI of the view, or false if none |
|
| 110 | - * found. |
|
| 111 | - * If $firstOnly is false, returns an array with all matching URIs, or an empty array if |
|
| 112 | - * none found. |
|
| 113 | - */ |
|
| 114 | - protected function transform($entry, $firstOnly = true) |
|
| 115 | - { |
|
| 116 | - $uris = []; |
|
| 117 | - |
|
| 118 | - try { |
|
| 119 | - foreach ($this->getVariants($entry) as $uri) { |
|
| 120 | - if (is_readable($uri)) { |
|
| 121 | - if ($firstOnly) { |
|
| 122 | - return $uri; |
|
| 123 | - } |
|
| 124 | - $uris [] = $uri; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } catch (Exception $exception) { |
|
| 128 | - // Fail silently. |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return $firstOnly ? false : $uris; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Get the individual variants that could be matched for the location. |
|
| 136 | - * |
|
| 137 | - * @since 0.1.1 |
|
| 138 | - * |
|
| 139 | - * @param string $entry Entry to get the variants for. |
|
| 140 | - * |
|
| 141 | - * @return array Array of variants to check. |
|
| 142 | - */ |
|
| 143 | - protected function getVariants($entry) |
|
| 144 | - { |
|
| 145 | - $variants = []; |
|
| 146 | - |
|
| 147 | - array_map(function ($extension) use ($entry, &$variants) { |
|
| 148 | - $variants[] = $entry . $extension; |
|
| 149 | - $variants[] = $this->path . DIRECTORY_SEPARATOR . $entry . $extension; |
|
| 150 | - }, $this->extensions); |
|
| 151 | - |
|
| 152 | - return $variants; |
|
| 153 | - } |
|
| 27 | + /** |
|
| 28 | + * Path that this location points to. |
|
| 29 | + * |
|
| 30 | + * @since 0.1.0 |
|
| 31 | + * |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + protected $path; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Extensions that this location can accept. |
|
| 38 | + * |
|
| 39 | + * @since 0.1.0 |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $extensions; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Instantiate a FilesystemLocation object. |
|
| 47 | + * |
|
| 48 | + * @since 0.1.0 |
|
| 49 | + * |
|
| 50 | + * @param string $path Path that this location points to. |
|
| 51 | + * @param array $extensions Array of extensions that this location can accept. |
|
| 52 | + */ |
|
| 53 | + public function __construct($path, array $extensions = []) |
|
| 54 | + { |
|
| 55 | + $this->path = $path; |
|
| 56 | + $this->extensions = array_merge($extensions, ['']); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Get the first URI that matches the given criteria. |
|
| 61 | + * |
|
| 62 | + * @since 0.1.0 |
|
| 63 | + * |
|
| 64 | + * @param array $criteria Criteria to match. |
|
| 65 | + * |
|
| 66 | + * @return string|false URI that matches the criteria or false if none found. |
|
| 67 | + */ |
|
| 68 | + public function getURI(array $criteria) |
|
| 69 | + { |
|
| 70 | + foreach ($criteria as $entry) { |
|
| 71 | + if ($uri = $this->transform($entry, true)) { |
|
| 72 | + return $uri; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Get all URIs that match the given criteria. |
|
| 81 | + * |
|
| 82 | + * @since 0.1.1 |
|
| 83 | + * |
|
| 84 | + * @param array $criteria Criteria to match. |
|
| 85 | + * |
|
| 86 | + * @return array URIs that match the criteria or empty array if none found. |
|
| 87 | + */ |
|
| 88 | + public function getURIs(array $criteria) |
|
| 89 | + { |
|
| 90 | + $uris = []; |
|
| 91 | + |
|
| 92 | + foreach ($criteria as $entry) { |
|
| 93 | + if ($uri = $this->transform($entry, false)) { |
|
| 94 | + $uris = array_merge($uris, (array)$uri); |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + return $uris; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Try to transform the entry into possible URIs. |
|
| 103 | + * |
|
| 104 | + * @since 0.1.0 |
|
| 105 | + * |
|
| 106 | + * @param string $entry Entry to transform. |
|
| 107 | + * @param bool $firstOnly Return the first result only. |
|
| 108 | + * |
|
| 109 | + * @return array|string|false If $firstOnly is true, returns a string with the URI of the view, or false if none |
|
| 110 | + * found. |
|
| 111 | + * If $firstOnly is false, returns an array with all matching URIs, or an empty array if |
|
| 112 | + * none found. |
|
| 113 | + */ |
|
| 114 | + protected function transform($entry, $firstOnly = true) |
|
| 115 | + { |
|
| 116 | + $uris = []; |
|
| 117 | + |
|
| 118 | + try { |
|
| 119 | + foreach ($this->getVariants($entry) as $uri) { |
|
| 120 | + if (is_readable($uri)) { |
|
| 121 | + if ($firstOnly) { |
|
| 122 | + return $uri; |
|
| 123 | + } |
|
| 124 | + $uris [] = $uri; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } catch (Exception $exception) { |
|
| 128 | + // Fail silently. |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return $firstOnly ? false : $uris; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Get the individual variants that could be matched for the location. |
|
| 136 | + * |
|
| 137 | + * @since 0.1.1 |
|
| 138 | + * |
|
| 139 | + * @param string $entry Entry to get the variants for. |
|
| 140 | + * |
|
| 141 | + * @return array Array of variants to check. |
|
| 142 | + */ |
|
| 143 | + protected function getVariants($entry) |
|
| 144 | + { |
|
| 145 | + $variants = []; |
|
| 146 | + |
|
| 147 | + array_map(function ($extension) use ($entry, &$variants) { |
|
| 148 | + $variants[] = $entry . $extension; |
|
| 149 | + $variants[] = $this->path . DIRECTORY_SEPARATOR . $entry . $extension; |
|
| 150 | + }, $this->extensions); |
|
| 151 | + |
|
| 152 | + return $variants; |
|
| 153 | + } |
|
| 154 | 154 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | |
| 92 | 92 | foreach ($criteria as $entry) { |
| 93 | 93 | if ($uri = $this->transform($entry, false)) { |
| 94 | - $uris = array_merge($uris, (array)$uri); |
|
| 94 | + $uris = array_merge($uris, (array) $uri); |
|
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | 97 | |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | { |
| 145 | 145 | $variants = []; |
| 146 | 146 | |
| 147 | - array_map(function ($extension) use ($entry, &$variants) { |
|
| 148 | - $variants[] = $entry . $extension; |
|
| 149 | - $variants[] = $this->path . DIRECTORY_SEPARATOR . $entry . $extension; |
|
| 147 | + array_map(function($extension) use ($entry, &$variants) { |
|
| 148 | + $variants[] = $entry.$extension; |
|
| 149 | + $variants[] = $this->path.DIRECTORY_SEPARATOR.$entry.$extension; |
|
| 150 | 150 | }, $this->extensions); |
| 151 | 151 | |
| 152 | 152 | return $variants; |