@@ -27,214 +27,214 @@ |
||
| 27 | 27 | abstract class AbstractFinder implements FinderInterface |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - use ConfigTrait; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Findable collection that the Finder can iterate through to find a match. |
|
| 34 | - * |
|
| 35 | - * @since 0.1.0 |
|
| 36 | - * |
|
| 37 | - * @var FindableCollection |
|
| 38 | - */ |
|
| 39 | - protected $findables; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * NullObject that is returned if the Finder could not find a match. |
|
| 43 | - * |
|
| 44 | - * @since 0.1.0 |
|
| 45 | - * |
|
| 46 | - * @var NullObject |
|
| 47 | - */ |
|
| 48 | - protected $nullObject; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Instantiate an AbstractFinder object. |
|
| 52 | - * |
|
| 53 | - * @since 0.1.0 |
|
| 54 | - * |
|
| 55 | - * @param ConfigInterface $config Configuration of the AbstractFinder. |
|
| 56 | - * |
|
| 57 | - * @throws FailedToProcessConfigException If the config could not be processed. |
|
| 58 | - */ |
|
| 59 | - public function __construct(ConfigInterface $config) |
|
| 60 | - { |
|
| 61 | - $this->processConfig($config); |
|
| 62 | - $this->findables = new FindableCollection(); |
|
| 63 | - $this->registerFindables($this->config); |
|
| 64 | - $this->registerNullObject($this->config); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Register the Findables defined in the given configuration. |
|
| 69 | - * |
|
| 70 | - * @since 0.1.0 |
|
| 71 | - * |
|
| 72 | - * @param ConfigInterface $config Configuration to register the Findables from. |
|
| 73 | - */ |
|
| 74 | - public function registerFindables(ConfigInterface $config) |
|
| 75 | - { |
|
| 76 | - foreach ($config->getKey($this->getFindablesConfigKey()) as $findableKey => $findableObject) { |
|
| 77 | - $this->findables->set($findableKey, $findableObject); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Register the NullObject defined in the given configuration. |
|
| 83 | - * |
|
| 84 | - * @since 0.1.0 |
|
| 85 | - * |
|
| 86 | - * @param ConfigInterface $config Configuration to register the NullObject from. |
|
| 87 | - */ |
|
| 88 | - public function registerNullObject(ConfigInterface $config) |
|
| 89 | - { |
|
| 90 | - $this->nullObject = $config->getKey($this->getNullObjectConfigKey()); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get the NullObject. |
|
| 95 | - * |
|
| 96 | - * @since 0.1.1 |
|
| 97 | - * |
|
| 98 | - * @return NullObject NullObject for the current Finder. |
|
| 99 | - */ |
|
| 100 | - public function getNullObject() |
|
| 101 | - { |
|
| 102 | - $this->initializeNullObject(); |
|
| 103 | - |
|
| 104 | - return $this->nullObject; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the config key for the Findables definitions. |
|
| 109 | - * |
|
| 110 | - * @since 0.1.0 |
|
| 111 | - * |
|
| 112 | - * @return string Config key use to define the Findables. |
|
| 113 | - */ |
|
| 114 | - protected function getFindablesConfigKey() |
|
| 115 | - { |
|
| 116 | - return 'Findables'; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Get the config key for the NullObject definitions. |
|
| 121 | - * |
|
| 122 | - * @since 0.1.0 |
|
| 123 | - * |
|
| 124 | - * @return string Config key use to define the NullObject. |
|
| 125 | - */ |
|
| 126 | - protected function getNullObjectConfigKey() |
|
| 127 | - { |
|
| 128 | - return 'NullObject'; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Initialize the NullObject. |
|
| 133 | - * |
|
| 134 | - * @param mixed $arguments Optional. Arguments to use. |
|
| 135 | - * |
|
| 136 | - * @since 0.1.1 |
|
| 137 | - */ |
|
| 138 | - protected function initializeNullObject($arguments = null) |
|
| 139 | - { |
|
| 140 | - if (! is_object($this->nullObject)) { |
|
| 141 | - $this->nullObject = new $this->nullObject(...$arguments); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Initialize the Findables that can be iterated. |
|
| 147 | - * |
|
| 148 | - * @param mixed $arguments Optional. Arguments to use. |
|
| 149 | - * |
|
| 150 | - * @since 0.1.0 |
|
| 151 | - * |
|
| 152 | - */ |
|
| 153 | - protected function initializeFindables($arguments = null) |
|
| 154 | - { |
|
| 155 | - $this->findables = $this->findables->map(function ($findable) use ($arguments) { |
|
| 156 | - return $this->initializeFindable($findable, $arguments); |
|
| 157 | - }); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Initialize a single findable by instantiating class name strings and calling closures. |
|
| 162 | - * |
|
| 163 | - * @since 0.1.0 |
|
| 164 | - * |
|
| 165 | - * @param mixed $findable Findable to instantiate. |
|
| 166 | - * @param mixed $arguments Optional. Arguments to use. |
|
| 167 | - * |
|
| 168 | - * @return Findable Instantiated findable. |
|
| 169 | - * @throws FailedToInstantiateFindableException If the findable could not be instantiated. |
|
| 170 | - */ |
|
| 171 | - protected function initializeFindable($findable, $arguments = null) |
|
| 172 | - { |
|
| 173 | - $findable = $this->maybeInstantiateFindable($findable, $arguments); |
|
| 174 | - |
|
| 175 | - if (! $findable instanceof Findable) { |
|
| 176 | - throw new FailedToInstantiateFindableException( |
|
| 177 | - sprintf( |
|
| 178 | - _('Could not instantiate Findable "%s".'), |
|
| 179 | - serialize($findable) |
|
| 180 | - ), |
|
| 181 | - (array)$arguments |
|
| 182 | - ); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - return $findable; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Maybe instantiate a Findable if it is not yet an object. |
|
| 190 | - * |
|
| 191 | - * @since 0.1.1 |
|
| 192 | - * |
|
| 193 | - * @param mixed $findable Findable to instantiate. |
|
| 194 | - * @param mixed $arguments Optional. Arguments to use. |
|
| 195 | - * |
|
| 196 | - * @return Findable Instantiated findable. |
|
| 197 | - */ |
|
| 198 | - protected function maybeInstantiateFindable($findable, $arguments = null) |
|
| 199 | - { |
|
| 200 | - if (is_string($findable)) { |
|
| 201 | - $findable = $this->instantiateFindableFromString($findable, $arguments); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - if (is_callable($findable)) { |
|
| 205 | - $findable = $this->instantiateFindableFromCallable($findable, $arguments); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - return $findable; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Instantiate a Findable from a string. |
|
| 213 | - * |
|
| 214 | - * @since 0.1.1 |
|
| 215 | - * |
|
| 216 | - * @param string $string String to use for instantiation. |
|
| 217 | - * @param mixed $arguments Optional. Arguments to use for instantiation. |
|
| 218 | - * |
|
| 219 | - * @return mixed |
|
| 220 | - */ |
|
| 221 | - protected function instantiateFindableFromString($string, $arguments = null) |
|
| 222 | - { |
|
| 223 | - return new $string(...$arguments); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Instantiate a Findable from a callable. |
|
| 228 | - * |
|
| 229 | - * @since 0.1.1 |
|
| 230 | - * |
|
| 231 | - * @param callable $callable Callable to use for instantiation. |
|
| 232 | - * @param mixed $arguments Optional. Arguments to use for instantiation. |
|
| 233 | - * |
|
| 234 | - * @return mixed |
|
| 235 | - */ |
|
| 236 | - protected function instantiateFindableFromCallable($callable, $arguments = null) |
|
| 237 | - { |
|
| 238 | - return $callable(...$arguments); |
|
| 239 | - } |
|
| 30 | + use ConfigTrait; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Findable collection that the Finder can iterate through to find a match. |
|
| 34 | + * |
|
| 35 | + * @since 0.1.0 |
|
| 36 | + * |
|
| 37 | + * @var FindableCollection |
|
| 38 | + */ |
|
| 39 | + protected $findables; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * NullObject that is returned if the Finder could not find a match. |
|
| 43 | + * |
|
| 44 | + * @since 0.1.0 |
|
| 45 | + * |
|
| 46 | + * @var NullObject |
|
| 47 | + */ |
|
| 48 | + protected $nullObject; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Instantiate an AbstractFinder object. |
|
| 52 | + * |
|
| 53 | + * @since 0.1.0 |
|
| 54 | + * |
|
| 55 | + * @param ConfigInterface $config Configuration of the AbstractFinder. |
|
| 56 | + * |
|
| 57 | + * @throws FailedToProcessConfigException If the config could not be processed. |
|
| 58 | + */ |
|
| 59 | + public function __construct(ConfigInterface $config) |
|
| 60 | + { |
|
| 61 | + $this->processConfig($config); |
|
| 62 | + $this->findables = new FindableCollection(); |
|
| 63 | + $this->registerFindables($this->config); |
|
| 64 | + $this->registerNullObject($this->config); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Register the Findables defined in the given configuration. |
|
| 69 | + * |
|
| 70 | + * @since 0.1.0 |
|
| 71 | + * |
|
| 72 | + * @param ConfigInterface $config Configuration to register the Findables from. |
|
| 73 | + */ |
|
| 74 | + public function registerFindables(ConfigInterface $config) |
|
| 75 | + { |
|
| 76 | + foreach ($config->getKey($this->getFindablesConfigKey()) as $findableKey => $findableObject) { |
|
| 77 | + $this->findables->set($findableKey, $findableObject); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Register the NullObject defined in the given configuration. |
|
| 83 | + * |
|
| 84 | + * @since 0.1.0 |
|
| 85 | + * |
|
| 86 | + * @param ConfigInterface $config Configuration to register the NullObject from. |
|
| 87 | + */ |
|
| 88 | + public function registerNullObject(ConfigInterface $config) |
|
| 89 | + { |
|
| 90 | + $this->nullObject = $config->getKey($this->getNullObjectConfigKey()); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get the NullObject. |
|
| 95 | + * |
|
| 96 | + * @since 0.1.1 |
|
| 97 | + * |
|
| 98 | + * @return NullObject NullObject for the current Finder. |
|
| 99 | + */ |
|
| 100 | + public function getNullObject() |
|
| 101 | + { |
|
| 102 | + $this->initializeNullObject(); |
|
| 103 | + |
|
| 104 | + return $this->nullObject; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the config key for the Findables definitions. |
|
| 109 | + * |
|
| 110 | + * @since 0.1.0 |
|
| 111 | + * |
|
| 112 | + * @return string Config key use to define the Findables. |
|
| 113 | + */ |
|
| 114 | + protected function getFindablesConfigKey() |
|
| 115 | + { |
|
| 116 | + return 'Findables'; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Get the config key for the NullObject definitions. |
|
| 121 | + * |
|
| 122 | + * @since 0.1.0 |
|
| 123 | + * |
|
| 124 | + * @return string Config key use to define the NullObject. |
|
| 125 | + */ |
|
| 126 | + protected function getNullObjectConfigKey() |
|
| 127 | + { |
|
| 128 | + return 'NullObject'; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Initialize the NullObject. |
|
| 133 | + * |
|
| 134 | + * @param mixed $arguments Optional. Arguments to use. |
|
| 135 | + * |
|
| 136 | + * @since 0.1.1 |
|
| 137 | + */ |
|
| 138 | + protected function initializeNullObject($arguments = null) |
|
| 139 | + { |
|
| 140 | + if (! is_object($this->nullObject)) { |
|
| 141 | + $this->nullObject = new $this->nullObject(...$arguments); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Initialize the Findables that can be iterated. |
|
| 147 | + * |
|
| 148 | + * @param mixed $arguments Optional. Arguments to use. |
|
| 149 | + * |
|
| 150 | + * @since 0.1.0 |
|
| 151 | + * |
|
| 152 | + */ |
|
| 153 | + protected function initializeFindables($arguments = null) |
|
| 154 | + { |
|
| 155 | + $this->findables = $this->findables->map(function ($findable) use ($arguments) { |
|
| 156 | + return $this->initializeFindable($findable, $arguments); |
|
| 157 | + }); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Initialize a single findable by instantiating class name strings and calling closures. |
|
| 162 | + * |
|
| 163 | + * @since 0.1.0 |
|
| 164 | + * |
|
| 165 | + * @param mixed $findable Findable to instantiate. |
|
| 166 | + * @param mixed $arguments Optional. Arguments to use. |
|
| 167 | + * |
|
| 168 | + * @return Findable Instantiated findable. |
|
| 169 | + * @throws FailedToInstantiateFindableException If the findable could not be instantiated. |
|
| 170 | + */ |
|
| 171 | + protected function initializeFindable($findable, $arguments = null) |
|
| 172 | + { |
|
| 173 | + $findable = $this->maybeInstantiateFindable($findable, $arguments); |
|
| 174 | + |
|
| 175 | + if (! $findable instanceof Findable) { |
|
| 176 | + throw new FailedToInstantiateFindableException( |
|
| 177 | + sprintf( |
|
| 178 | + _('Could not instantiate Findable "%s".'), |
|
| 179 | + serialize($findable) |
|
| 180 | + ), |
|
| 181 | + (array)$arguments |
|
| 182 | + ); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + return $findable; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Maybe instantiate a Findable if it is not yet an object. |
|
| 190 | + * |
|
| 191 | + * @since 0.1.1 |
|
| 192 | + * |
|
| 193 | + * @param mixed $findable Findable to instantiate. |
|
| 194 | + * @param mixed $arguments Optional. Arguments to use. |
|
| 195 | + * |
|
| 196 | + * @return Findable Instantiated findable. |
|
| 197 | + */ |
|
| 198 | + protected function maybeInstantiateFindable($findable, $arguments = null) |
|
| 199 | + { |
|
| 200 | + if (is_string($findable)) { |
|
| 201 | + $findable = $this->instantiateFindableFromString($findable, $arguments); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + if (is_callable($findable)) { |
|
| 205 | + $findable = $this->instantiateFindableFromCallable($findable, $arguments); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + return $findable; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Instantiate a Findable from a string. |
|
| 213 | + * |
|
| 214 | + * @since 0.1.1 |
|
| 215 | + * |
|
| 216 | + * @param string $string String to use for instantiation. |
|
| 217 | + * @param mixed $arguments Optional. Arguments to use for instantiation. |
|
| 218 | + * |
|
| 219 | + * @return mixed |
|
| 220 | + */ |
|
| 221 | + protected function instantiateFindableFromString($string, $arguments = null) |
|
| 222 | + { |
|
| 223 | + return new $string(...$arguments); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Instantiate a Findable from a callable. |
|
| 228 | + * |
|
| 229 | + * @since 0.1.1 |
|
| 230 | + * |
|
| 231 | + * @param callable $callable Callable to use for instantiation. |
|
| 232 | + * @param mixed $arguments Optional. Arguments to use for instantiation. |
|
| 233 | + * |
|
| 234 | + * @return mixed |
|
| 235 | + */ |
|
| 236 | + protected function instantiateFindableFromCallable($callable, $arguments = null) |
|
| 237 | + { |
|
| 238 | + return $callable(...$arguments); |
|
| 239 | + } |
|
| 240 | 240 | } |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | */ |
| 138 | 138 | protected function initializeNullObject($arguments = null) |
| 139 | 139 | { |
| 140 | - if (! is_object($this->nullObject)) { |
|
| 140 | + if ( ! is_object($this->nullObject)) { |
|
| 141 | 141 | $this->nullObject = new $this->nullObject(...$arguments); |
| 142 | 142 | } |
| 143 | 143 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | protected function initializeFindables($arguments = null) |
| 154 | 154 | { |
| 155 | - $this->findables = $this->findables->map(function ($findable) use ($arguments) { |
|
| 155 | + $this->findables = $this->findables->map(function($findable) use ($arguments) { |
|
| 156 | 156 | return $this->initializeFindable($findable, $arguments); |
| 157 | 157 | }); |
| 158 | 158 | } |
@@ -172,13 +172,13 @@ discard block |
||
| 172 | 172 | { |
| 173 | 173 | $findable = $this->maybeInstantiateFindable($findable, $arguments); |
| 174 | 174 | |
| 175 | - if (! $findable instanceof Findable) { |
|
| 175 | + if ( ! $findable instanceof Findable) { |
|
| 176 | 176 | throw new FailedToInstantiateFindableException( |
| 177 | 177 | sprintf( |
| 178 | 178 | _('Could not instantiate Findable "%s".'), |
| 179 | 179 | serialize($findable) |
| 180 | 180 | ), |
| 181 | - (array)$arguments |
|
| 181 | + (array) $arguments |
|
| 182 | 182 | ); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -24,39 +24,39 @@ |
||
| 24 | 24 | class EngineFinder extends AbstractFinder |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Find a result based on a specific criteria. |
|
| 29 | - * |
|
| 30 | - * @since 0.1.0 |
|
| 31 | - * |
|
| 32 | - * @param array $criteria Criteria to search for. |
|
| 33 | - * |
|
| 34 | - * @return EngineInterface Result of the search. |
|
| 35 | - */ |
|
| 36 | - public function find(array $criteria) |
|
| 37 | - { |
|
| 38 | - $this->initializeFindables(); |
|
| 27 | + /** |
|
| 28 | + * Find a result based on a specific criteria. |
|
| 29 | + * |
|
| 30 | + * @since 0.1.0 |
|
| 31 | + * |
|
| 32 | + * @param array $criteria Criteria to search for. |
|
| 33 | + * |
|
| 34 | + * @return EngineInterface Result of the search. |
|
| 35 | + */ |
|
| 36 | + public function find(array $criteria) |
|
| 37 | + { |
|
| 38 | + $this->initializeFindables(); |
|
| 39 | 39 | |
| 40 | - foreach ($criteria as $entry) { |
|
| 41 | - foreach ($this->findables as $engine) { |
|
| 42 | - if ($engine->canHandle($entry)) { |
|
| 43 | - return $engine; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - } |
|
| 40 | + foreach ($criteria as $entry) { |
|
| 41 | + foreach ($this->findables as $engine) { |
|
| 42 | + if ($engine->canHandle($entry)) { |
|
| 43 | + return $engine; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - return $this->getNullObject(); |
|
| 49 | - } |
|
| 48 | + return $this->getNullObject(); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Get the config key for the Findables definitions. |
|
| 53 | - * |
|
| 54 | - * @since 0.1.0 |
|
| 55 | - * |
|
| 56 | - * @return string Config key use to define the Findables. |
|
| 57 | - */ |
|
| 58 | - protected function getFindablesConfigKey() |
|
| 59 | - { |
|
| 60 | - return 'Engines'; |
|
| 61 | - } |
|
| 51 | + /** |
|
| 52 | + * Get the config key for the Findables definitions. |
|
| 53 | + * |
|
| 54 | + * @since 0.1.0 |
|
| 55 | + * |
|
| 56 | + * @return string Config key use to define the Findables. |
|
| 57 | + */ |
|
| 58 | + protected function getFindablesConfigKey() |
|
| 59 | + { |
|
| 60 | + return 'Engines'; |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -25,42 +25,42 @@ |
||
| 25 | 25 | class ViewFinder extends AbstractFinder |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Find a result based on a specific criteria. |
|
| 30 | - * |
|
| 31 | - * @since 0.1.0 |
|
| 32 | - * |
|
| 33 | - * @param array $criteria Criteria to search for. |
|
| 34 | - * @param EngineInterface|null $engine Optional. Engine to use with the view. |
|
| 35 | - * |
|
| 36 | - * @return ViewInterface View that was found. |
|
| 37 | - */ |
|
| 38 | - public function find(array $criteria, EngineInterface $engine = null) |
|
| 39 | - { |
|
| 40 | - $uri = $criteria[0]; |
|
| 28 | + /** |
|
| 29 | + * Find a result based on a specific criteria. |
|
| 30 | + * |
|
| 31 | + * @since 0.1.0 |
|
| 32 | + * |
|
| 33 | + * @param array $criteria Criteria to search for. |
|
| 34 | + * @param EngineInterface|null $engine Optional. Engine to use with the view. |
|
| 35 | + * |
|
| 36 | + * @return ViewInterface View that was found. |
|
| 37 | + */ |
|
| 38 | + public function find(array $criteria, EngineInterface $engine = null) |
|
| 39 | + { |
|
| 40 | + $uri = $criteria[0]; |
|
| 41 | 41 | |
| 42 | - $this->initializeFindables([$uri, $engine]); |
|
| 42 | + $this->initializeFindables([$uri, $engine]); |
|
| 43 | 43 | |
| 44 | - foreach ($criteria as $entry) { |
|
| 45 | - foreach ($this->findables as $viewObject) { |
|
| 46 | - if ($viewObject->canHandle($entry)) { |
|
| 47 | - return $viewObject; |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - } |
|
| 44 | + foreach ($criteria as $entry) { |
|
| 45 | + foreach ($this->findables as $viewObject) { |
|
| 46 | + if ($viewObject->canHandle($entry)) { |
|
| 47 | + return $viewObject; |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - return $this->getNullObject(); |
|
| 53 | - } |
|
| 52 | + return $this->getNullObject(); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get the config key for the Findables definitions. |
|
| 57 | - * |
|
| 58 | - * @since 0.1.0 |
|
| 59 | - * |
|
| 60 | - * @return string Config key use to define the Findables. |
|
| 61 | - */ |
|
| 62 | - protected function getFindablesConfigKey() |
|
| 63 | - { |
|
| 64 | - return 'Views'; |
|
| 65 | - } |
|
| 55 | + /** |
|
| 56 | + * Get the config key for the Findables definitions. |
|
| 57 | + * |
|
| 58 | + * @since 0.1.0 |
|
| 59 | + * |
|
| 60 | + * @return string Config key use to define the Findables. |
|
| 61 | + */ |
|
| 62 | + protected function getFindablesConfigKey() |
|
| 63 | + { |
|
| 64 | + return 'Views'; |
|
| 65 | + } |
|
| 66 | 66 | } |
@@ -34,227 +34,227 @@ |
||
| 34 | 34 | class ViewBuilder |
| 35 | 35 | { |
| 36 | 36 | |
| 37 | - use ConfigTrait; |
|
| 37 | + use ConfigTrait; |
|
| 38 | 38 | |
| 39 | - const ENGINE_FINDER_KEY = 'EngineFinder'; |
|
| 40 | - const VIEW_FINDER_KEY = 'ViewFinder'; |
|
| 39 | + const ENGINE_FINDER_KEY = 'EngineFinder'; |
|
| 40 | + const VIEW_FINDER_KEY = 'ViewFinder'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * ViewFinder instance. |
|
| 44 | - * |
|
| 45 | - * @since 0.1.0 |
|
| 46 | - * |
|
| 47 | - * @var ViewFinderInterface |
|
| 48 | - */ |
|
| 49 | - protected $viewFinder; |
|
| 42 | + /** |
|
| 43 | + * ViewFinder instance. |
|
| 44 | + * |
|
| 45 | + * @since 0.1.0 |
|
| 46 | + * |
|
| 47 | + * @var ViewFinderInterface |
|
| 48 | + */ |
|
| 49 | + protected $viewFinder; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * EngineFinder instance. |
|
| 53 | - * |
|
| 54 | - * @since 0.1.0 |
|
| 55 | - * |
|
| 56 | - * @var EngineFinderInterface |
|
| 57 | - */ |
|
| 58 | - protected $engineFinder; |
|
| 51 | + /** |
|
| 52 | + * EngineFinder instance. |
|
| 53 | + * |
|
| 54 | + * @since 0.1.0 |
|
| 55 | + * |
|
| 56 | + * @var EngineFinderInterface |
|
| 57 | + */ |
|
| 58 | + protected $engineFinder; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Locations to scan for views. |
|
| 62 | - * |
|
| 63 | - * @since 0.1.0 |
|
| 64 | - * |
|
| 65 | - * @var LocationCollection |
|
| 66 | - */ |
|
| 67 | - protected $locations; |
|
| 60 | + /** |
|
| 61 | + * Locations to scan for views. |
|
| 62 | + * |
|
| 63 | + * @since 0.1.0 |
|
| 64 | + * |
|
| 65 | + * @var LocationCollection |
|
| 66 | + */ |
|
| 67 | + protected $locations; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Instantiate a ViewBuilder object. |
|
| 71 | - * |
|
| 72 | - * @since 0.1.0 |
|
| 73 | - * |
|
| 74 | - * @param ConfigInterface $config Configuration settings. |
|
| 75 | - * @param ViewFinderInterface|null $viewFinder ViewFinder instance. |
|
| 76 | - * @param EngineFinderInterface|null $engineFinder EngineFinder instance. |
|
| 77 | - * |
|
| 78 | - * @throws FailedToProcessConfigException If the config could not be processed. |
|
| 79 | - */ |
|
| 80 | - public function __construct( |
|
| 81 | - ConfigInterface $config, |
|
| 82 | - ViewFinderInterface $viewFinder = null, |
|
| 83 | - EngineFinderInterface $engineFinder = null |
|
| 84 | - ) { |
|
| 85 | - $this->processConfig($config); |
|
| 86 | - $this->viewFinder = $viewFinder; |
|
| 87 | - $this->engineFinder = $engineFinder; |
|
| 88 | - $this->locations = new LocationCollection(); |
|
| 89 | - } |
|
| 69 | + /** |
|
| 70 | + * Instantiate a ViewBuilder object. |
|
| 71 | + * |
|
| 72 | + * @since 0.1.0 |
|
| 73 | + * |
|
| 74 | + * @param ConfigInterface $config Configuration settings. |
|
| 75 | + * @param ViewFinderInterface|null $viewFinder ViewFinder instance. |
|
| 76 | + * @param EngineFinderInterface|null $engineFinder EngineFinder instance. |
|
| 77 | + * |
|
| 78 | + * @throws FailedToProcessConfigException If the config could not be processed. |
|
| 79 | + */ |
|
| 80 | + public function __construct( |
|
| 81 | + ConfigInterface $config, |
|
| 82 | + ViewFinderInterface $viewFinder = null, |
|
| 83 | + EngineFinderInterface $engineFinder = null |
|
| 84 | + ) { |
|
| 85 | + $this->processConfig($config); |
|
| 86 | + $this->viewFinder = $viewFinder; |
|
| 87 | + $this->engineFinder = $engineFinder; |
|
| 88 | + $this->locations = new LocationCollection(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Create a new view for a given URI. |
|
| 93 | - * |
|
| 94 | - * @since 0.1.0 |
|
| 95 | - * |
|
| 96 | - * @param string $view View identifier to create a view for. |
|
| 97 | - * @param mixed $type Type of view to create. |
|
| 98 | - * |
|
| 99 | - * @return ViewInterface Instance of the requested view. |
|
| 100 | - */ |
|
| 101 | - public function create($view, $type = null) |
|
| 102 | - { |
|
| 103 | - $uri = $this->scanLocations([$view]); |
|
| 104 | - $engine = $this->getEngine($uri); |
|
| 91 | + /** |
|
| 92 | + * Create a new view for a given URI. |
|
| 93 | + * |
|
| 94 | + * @since 0.1.0 |
|
| 95 | + * |
|
| 96 | + * @param string $view View identifier to create a view for. |
|
| 97 | + * @param mixed $type Type of view to create. |
|
| 98 | + * |
|
| 99 | + * @return ViewInterface Instance of the requested view. |
|
| 100 | + */ |
|
| 101 | + public function create($view, $type = null) |
|
| 102 | + { |
|
| 103 | + $uri = $this->scanLocations([$view]); |
|
| 104 | + $engine = $this->getEngine($uri); |
|
| 105 | 105 | |
| 106 | - return $uri |
|
| 107 | - ? $this->getView($uri, $engine, $type) |
|
| 108 | - : $this->getViewFinder()->getNullObject(); |
|
| 109 | - } |
|
| 106 | + return $uri |
|
| 107 | + ? $this->getView($uri, $engine, $type) |
|
| 108 | + : $this->getViewFinder()->getNullObject(); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Get an Engine that can deal with the given URI. |
|
| 113 | - * |
|
| 114 | - * @since 0.1.0 |
|
| 115 | - * |
|
| 116 | - * @param string|false $uri URI to get an engine for. |
|
| 117 | - * |
|
| 118 | - * @return EngineInterface Instance of an engine that can deal with the given URI. |
|
| 119 | - */ |
|
| 120 | - public function getEngine($uri) |
|
| 121 | - { |
|
| 122 | - return $this->getEngineFinder()->find([$uri]); |
|
| 123 | - } |
|
| 111 | + /** |
|
| 112 | + * Get an Engine that can deal with the given URI. |
|
| 113 | + * |
|
| 114 | + * @since 0.1.0 |
|
| 115 | + * |
|
| 116 | + * @param string|false $uri URI to get an engine for. |
|
| 117 | + * |
|
| 118 | + * @return EngineInterface Instance of an engine that can deal with the given URI. |
|
| 119 | + */ |
|
| 120 | + public function getEngine($uri) |
|
| 121 | + { |
|
| 122 | + return $this->getEngineFinder()->find([$uri]); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Get a view for a given URI, engine and type. |
|
| 127 | - * |
|
| 128 | - * @since 0.1.0 |
|
| 129 | - * |
|
| 130 | - * @param string $uri URI to get a view for. |
|
| 131 | - * @param EngineInterface $engine Engine to use for the view. |
|
| 132 | - * @param mixed $type Type of view to get. |
|
| 133 | - * |
|
| 134 | - * @return ViewInterface View that matches the given requirements. |
|
| 135 | - */ |
|
| 136 | - public function getView($uri, EngineInterface $engine, $type = null) |
|
| 137 | - { |
|
| 138 | - if (null === $type) { |
|
| 139 | - return $this->getViewFinder()->find([$uri], $engine); |
|
| 140 | - } |
|
| 125 | + /** |
|
| 126 | + * Get a view for a given URI, engine and type. |
|
| 127 | + * |
|
| 128 | + * @since 0.1.0 |
|
| 129 | + * |
|
| 130 | + * @param string $uri URI to get a view for. |
|
| 131 | + * @param EngineInterface $engine Engine to use for the view. |
|
| 132 | + * @param mixed $type Type of view to get. |
|
| 133 | + * |
|
| 134 | + * @return ViewInterface View that matches the given requirements. |
|
| 135 | + */ |
|
| 136 | + public function getView($uri, EngineInterface $engine, $type = null) |
|
| 137 | + { |
|
| 138 | + if (null === $type) { |
|
| 139 | + return $this->getViewFinder()->find([$uri], $engine); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - return $this->resolveType($type, $uri, $engine); |
|
| 143 | - } |
|
| 142 | + return $this->resolveType($type, $uri, $engine); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Get the ViewFinder instance. |
|
| 147 | - * |
|
| 148 | - * @since 0.1.0 |
|
| 149 | - * |
|
| 150 | - * @return ViewFinderInterface Instance of a ViewFinder. |
|
| 151 | - */ |
|
| 152 | - public function getViewFinder() |
|
| 153 | - { |
|
| 154 | - return $this->getFinder($this->viewFinder, static::VIEW_FINDER_KEY); |
|
| 155 | - } |
|
| 145 | + /** |
|
| 146 | + * Get the ViewFinder instance. |
|
| 147 | + * |
|
| 148 | + * @since 0.1.0 |
|
| 149 | + * |
|
| 150 | + * @return ViewFinderInterface Instance of a ViewFinder. |
|
| 151 | + */ |
|
| 152 | + public function getViewFinder() |
|
| 153 | + { |
|
| 154 | + return $this->getFinder($this->viewFinder, static::VIEW_FINDER_KEY); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * Get the EngineFinder instance. |
|
| 159 | - * |
|
| 160 | - * @since 0.1.0 |
|
| 161 | - * |
|
| 162 | - * @return EngineFinderInterface Instance of a EngineFinder. |
|
| 163 | - */ |
|
| 164 | - public function getEngineFinder() |
|
| 165 | - { |
|
| 166 | - return $this->getFinder($this->engineFinder, static::ENGINE_FINDER_KEY); |
|
| 167 | - } |
|
| 157 | + /** |
|
| 158 | + * Get the EngineFinder instance. |
|
| 159 | + * |
|
| 160 | + * @since 0.1.0 |
|
| 161 | + * |
|
| 162 | + * @return EngineFinderInterface Instance of a EngineFinder. |
|
| 163 | + */ |
|
| 164 | + public function getEngineFinder() |
|
| 165 | + { |
|
| 166 | + return $this->getFinder($this->engineFinder, static::ENGINE_FINDER_KEY); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Add a location to scan with the ViewFinder. |
|
| 171 | - * |
|
| 172 | - * @since 0.1.0 |
|
| 173 | - * |
|
| 174 | - * @param LocationInterface $location Location to scan with the ViewFinder. |
|
| 175 | - */ |
|
| 176 | - public function addLocation(LocationInterface $location) |
|
| 177 | - { |
|
| 178 | - $this->locations->add($location); |
|
| 179 | - } |
|
| 169 | + /** |
|
| 170 | + * Add a location to scan with the ViewFinder. |
|
| 171 | + * |
|
| 172 | + * @since 0.1.0 |
|
| 173 | + * |
|
| 174 | + * @param LocationInterface $location Location to scan with the ViewFinder. |
|
| 175 | + */ |
|
| 176 | + public function addLocation(LocationInterface $location) |
|
| 177 | + { |
|
| 178 | + $this->locations->add($location); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Scan Locations for an URI that matches the specified criteria. |
|
| 183 | - * |
|
| 184 | - * @since 0.1.0 |
|
| 185 | - * |
|
| 186 | - * @param array $criteria Criteria to match. |
|
| 187 | - * |
|
| 188 | - * @return string|false URI of the requested view, or false if not found. |
|
| 189 | - */ |
|
| 190 | - public function scanLocations(array $criteria) |
|
| 191 | - { |
|
| 192 | - return $this->locations->map(function ($location) use ($criteria) { |
|
| 193 | - /** @var LocationInterface $location */ |
|
| 194 | - return $location->getURI($criteria); |
|
| 195 | - })->filter(function ($uri) { |
|
| 196 | - return false !== $uri; |
|
| 197 | - })->first() ?: false; |
|
| 198 | - } |
|
| 181 | + /** |
|
| 182 | + * Scan Locations for an URI that matches the specified criteria. |
|
| 183 | + * |
|
| 184 | + * @since 0.1.0 |
|
| 185 | + * |
|
| 186 | + * @param array $criteria Criteria to match. |
|
| 187 | + * |
|
| 188 | + * @return string|false URI of the requested view, or false if not found. |
|
| 189 | + */ |
|
| 190 | + public function scanLocations(array $criteria) |
|
| 191 | + { |
|
| 192 | + return $this->locations->map(function ($location) use ($criteria) { |
|
| 193 | + /** @var LocationInterface $location */ |
|
| 194 | + return $location->getURI($criteria); |
|
| 195 | + })->filter(function ($uri) { |
|
| 196 | + return false !== $uri; |
|
| 197 | + })->first() ?: false; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * Get a finder instance. |
|
| 202 | - * |
|
| 203 | - * @since 0.1.1 |
|
| 204 | - * |
|
| 205 | - * @param mixed $property Property to use. |
|
| 206 | - * @param string $key Configuration key to use. |
|
| 207 | - * |
|
| 208 | - * @return FinderInterface The requested finder instance. |
|
| 209 | - */ |
|
| 210 | - protected function getFinder(&$property, $key) |
|
| 211 | - { |
|
| 212 | - if (null === $property) { |
|
| 213 | - $engineFinderClass = $this->config->getKey($key, 'ClassName'); |
|
| 214 | - $property = new $engineFinderClass($this->config->getSubConfig($key)); |
|
| 215 | - } |
|
| 200 | + /** |
|
| 201 | + * Get a finder instance. |
|
| 202 | + * |
|
| 203 | + * @since 0.1.1 |
|
| 204 | + * |
|
| 205 | + * @param mixed $property Property to use. |
|
| 206 | + * @param string $key Configuration key to use. |
|
| 207 | + * |
|
| 208 | + * @return FinderInterface The requested finder instance. |
|
| 209 | + */ |
|
| 210 | + protected function getFinder(&$property, $key) |
|
| 211 | + { |
|
| 212 | + if (null === $property) { |
|
| 213 | + $engineFinderClass = $this->config->getKey($key, 'ClassName'); |
|
| 214 | + $property = new $engineFinderClass($this->config->getSubConfig($key)); |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - return $property; |
|
| 218 | - } |
|
| 217 | + return $property; |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Resolve the view type. |
|
| 222 | - * |
|
| 223 | - * @since 0.1.0 |
|
| 224 | - * |
|
| 225 | - * @param mixed $type Type of view that was requested. |
|
| 226 | - * @param string $uri URI to get a view for. |
|
| 227 | - * @param EngineInterface|null $engine Engine to use for the view. |
|
| 228 | - * |
|
| 229 | - * @return ViewInterface Resolved View object. |
|
| 230 | - * @throws FailedToInstantiateViewException If the view type could not be resolved. |
|
| 231 | - */ |
|
| 232 | - protected function resolveType($type, $uri, EngineInterface $engine = null) |
|
| 233 | - { |
|
| 234 | - $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
| 220 | + /** |
|
| 221 | + * Resolve the view type. |
|
| 222 | + * |
|
| 223 | + * @since 0.1.0 |
|
| 224 | + * |
|
| 225 | + * @param mixed $type Type of view that was requested. |
|
| 226 | + * @param string $uri URI to get a view for. |
|
| 227 | + * @param EngineInterface|null $engine Engine to use for the view. |
|
| 228 | + * |
|
| 229 | + * @return ViewInterface Resolved View object. |
|
| 230 | + * @throws FailedToInstantiateViewException If the view type could not be resolved. |
|
| 231 | + */ |
|
| 232 | + protected function resolveType($type, $uri, EngineInterface $engine = null) |
|
| 233 | + { |
|
| 234 | + $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
| 235 | 235 | |
| 236 | - if (is_string($type) && $this->config->hasKey($configKey)) { |
|
| 237 | - $className = $this->config->getKey($configKey); |
|
| 238 | - $type = new $className($uri, $engine); |
|
| 239 | - } |
|
| 236 | + if (is_string($type) && $this->config->hasKey($configKey)) { |
|
| 237 | + $className = $this->config->getKey($configKey); |
|
| 238 | + $type = new $className($uri, $engine); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - if (is_string($type)) { |
|
| 242 | - $type = new $type($uri, $engine); |
|
| 243 | - } |
|
| 241 | + if (is_string($type)) { |
|
| 242 | + $type = new $type($uri, $engine); |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - if (is_callable($type)) { |
|
| 246 | - $type = $type($uri, $engine); |
|
| 247 | - } |
|
| 245 | + if (is_callable($type)) { |
|
| 246 | + $type = $type($uri, $engine); |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - if (! $type instanceof ViewInterface) { |
|
| 250 | - throw new FailedToInstantiateViewException( |
|
| 251 | - sprintf( |
|
| 252 | - _('Could not instantiate view "%s".'), |
|
| 253 | - serialize($type) |
|
| 254 | - ) |
|
| 255 | - ); |
|
| 256 | - } |
|
| 249 | + if (! $type instanceof ViewInterface) { |
|
| 250 | + throw new FailedToInstantiateViewException( |
|
| 251 | + sprintf( |
|
| 252 | + _('Could not instantiate view "%s".'), |
|
| 253 | + serialize($type) |
|
| 254 | + ) |
|
| 255 | + ); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - return $type; |
|
| 259 | - } |
|
| 258 | + return $type; |
|
| 259 | + } |
|
| 260 | 260 | } |
@@ -189,10 +189,10 @@ discard block |
||
| 189 | 189 | */ |
| 190 | 190 | public function scanLocations(array $criteria) |
| 191 | 191 | { |
| 192 | - return $this->locations->map(function ($location) use ($criteria) { |
|
| 192 | + return $this->locations->map(function($location) use ($criteria) { |
|
| 193 | 193 | /** @var LocationInterface $location */ |
| 194 | 194 | return $location->getURI($criteria); |
| 195 | - })->filter(function ($uri) { |
|
| 195 | + })->filter(function($uri) { |
|
| 196 | 196 | return false !== $uri; |
| 197 | 197 | })->first() ?: false; |
| 198 | 198 | } |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $type = $type($uri, $engine); |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - if (! $type instanceof ViewInterface) { |
|
| 249 | + if ( ! $type instanceof ViewInterface) { |
|
| 250 | 250 | throw new FailedToInstantiateViewException( |
| 251 | 251 | sprintf( |
| 252 | 252 | _('Could not instantiate view "%s".'), |
@@ -25,150 +25,150 @@ |
||
| 25 | 25 | class FilesystemLocation implements LocationInterface |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Path that this location points to. |
|
| 30 | - * |
|
| 31 | - * @since 0.1.0 |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected $path; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Extensions that this location can accept. |
|
| 39 | - * |
|
| 40 | - * @since 0.1.0 |
|
| 41 | - * |
|
| 42 | - * @var ExtensionCollection |
|
| 43 | - */ |
|
| 44 | - protected $extensions; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Instantiate a FilesystemLocation object. |
|
| 48 | - * |
|
| 49 | - * @since 0.1.0 |
|
| 50 | - * |
|
| 51 | - * @param string $path Path that this location points to. |
|
| 52 | - * @param ExtensionCollection|array|string $extensions Extensions that this location can accept. |
|
| 53 | - */ |
|
| 54 | - public function __construct($path, $extensions = null) |
|
| 55 | - { |
|
| 56 | - $this->path = $path; |
|
| 57 | - $this->extensions = $this->validateExtensions($extensions); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Get the first URI that matches the given criteria. |
|
| 62 | - * |
|
| 63 | - * @since 0.1.0 |
|
| 64 | - * |
|
| 65 | - * @param array $criteria Criteria to match. |
|
| 66 | - * |
|
| 67 | - * @return string|false URI that matches the criteria or false if none found. |
|
| 68 | - */ |
|
| 69 | - public function getURI(array $criteria) |
|
| 70 | - { |
|
| 71 | - foreach ($criteria as $entry) { |
|
| 72 | - if ($uri = $this->transform($entry, true)) { |
|
| 73 | - return $uri; |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Get all URIs that match the given criteria. |
|
| 82 | - * |
|
| 83 | - * @since 0.1.1 |
|
| 84 | - * |
|
| 85 | - * @param array $criteria Criteria to match. |
|
| 86 | - * |
|
| 87 | - * @return array URIs that match the criteria or empty array if none found. |
|
| 88 | - */ |
|
| 89 | - public function getURIs(array $criteria) |
|
| 90 | - { |
|
| 91 | - $uris = []; |
|
| 92 | - |
|
| 93 | - foreach ($criteria as $entry) { |
|
| 94 | - if ($uri = $this->transform($entry, false)) { |
|
| 95 | - $uris = array_merge($uris, (array)$uri); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return $uris; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Validate the extensions and return a collection. |
|
| 104 | - * |
|
| 105 | - * @since 0.1.1 |
|
| 106 | - * |
|
| 107 | - * @param ExtensionCollection|array|string $extensions Extensions to validate. |
|
| 108 | - * |
|
| 109 | - * @return ExtensionCollection Validated extensions collection. |
|
| 110 | - */ |
|
| 111 | - protected function validateExtensions($extensions) |
|
| 112 | - { |
|
| 113 | - if (! $extensions instanceof ExtensionCollection) { |
|
| 114 | - $extensions = new ExtensionCollection((array)$extensions); |
|
| 115 | - } |
|
| 116 | - $extensions->add(''); |
|
| 117 | - |
|
| 118 | - return $extensions; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Try to transform the entry into possible URIs. |
|
| 123 | - * |
|
| 124 | - * @since 0.1.0 |
|
| 125 | - * |
|
| 126 | - * @param string $entry Entry to transform. |
|
| 127 | - * @param bool $firstOnly Return the first result only. |
|
| 128 | - * |
|
| 129 | - * @return array|string|false If $firstOnly is true, returns a string with the URI of the view, or false if none |
|
| 130 | - * found. |
|
| 131 | - * If $firstOnly is false, returns an array with all matching URIs, or an empty array if |
|
| 132 | - * none found. |
|
| 133 | - */ |
|
| 134 | - protected function transform($entry, $firstOnly = true) |
|
| 135 | - { |
|
| 136 | - $uris = []; |
|
| 137 | - |
|
| 138 | - try { |
|
| 139 | - foreach ($this->getVariants($entry) as $uri) { |
|
| 140 | - if (is_readable($uri)) { |
|
| 141 | - if ($firstOnly) { |
|
| 142 | - return $uri; |
|
| 143 | - } |
|
| 144 | - $uris [] = $uri; |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - } catch (Exception $exception) { |
|
| 148 | - // Fail silently. |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return $firstOnly ? false : $uris; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Get the individual variants that could be matched for the location. |
|
| 156 | - * |
|
| 157 | - * @since 0.1.1 |
|
| 158 | - * |
|
| 159 | - * @param string $entry Entry to get the variants for. |
|
| 160 | - * |
|
| 161 | - * @return array Array of variants to check. |
|
| 162 | - */ |
|
| 163 | - protected function getVariants($entry) |
|
| 164 | - { |
|
| 165 | - $variants = []; |
|
| 166 | - |
|
| 167 | - $this->extensions->map(function ($extension) use ($entry, &$variants) { |
|
| 168 | - $variants[] = $entry . $extension; |
|
| 169 | - $variants[] = $this->path . DIRECTORY_SEPARATOR . $entry . $extension; |
|
| 170 | - }); |
|
| 171 | - |
|
| 172 | - return $variants; |
|
| 173 | - } |
|
| 28 | + /** |
|
| 29 | + * Path that this location points to. |
|
| 30 | + * |
|
| 31 | + * @since 0.1.0 |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected $path; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Extensions that this location can accept. |
|
| 39 | + * |
|
| 40 | + * @since 0.1.0 |
|
| 41 | + * |
|
| 42 | + * @var ExtensionCollection |
|
| 43 | + */ |
|
| 44 | + protected $extensions; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Instantiate a FilesystemLocation object. |
|
| 48 | + * |
|
| 49 | + * @since 0.1.0 |
|
| 50 | + * |
|
| 51 | + * @param string $path Path that this location points to. |
|
| 52 | + * @param ExtensionCollection|array|string $extensions Extensions that this location can accept. |
|
| 53 | + */ |
|
| 54 | + public function __construct($path, $extensions = null) |
|
| 55 | + { |
|
| 56 | + $this->path = $path; |
|
| 57 | + $this->extensions = $this->validateExtensions($extensions); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Get the first URI that matches the given criteria. |
|
| 62 | + * |
|
| 63 | + * @since 0.1.0 |
|
| 64 | + * |
|
| 65 | + * @param array $criteria Criteria to match. |
|
| 66 | + * |
|
| 67 | + * @return string|false URI that matches the criteria or false if none found. |
|
| 68 | + */ |
|
| 69 | + public function getURI(array $criteria) |
|
| 70 | + { |
|
| 71 | + foreach ($criteria as $entry) { |
|
| 72 | + if ($uri = $this->transform($entry, true)) { |
|
| 73 | + return $uri; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Get all URIs that match the given criteria. |
|
| 82 | + * |
|
| 83 | + * @since 0.1.1 |
|
| 84 | + * |
|
| 85 | + * @param array $criteria Criteria to match. |
|
| 86 | + * |
|
| 87 | + * @return array URIs that match the criteria or empty array if none found. |
|
| 88 | + */ |
|
| 89 | + public function getURIs(array $criteria) |
|
| 90 | + { |
|
| 91 | + $uris = []; |
|
| 92 | + |
|
| 93 | + foreach ($criteria as $entry) { |
|
| 94 | + if ($uri = $this->transform($entry, false)) { |
|
| 95 | + $uris = array_merge($uris, (array)$uri); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return $uris; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Validate the extensions and return a collection. |
|
| 104 | + * |
|
| 105 | + * @since 0.1.1 |
|
| 106 | + * |
|
| 107 | + * @param ExtensionCollection|array|string $extensions Extensions to validate. |
|
| 108 | + * |
|
| 109 | + * @return ExtensionCollection Validated extensions collection. |
|
| 110 | + */ |
|
| 111 | + protected function validateExtensions($extensions) |
|
| 112 | + { |
|
| 113 | + if (! $extensions instanceof ExtensionCollection) { |
|
| 114 | + $extensions = new ExtensionCollection((array)$extensions); |
|
| 115 | + } |
|
| 116 | + $extensions->add(''); |
|
| 117 | + |
|
| 118 | + return $extensions; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Try to transform the entry into possible URIs. |
|
| 123 | + * |
|
| 124 | + * @since 0.1.0 |
|
| 125 | + * |
|
| 126 | + * @param string $entry Entry to transform. |
|
| 127 | + * @param bool $firstOnly Return the first result only. |
|
| 128 | + * |
|
| 129 | + * @return array|string|false If $firstOnly is true, returns a string with the URI of the view, or false if none |
|
| 130 | + * found. |
|
| 131 | + * If $firstOnly is false, returns an array with all matching URIs, or an empty array if |
|
| 132 | + * none found. |
|
| 133 | + */ |
|
| 134 | + protected function transform($entry, $firstOnly = true) |
|
| 135 | + { |
|
| 136 | + $uris = []; |
|
| 137 | + |
|
| 138 | + try { |
|
| 139 | + foreach ($this->getVariants($entry) as $uri) { |
|
| 140 | + if (is_readable($uri)) { |
|
| 141 | + if ($firstOnly) { |
|
| 142 | + return $uri; |
|
| 143 | + } |
|
| 144 | + $uris [] = $uri; |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + } catch (Exception $exception) { |
|
| 148 | + // Fail silently. |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return $firstOnly ? false : $uris; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Get the individual variants that could be matched for the location. |
|
| 156 | + * |
|
| 157 | + * @since 0.1.1 |
|
| 158 | + * |
|
| 159 | + * @param string $entry Entry to get the variants for. |
|
| 160 | + * |
|
| 161 | + * @return array Array of variants to check. |
|
| 162 | + */ |
|
| 163 | + protected function getVariants($entry) |
|
| 164 | + { |
|
| 165 | + $variants = []; |
|
| 166 | + |
|
| 167 | + $this->extensions->map(function ($extension) use ($entry, &$variants) { |
|
| 168 | + $variants[] = $entry . $extension; |
|
| 169 | + $variants[] = $this->path . DIRECTORY_SEPARATOR . $entry . $extension; |
|
| 170 | + }); |
|
| 171 | + |
|
| 172 | + return $variants; |
|
| 173 | + } |
|
| 174 | 174 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | |
| 93 | 93 | foreach ($criteria as $entry) { |
| 94 | 94 | if ($uri = $this->transform($entry, false)) { |
| 95 | - $uris = array_merge($uris, (array)$uri); |
|
| 95 | + $uris = array_merge($uris, (array) $uri); |
|
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | |
@@ -110,8 +110,8 @@ discard block |
||
| 110 | 110 | */ |
| 111 | 111 | protected function validateExtensions($extensions) |
| 112 | 112 | { |
| 113 | - if (! $extensions instanceof ExtensionCollection) { |
|
| 114 | - $extensions = new ExtensionCollection((array)$extensions); |
|
| 113 | + if ( ! $extensions instanceof ExtensionCollection) { |
|
| 114 | + $extensions = new ExtensionCollection((array) $extensions); |
|
| 115 | 115 | } |
| 116 | 116 | $extensions->add(''); |
| 117 | 117 | |
@@ -164,9 +164,9 @@ discard block |
||
| 164 | 164 | { |
| 165 | 165 | $variants = []; |
| 166 | 166 | |
| 167 | - $this->extensions->map(function ($extension) use ($entry, &$variants) { |
|
| 168 | - $variants[] = $entry . $extension; |
|
| 169 | - $variants[] = $this->path . DIRECTORY_SEPARATOR . $entry . $extension; |
|
| 167 | + $this->extensions->map(function($extension) use ($entry, &$variants) { |
|
| 168 | + $variants[] = $entry.$extension; |
|
| 169 | + $variants[] = $this->path.DIRECTORY_SEPARATOR.$entry.$extension; |
|
| 170 | 170 | }); |
| 171 | 171 | |
| 172 | 172 | return $variants; |