Passed
Push — master ( 8e8597...ac1311 )
by Alain
02:41
created
src/View/Support/AbstractFinder.php 1 patch
Indentation   +205 added lines, -205 removed lines patch added patch discarded remove patch
@@ -27,209 +27,209 @@
 block discarded – undo
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 NullFindable
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 NullFindable 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
-     * @since 0.1.1
135
-     *
136
-     * @param mixed $arguments Optional. Arguments to use.
137
-     */
138
-    protected function initializeNullObject($arguments = null)
139
-    {
140
-        $this->nullObject = $this->maybeInstantiateFindable($this->nullObject, $arguments);
141
-    }
142
-
143
-    /**
144
-     * Initialize the Findables that can be iterated.
145
-     *
146
-     * @param mixed $arguments Optional. Arguments to use.
147
-     *
148
-     * @since 0.1.0
149
-     *
150
-     */
151
-    protected function initializeFindables($arguments = null)
152
-    {
153
-        $this->findables = $this->findables->map(function ($findable) use ($arguments) {
154
-            return $this->initializeFindable($findable, $arguments);
155
-        });
156
-    }
157
-
158
-    /**
159
-     * Initialize a single findable by instantiating class name strings and calling closures.
160
-     *
161
-     * @since 0.1.0
162
-     *
163
-     * @param mixed $findable  Findable to instantiate.
164
-     * @param mixed $arguments Optional. Arguments to use.
165
-     *
166
-     * @return Findable Instantiated findable.
167
-     */
168
-    protected function initializeFindable($findable, $arguments = null)
169
-    {
170
-        return $this->maybeInstantiateFindable($findable, $arguments);
171
-    }
172
-
173
-    /**
174
-     * Maybe instantiate a Findable if it is not yet an object.
175
-     *
176
-     * @since 0.1.1
177
-     *
178
-     * @param mixed $findable  Findable to instantiate.
179
-     * @param mixed $arguments Optional. Arguments to use.
180
-     *
181
-     * @return Findable Instantiated findable.
182
-     * @throws FailedToInstantiateFindableException If the findable could not be instantiated.
183
-     */
184
-    protected function maybeInstantiateFindable($findable, $arguments = null)
185
-    {
186
-        if (is_string($findable)) {
187
-            $findable = $this->instantiateFindableFromString($findable, $arguments);
188
-        }
189
-
190
-        if (is_callable($findable)) {
191
-            $findable = $this->instantiateFindableFromCallable($findable, $arguments);
192
-        }
193
-
194
-        if (! $findable instanceof Findable) {
195
-            throw new FailedToInstantiateFindableException(
196
-                sprintf(
197
-                    _('Could not instantiate Findable "%s".'),
198
-                    serialize($findable)
199
-                )
200
-            );
201
-        }
202
-
203
-        return $findable;
204
-    }
205
-
206
-    /**
207
-     * Instantiate a Findable from a string.
208
-     *
209
-     * @since 0.1.1
210
-     *
211
-     * @param string $string    String to use for instantiation.
212
-     * @param mixed  $arguments Optional. Arguments to use for instantiation.
213
-     *
214
-     * @return Findable Instantiated Findable.
215
-     */
216
-    protected function instantiateFindableFromString($string, $arguments = null)
217
-    {
218
-        return new $string(...$arguments);
219
-    }
220
-
221
-    /**
222
-     * Instantiate a Findable from a callable.
223
-     *
224
-     * @since 0.1.1
225
-     *
226
-     * @param callable $callable  Callable to use for instantiation.
227
-     * @param mixed    $arguments Optional. Arguments to use for instantiation.
228
-     *
229
-     * @return Findable Instantiated Findable.
230
-     */
231
-    protected function instantiateFindableFromCallable($callable, $arguments = null)
232
-    {
233
-        return $callable(...$arguments);
234
-    }
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 NullFindable
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 NullFindable 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
+	 * @since 0.1.1
135
+	 *
136
+	 * @param mixed $arguments Optional. Arguments to use.
137
+	 */
138
+	protected function initializeNullObject($arguments = null)
139
+	{
140
+		$this->nullObject = $this->maybeInstantiateFindable($this->nullObject, $arguments);
141
+	}
142
+
143
+	/**
144
+	 * Initialize the Findables that can be iterated.
145
+	 *
146
+	 * @param mixed $arguments Optional. Arguments to use.
147
+	 *
148
+	 * @since 0.1.0
149
+	 *
150
+	 */
151
+	protected function initializeFindables($arguments = null)
152
+	{
153
+		$this->findables = $this->findables->map(function ($findable) use ($arguments) {
154
+			return $this->initializeFindable($findable, $arguments);
155
+		});
156
+	}
157
+
158
+	/**
159
+	 * Initialize a single findable by instantiating class name strings and calling closures.
160
+	 *
161
+	 * @since 0.1.0
162
+	 *
163
+	 * @param mixed $findable  Findable to instantiate.
164
+	 * @param mixed $arguments Optional. Arguments to use.
165
+	 *
166
+	 * @return Findable Instantiated findable.
167
+	 */
168
+	protected function initializeFindable($findable, $arguments = null)
169
+	{
170
+		return $this->maybeInstantiateFindable($findable, $arguments);
171
+	}
172
+
173
+	/**
174
+	 * Maybe instantiate a Findable if it is not yet an object.
175
+	 *
176
+	 * @since 0.1.1
177
+	 *
178
+	 * @param mixed $findable  Findable to instantiate.
179
+	 * @param mixed $arguments Optional. Arguments to use.
180
+	 *
181
+	 * @return Findable Instantiated findable.
182
+	 * @throws FailedToInstantiateFindableException If the findable could not be instantiated.
183
+	 */
184
+	protected function maybeInstantiateFindable($findable, $arguments = null)
185
+	{
186
+		if (is_string($findable)) {
187
+			$findable = $this->instantiateFindableFromString($findable, $arguments);
188
+		}
189
+
190
+		if (is_callable($findable)) {
191
+			$findable = $this->instantiateFindableFromCallable($findable, $arguments);
192
+		}
193
+
194
+		if (! $findable instanceof Findable) {
195
+			throw new FailedToInstantiateFindableException(
196
+				sprintf(
197
+					_('Could not instantiate Findable "%s".'),
198
+					serialize($findable)
199
+				)
200
+			);
201
+		}
202
+
203
+		return $findable;
204
+	}
205
+
206
+	/**
207
+	 * Instantiate a Findable from a string.
208
+	 *
209
+	 * @since 0.1.1
210
+	 *
211
+	 * @param string $string    String to use for instantiation.
212
+	 * @param mixed  $arguments Optional. Arguments to use for instantiation.
213
+	 *
214
+	 * @return Findable Instantiated Findable.
215
+	 */
216
+	protected function instantiateFindableFromString($string, $arguments = null)
217
+	{
218
+		return new $string(...$arguments);
219
+	}
220
+
221
+	/**
222
+	 * Instantiate a Findable from a callable.
223
+	 *
224
+	 * @since 0.1.1
225
+	 *
226
+	 * @param callable $callable  Callable to use for instantiation.
227
+	 * @param mixed    $arguments Optional. Arguments to use for instantiation.
228
+	 *
229
+	 * @return Findable Instantiated Findable.
230
+	 */
231
+	protected function instantiateFindableFromCallable($callable, $arguments = null)
232
+	{
233
+		return $callable(...$arguments);
234
+	}
235 235
 }
Please login to merge, or discard this patch.
src/View/Location/FilesystemLocation.php 1 patch
Indentation   +146 added lines, -146 removed lines patch added patch discarded remove patch
@@ -25,150 +25,150 @@
 block discarded – undo
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|null $extensions Optional. 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|null $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|null $extensions Optional. 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|null $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
 }
Please login to merge, or discard this patch.